module MongoidRecurring::HasRecurringFields::ClassMethods

Public Instance Methods

has_recurring_fields(options={}) click to toggle source
# File lib/mongoid_recurring/has_recurring_fields.rb, line 17
def has_recurring_fields options={}
   @@schedule_duration = options[:schedule_duration] if options[:schedule_duration].present?

  field :dtstart, type: DateTime
  field :dtend, type: DateTime
  field :all_day, type: Boolean, default: false

  field :schedule, type: MongoidIceCubeExtension::Schedule
  field :schedule_dtend, type: DateTime

  # ---------------------------------------------------------------------

  embeds_many :occurrences, class_name: 'MongoidRecurring::Occurrence', order: :dtstart.asc

  validates :dtstart, presence: true

  before_save :expand_schedule_to_occurrences

  # ---------------------------------------------------------------------

  scope :for_datetime_range, -> (dtstart, dtend) {
    where({ occurrences: { "$elemMatch" => Occurrence.for_datetime_range(dtstart, dtend).selector } })
  }

  scope :from_datetime, -> (dtstart) {
    where( occurrences: { "$elemMatch" => Occurrence.from_datetime(dtstart).selector } )
  }

  scope :to_datetime, -> (dtend) {
    where( occurrences: { "$elemMatch" => Occurrence.to_datetime(dtend).selector } )
  }
end