Hi there.
I have a plugin module that extends the AR with a before_save
callback to log all changes made into a relating acts_as_commentable
comment. This works fine.
However, I want to add more details to the comment such as who made the change etc. I have made a couple of fields available to the model instance log_message
and log_owner
. The problem seems to be that my local variable options
doesn't get inherited through the class_eval
statement.
This is my code:
options = { :add_to_accessible => true, :owner_model => "User" }
attr_accessor :log_message, :log_owner, :log_options
attr_accessible :log_message, :log_owner, :log_options if options[:add_to_accessible] # This inherits the options fine.
# Before save goes here...
class_eval do
def log_changes_to_comments
self.changes.each do |attr, values|
# Do something with comment here...
comment << options[:owner_model].classify.constantize.find(self.log_owner).name if self.log_owner
# Do more with comment here...
end
end
end
Now, specifically the problem arises in: comment << options[:owner_model].classify.constantize
which raises an error saying that the options
variable doesn't exist.
How can I make sure that this variable is inherited?