Sha256: f299a4c1484b95b2020c4dfb50113259e021cdf83b9ae09d74c8a0795e5d5de8

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

module DateBook
  module ActsAsEventOccurrence
    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, -> (start_date) { (where 'end_date >= ?', start_date) }
      scope :starting_before, -> (end_date) { where 'date < ?', end_date }
      scope :for_events, -> { where(schedulable_type: 'Event') }
      scope :for_schedulables, -> (model_name, ids) { where(schedulable_type: model_name).where('schedulable_id IN (?)',  ids) }
      scope :ascending, -> { order date: :asc }
      scope :descending, -> { order date: :desc }

      include InstanceMethods
      extend ClassMethods
    end

    module InstanceMethods
      def url
        event.url(self)
      end

      def popover_url
        event.popover_url(self)
      end

      def start_moment
        if schedule.all_day
          I18n.localize date, format: :moment_date
        else
          I18n.localize date, format: :moment_datetime
        end
      end
      alias_method :start, :start_moment

      def end_moment
        if schedule.all_day
          I18n.localize end_date, format: :moment_date
        else
          I18n.localize end_date, format: :moment_datetime
        end
      end
      alias_method :end, :end_moment

      private

      def set_end_date
        self.end_date = date + schedulable.schedule.duration
      end
    end

    module ClassMethods
      def event_ids
        for_events.pluck(:schedulable_id).uniq
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
date_book-0.0.6 lib/date_book/concerns/acts_as_event_occurrence.rb
date_book-0.0.5 lib/date_book/concerns/acts_as_event_occurrence.rb
date_book-0.0.3 lib/date_book/concerns/acts_as_event_occurrence.rb
date_book-0.0.2 lib/date_book/concerns/acts_as_event_occurrence.rb
date_book-0.0.1 lib/date_book/concerns/acts_as_event_occurrence.rb