Module: Eventable::Model

Defined in:
lib/eventable/model.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/eventable/model.rb', line 3

def self.included(base)   # :nodoc:
  raise "cannot declare acts_as_event on more than one model! " +
    "(#{Eventable.model_name} & #{base.name})" if
    Eventable.model_name.present? && Eventable.model_name != base.name
  Eventable.model_name = base.name
  [Eventable::ProgramItem,
   Eventable::Price,
   Eventable::OpeningPeriod,
   Eventable::Categorization].each do |m|
    m.reflect_on_association(:event).options[:class_name] = base.name
  end

  base.instance_eval do
    has_many :program_items,   :foreign_key => :event_id, :class_name => 'Eventable::ProgramItem',    :dependent => :destroy
    has_many :prices,          :foreign_key => :event_id, :class_name => 'Eventable::Price',          :dependent => :destroy
    has_many :opening_periods, :foreign_key => :event_id, :class_name => 'Eventable::OpeningPeriod',  :dependent => :destroy
    has_many :categorizations, :foreign_key => :event_id, :class_name => 'Eventable::Categorization', :dependent => :destroy
    has_many :categories, :through => :categorizations,  :class_name => 'Eventable::Category'
    accepts_nested_attributes_for :prices, :allow_destroy => true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
    accepts_nested_attributes_for :opening_periods, :allow_destroy => true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
  end

  base.send :extend, ClassMethods
  base.send :include, InstanceMethods
end