Question about observer callbacks


#1

I’m not sure how to implement this so I’ll describe what I currently
have
done then what I wish to have happen

currently, I have a generic observer for several models

class AuditObserver < ActiveRecord::Observer
observe Foo, Bar

def after_update(model)
model.log(“UPDATED " + Time.now.strftime(”%m-%d-%Y %H:%M"))
end

def after_create(model)
model.log(“CREATED " + Time.now.strftime(”%m-%d-%Y %H:%M"))
end

def after_destroy(model)
model.log(“DESTROYED " + Time.now.strftime(”%m-%d-%Y %H:%M"))
end
end

and in each model I have a log method to log certain information to a
corresponding log table in the database

class Foo < ActiveRecord::Base
has_many :logs, :class_name => “FooLog”

def log(msg)
fl = FooLog.new
fl.message = msg
logs << fl
end
end

now, what i would like to do is customize the message beyond 'CREATED",
‘UPDATED’ or 'DESTROYED". I’d like my messages to be more robust beyond
the
generic created, updated or deleted message but I am not really sure how
to
accomplish this . for example, say a particular attribute of a Foo
record
gets updated, I’d like to be able to say “Foo record 456, status changed
to
CLOSED” when the status attributed is updated.

class FooController < ApplicationController

def change_status
@foo = Foo.find(params[:id])
@foo.update_attribute(:status, params[:status])
# observer’s after_update method will get called after the above
update,
# but how to send a specific message about the status change???
# i could add a log message directly, but i would have to do that
everywhere
# I update the model, which makes using an observer kind of
pointless…
# @foo.log(“status updated to #{params[:status]}”
end

def change_assignment
@foo = Foo.find(params[:id])
@foo.update_attribute(:user_id, User.find(session[:user])
# same thing here, how to specify a customized message for the
update???
end
end

any help is appreciated

Chris