Forum: Ruby on Rails question about observer callbacks

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.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-06-01 14:10
(Received via mailing list)
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
This topic is locked and can not be replied to.