I want to include common auditing functionality in my models. This involves storing the changes made to a model in a seperate table : I have created the functions in an Audit module : module Audit before_update :before_changes after_update :after_changes def before_changes old=self.class.find(id).attributes @changes=[] attributes.each do |key, val| if val != old[key] change = Change.new(:primary_key_id=>id, :table=>self.class.table_name, :attribute=>self.class.human_attribute_name(key), :old=>old[key], :new=>val) @changes << change end end end def after_changes if(@changes.size>0) @changes.each {|change| change.save} end end end I now want to include it like this in my model class : class Customer < ActiveRecord::Base include Audit ... end But i get the error : "undefined method ''before_update'' for Audit::Module" I can change it like this so it works fine: class Customer < ActiveRecord::Base include Audit before_update :before_changes after_update :after_changes .... end But this is not an ideal solution. I''d rather only include the one include. Any ideas??? Thanks, Chris -- Posted via http://www.ruby-forum.com/.
I also want to include "has_many :changes" in each model that uses the mixin. I cannot include "has_many :changes" in the module. -- Posted via http://www.ruby-forum.com/.
Rick Olson
2006-Feb-22 06:35 UTC
[Rails] Re: Auditing mixin for model classes. Small problem
On 2/22/06, Chris <evilgeenius@gmail.com> wrote:> I also want to include > > "has_many :changes" > > in each model that uses the mixin. I cannot include "has_many :changes" > in the module.I''d highly suggest looking into the various plugins around. But this is probably what you''re looking for. module Audit def self.included(base) base.before_save ... base.has_many :changes end end http://ruby-doc.org/core/classes/Module.html#M000743 -- Rick Olson http://techno-weenie.net
Andy
2006-Feb-22 06:47 UTC
[Rails] Re: Re: Auditing mixin for model classes. Small problem
Thanks. YOu''re the man. Have you seen a plugin anywhere that does this? -- Posted via http://www.ruby-forum.com/.
Theodore Mills
2006-Feb-23 15:43 UTC
[Rails] Re: Re: Auditing mixin for model classes. Small problem
The acts_as_versioned plugin is pretty close I imagine. On 2/22/06, Andy <easy@gmail.com> wrote:> Thanks. YOu''re the man. > > Have you seen a plugin anywhere that does this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Rick Olson
2006-Feb-23 15:49 UTC
[Rails] Re: Re: Auditing mixin for model classes. Small problem
On 2/23/06, Theodore Mills <twmills@gmail.com> wrote:> The acts_as_versioned plugin is pretty close I imagine.It looks like you''re using one table to audit all models. Acts as Versioned uses a separate table for each model. The nice thing is that each versioned record can also act like the actual method... -- Rick Olson http://techno-weenie.net