Module: ActsAsEventOwner::Core::ClassMethods

Defined in:
lib/acts_as_event_owner/core.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_event_owner(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/acts_as_event_owner/core.rb', line 8

def acts_as_event_owner options = {}
  include InstanceMethods

  class_eval do
    has_many :event_specifications, :class_name => ActsAsEventOwner::EventSpecification.name, :as => :owner, :dependent => :destroy
    has_many :events, :class_name => ActsAsEventOwner::EventOccurrence.name, :as => :owner, :readonly => true do
      def generate(options={})
        proxy_association.owner.event_specifications.find(:all, :conditions => "until IS NULL OR until >= '#{Time.zone.now.to_s(:db)}'").each {|spec| spec.generate_events(options)}
        self.reload
      end

      def <<(obj)
        raise ActsAsEventOwner::Exception.new("Do not add events directly- add event specifications, then call events.generate")
      end

      def build(attributes={})
        raise ActsAsEventOwner::Exception.new("Do not build events directly- build event specifications, then call events.generate")
      end

      def create(attributes={})
        raise ActsAsEventOwner::Exception.new("Do not create events directly- build event specifications, then call events.generate")
      end

      def upcoming
        find(:all, :conditions => ["start_at >= ?", Time.zone.now])
      end

      def past
        find(:all, :conditions => ["start_at < ?", Time.zone.now])
      end
    end
  end
end