Sha256: e73d18531a5d53baa7de1923bf6f1f2d9b378e5ce442f4514c1315539fe300e6

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

module Eventable::Model

  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



  module ClassMethods
    private

    def configure_eventable(*args)
      @eventable_options = args.extract_options!
      if u = @eventable_options[:user_class_name]
        Eventable::ProgramItem.reflect_on_association(:user).options[:class_name] = u
      end
    end
  end

  module InstanceMethods
    def pricelist
      Eventable::Pricelist.new(self)
    end

    def opening_periodlist
      Eventable::OpeningPeriodlist.new(self)
    end

    def categorylist
      Eventable::Categorylist.new(self)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dcs-eventable-0.0.8 lib/eventable/model.rb