How can I add a print method for Audited gem in Rails?

26 views
Skip to first unread message

David McDonald

unread,
May 9, 2015, 11:14:58 AM5/9/15
to rubyonra...@googlegroups.com
I'm using the gem Audited in my Rails app. I want to create a way to print out the Audit record so I can have a feed of what has been changed by a User, or what has changed on an object. Currently, I have setup a method called print_audit on each object that I'm using with Audited. But this seems incredibly wrong. I would like to override the to_s method on the Audit model... but I'm unsure what the correct way of doing that would be... is the approach incorrect and if not how would I go about doing this?

Hassan Schroeder

unread,
May 9, 2015, 12:42:09 PM5/9/15
to rubyonrails-talk
On Sat, May 9, 2015 at 7:57 AM, David McDonald <dave...@gmail.com> wrote:
> I'm using the gem Audited in my Rails app. I want to create a way to print
> out the Audit record so I can have a feed of what has been changed by a
> User, or what has changed on an object. Currently, I have setup a method
> called print_audit on each object that I'm using with Audited. But this
> seems incredibly wrong.

Trust your instincts :-)

I'd consider the Single Responsibility Principal and write something
entirely separate, e.g. an AuditReport class and whatever supporting
classes/modules that requires. (TDDing will tell you what they are.)

FWIW,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

David McDonald

unread,
May 9, 2015, 1:09:01 PM5/9/15
to rubyonra...@googlegroups.com
Thanks for the reply. I had not heard of "SRP" and looked it up.  This is the method I think I'm going to use as it seems simple and works out quite well I believe.  If anyone knows of a better way I'm all ears! Thanks again Hassan!

Added file: config/initializers/audited_additional_methods.rb

module Audited
  module Adapters
    module ActiveRecord
      class Audit < ::ActiveRecord::Base
        def to_s
          "Added my new to_s logic here"
        end
      end
    end
  end
end
Reply all
Reply to author
Forward
0 new messages