module MongoidOccurrences::HasOccurrences::ClassMethods

Public Instance Methods

embeds_many_occurrences(options = {}) click to toggle source
# File lib/mongoid_occurrences/has_occurrences.rb, line 8
def embeds_many_occurrences(options = {})
  field :_previous_occurrences_cache_key, type: String

  embeds_many :occurrences, class_name: options.fetch(:class_name), cascade_callbacks: true, order: :ds.asc
  accepts_nested_attributes_for :occurrences, allow_destroy: true, reject_if: :all_blank

  embeds_many :daily_occurrences, class_name: 'MongoidOccurrences::DailyOccurrence', order: :dtstart.asc

  after_validation :assign_occurrences_cache_key!
  after_validation :assign_daily_occurrences!, if: :_previous_occurrences_cache_key_changed?

  scope :occurs_between, ->(dtstart, dtend) { elem_match(daily_occurrences: DailyOccurrence.occurs_between(dtstart, dtend).selector) }
  scope :occurs_from, ->(dtstart) { elem_match(daily_occurrences: DailyOccurrence.occurs_from(dtstart).selector) }
  scope :occurs_on, ->(day) { elem_match(daily_occurrences: DailyOccurrence.occurs_on(day).selector) }
  scope :occurs_until, ->(dtend) { elem_match(daily_occurrences: DailyOccurrence.occurs_until(dtend).selector) }
end