Module: Invoicing::TimeDependent::ActMethods
- Defined in:
- lib/invoicing/time_dependent.rb
Instance Method Summary collapse
-
#acts_as_time_dependent(*args) ⇒ Object
Identifies the current model object as a
TimeDependent
object, and creates all the necessary methods.
Instance Method Details
#acts_as_time_dependent(*args) ⇒ Object
Identifies the current model object as a TimeDependent
object, and creates all the necessary methods.
Accepts options in a hash, all of which are optional:
-
id
– Alternative name for theid
column -
valid_from
– Alternative name for thevalid_from
column -
valid_until
– Alternative name for thevalid_until
column -
replaced_by_id
– Alternative name for thereplaced_by_id
column -
value
– Alternative name for thevalue
column -
is_default
– Alternative name for theis_default
column
Example:
class CommissionRate < ActiveRecord::Base
acts_as_time_dependent :value => :rate
belongs_to :referral_program
named_scope :for_referral_program, lambda { |p| { :conditions => { :referral_program_id => p.id } } }
end
reseller_program = ReferralProgram.find(1)
current_commission = CommissionRate.for_referral_program(reseller_program).default_record_now
puts "Earn #{current_commission.rate} per cent commission as a reseller..."
changes = current_commission.changes_until(1.year.from_now)
for next_commission in changes
= next_commission.nil? ? "Discontinued as of" : "Changing to #{next_commission.rate} per cent on"
puts "#{} #{current_commission.valid_until.strftime('%d %b %Y')}!"
current_commission = next_commission
end
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/invoicing/time_dependent.rb', line 192 def acts_as_time_dependent(*args) Invoicing::ClassInfo.acts_as(Invoicing::TimeDependent, self, args) # Create replaced_by association if it doesn't exist yet replaced_by_id = time_dependent_class_info.method(:replaced_by_id) unless respond_to? :replaced_by belongs_to :replaced_by, :class_name => self, :foreign_key => replaced_by_id end # Create value_at and value_now method aliases value_method = time_dependent_class_info.method(:value).to_s if value_method != 'value' alias_method(value_method + '_at', :value_at) alias_method(value_method + '_now', :value_now) class_eval <<-ALIAS class << self alias_method('default_#{value_method}_at', :default_value_at) alias_method('default_#{value_method}_now', :default_value_now) end ALIAS end end |