Rails 2.2.2
Ruby 1.8.5
I am attempting to extend AR with a generalized audit attribute setter.
I have written this code, which does not work for reasons unknown to me.
Perhaps someone here can explain why it does not.
# Put this in config/initializer as active_record_addins.rb
module ActiveRecord
module HLLAuditStamps
def self.included(base)
super
# alias_method_chain :create, :audit
# is the same as:
# alias_method :create_without_audit, :create
# alias_method :create, :create_with_audit
base.alias_method_chain :create, :audit
base.alias_method_chain :update, :audit
end
private
def create_with_audit
write_audit_fields
# Now we call our alias to the original method
create_without_audit
end
def normal_time_now
if base.class.default_timezone == :utc
return DateTime.now.utc
else
return DateTime.now
end
end
def update_with_audit
write_audit_fields
update_without_audit
end
def write_audit_fields
write_attribute('accessed_at', normal_time_now) if \
respond_to?('accessed_at')
write_attribute('accessed_on', normal_time_now) if \
respond_to?('accessed_on')
end
end
end
What I intend is that whenever the AR create or update method is called
then the attributes specified in write_audit_fields are set to the value
provided by normal_time_now. They are not and I cannot tell why.
Any suggestions as to what I am doing wrong?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---