Forum: Ruby on Rails Auditing mixin for model classes. Small problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-02-22 07:03
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
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-02-22 07:15
I also want to include

"has_many :changes"

in each model that uses the mixin. I cannot include "has_many :changes"
in the module.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-22 07:35
(Received via mailing list)
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
0f9185fe8843835a97511344077dd43e?d=identicon&s=25 Andy (Guest)
on 2006-02-22 07:47
Thanks. YOu're the man.

Have you seen a plugin anywhere that does this?
90bedc2d9234191f2b35dcb9fbbc4390?d=identicon&s=25 Theodore Mills (Guest)
on 2006-02-23 16:44
(Received via mailing list)
The acts_as_versioned plugin is pretty close I imagine.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-23 16:50
(Received via mailing list)
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
This topic is locked and can not be replied to.