0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/plugins/discourse-calendar/app/models/discourse_post_event/event_date.rb
Jarek Radosz 8baf4d5d4c
DEV: Enable Style/RedundantSelf rubocop rule (#40098)
(to be enabled in the shared config)
2026-05-19 19:27:45 +02:00

92 lines
2.9 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscoursePostEvent
class EventDate < ActiveRecord::Base
self.table_name = "discourse_calendar_post_event_dates"
belongs_to :event
scope :pending,
-> { where(finished_at: nil).joins(:event).merge(DiscoursePostEvent::Event.visible.open) }
scope :expired, -> { where("ends_at IS NOT NULL AND ends_at < ?", Time.now) }
scope :not_expired, -> { where("ends_at IS NULL OR ends_at > ?", Time.now) }
scope :current_first, -> { order(Arel.sql(current_ordering_sql)) }
def self.current_ordering_sql
<<~SQL.squish
finished_at IS NOT NULL,
CASE WHEN finished_at IS NULL THEN starts_at ELSE updated_at END DESC,
id DESC
SQL
end
after_commit :upsert_topic_custom_field, on: %i[create update]
def upsert_topic_custom_field
if event.post && event.post.is_first_post?
TopicCustomField.upsert(
{
topic_id: event.post.topic_id,
name: TOPIC_POST_EVENT_STARTS_AT,
value: starts_at,
created_at: Time.now,
updated_at: Time.now,
},
unique_by: "idx_topic_custom_fields_topic_post_event_starts_at",
)
TopicCustomField.upsert(
{
topic_id: event.post.topic_id,
name: TOPIC_POST_EVENT_ENDS_AT,
value: ends_at,
created_at: Time.now,
updated_at: Time.now,
},
unique_by: "idx_topic_custom_fields_topic_post_event_ends_at",
)
TopicCustomField.upsert(
{
topic_id: event.post.topic_id,
name: TOPIC_POST_EVENT_ALL_DAY,
value: event.all_day || false,
created_at: Time.now,
updated_at: Time.now,
},
unique_by: "idx_topic_custom_fields_topic_post_event_all_day",
)
end
end
def started?
starts_at <= Time.current
end
def ended?
(ends_at || starts_at.end_of_day) <= Time.current
end
end
end
# == Schema Information
#
# Table name: discourse_calendar_post_event_dates
#
# id :bigint not null, primary key
# ends_at :datetime
# event_started_sent_at :datetime
# event_will_start_sent_at :datetime
# finished_at :datetime
# reminder_counter :integer default(0)
# starts_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# event_id :integer
#
# Indexes
#
# idx_discourse_calendar_post_event_dates_event_id_starts_at_uniq (event_id,starts_at) UNIQUE
# index_discourse_calendar_post_event_dates_on_event_id (event_id)
# index_discourse_calendar_post_event_dates_on_event_id_and_dates (event_id,finished_at,starts_at DESC,updated_at DESC,id DESC)
# index_discourse_calendar_post_event_dates_on_finished_at (finished_at)
#