Skip to content

A platform for community discussion. Free, open, simple.

3.3 KB
Raw Blame
# frozen_string_literal: true

module DiscourseEvents
  module Events
    class EventDate < ActiveRecord::Base
      self.table_name = "discourse_calendar_post_event_dates"
      belongs_to :event

      scope :pending,
            -> do
              where(finished_at: nil).joins(:event).merge(
                DiscourseEvents::Events::Event.visible.open,
              )
            end
      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

      # Mirrors the opening of Event#currently_within_event_timeframe?.
      def opens_at
        return starts_at.beginning_of_day if event.all_day
        starts_at - Event::EARLY_ACCESS_MINUTES.minutes
      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
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)
#