I’m trying to write an acts_as_ plugin for my rails app, and I need to
override valid? and update_attributes. No matter what I do, I can’t get
it to call my plugin methods, but it will call my method_missing fine.
-from acts_as_option_record.rb
module ActsAsOptionRecord
def self.included(base)
base.extend ClassMethods
base.extend InstanceMethods
end
module ClassMethods
def acts_as_option_record
include ActsAsOptionRecord::InstanceMethods
extend ActsAsOptionRecord::ClassMethods
end
end
module InstanceMethods
def update_attributes (arguments)
debugger
#stuff
end
def valid?
debugger
#more stuff
end
end
end
-from init.rb
ActiveRecord::Base.send(:include, ActsAsOptionRecord)
Never ever override basic methods, if you want to do this, what you
really need is alias_method_chain 
Imagine that you want to do some logging before and after calling the
:update_attributes method, here’s how it would look like:
module ActsAsLoggable
def self.included( base )
base.send( :include, InstanceMethods )
base.alias_method_chain :update_attributes, :acts_as_loggable
end
module InstanceMethods
def update_attributes_with_acts_as_loggable( attributes )
puts "before update attributes"
self.update_attributes_without_acts_as_loggable( attributes
)
puts “after update attributes”
end
end
end
Maurício Linhares
http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr
On Thu, Jun 11, 2009 at 7:05 PM, Helena
Thanks, that’s working great.
I did have to put the alias method in my ClassMethod definition of
acts_as_option_record to get it to load correctly for my use.
module ClassMethods
def acts_as_option_record
include ActsAsOptionRecord::InstanceMethods
extend ActsAsOptionRecord::ClassMethods
alias_method_chain :update_attributes, :option_record
end
end