module DateBook::ActsAsEventOccurrence

Mixin to allow acts_as_event_occurrence behavior in EventOccurrence model

Public Instance Methods

acts_as_event_occurrence(_options = {}) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/date_book/concerns/acts_as_event_occurrence.rb, line 8
def acts_as_event_occurrence(_options = {})
  before_save :set_end_date

  alias_attribute :start_date, :date

  belongs_to :schedulable, polymorphic: true
  alias_attribute :event, :schedulable
  delegate :schedule, to: :schedulable

  # Scopes
  scope :remaining, -> { where('date >= ?', Time.now) }
  scope :previous, -> { where('date < ?', Time.now) }
  scope :ending_after, (lambda do |start_date|
    (where 'end_date >= ?', start_date)
  end)
  scope :starting_before, ->(end_date) { where 'date < ?', end_date }
  scope :for_events, -> { where(schedulable_type: 'Event') }
  scope :for_schedulables, (lambda do |model_name, ids|
    where(schedulable_type: model_name).where('schedulable_id IN (?)', ids)
  end)
  scope :ascending, -> { order date: :asc }
  scope :descending, -> { order date: :desc }

  include InstanceMethods
  extend ClassMethods
end