after_save in plugin

17 views
Skip to first unread message

Tsirkin Evgeny

unread,
Jan 28, 2012, 6:04:05 PM1/28/12
to Ruby on Rails: Talk
Dear list,
I am trying to override an after_save callback declared in main app
using plugin.
The main model class is:

class WorkHours < ActiveRecord::Base
@after_save_call_back_called=0
after_save :after_save_call_back
def after_save_call_back
logger.debug "after save called"
@after_save_call_back_called=1
end
end

And in my plugin (in the lib directory) :

module WorkHoursPatch
# cattr_accessor
def self.included(base) # :nodoc:
base.logger.debug "inlcuded called"
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
base.after_save.delete_if{
|callback| callback.method == :after_save_call_back
}
base.send(:after_save, :after_save_call_back_patched)
end

module ClassMethods

end

module InstanceMethods
def after_save_call_back_patched
logger.debug "overriden after save called"
@after_save_call_back_called=1
end
# Wraps the association to get the Deliverable subject. Needed for
the
# Query and filtering
# def is_new
# unless self.is_new.nil?
# return self.is_new
# end
# return false;
# end
end
end
#
## Add module to Issue
WorkHours.send(:include, WorkHoursPatch)

The init.rb says
require 'work_hours_patch'

All this should work and running the console I an see the old plugin
being deleted and the new one added.

Please help!
I am spending on this days!
Evgeny

Tim Shaffer

unread,
Jan 30, 2012, 11:28:42 AM1/30/12
to rubyonra...@googlegroups.com
I think your issue may be a mixup with class instance and instance variables.

However, what's the issue you are having with all this? Are you getting any error messages? 


Tsirkin Evgeny

unread,
Jan 31, 2012, 3:05:50 AM1/31/12
to rubyonra...@googlegroups.com
Thanks Tom ,
I have actually figured out the problem.
Seems like since the lib of the plugin is not reloaded on every request 
but the main app is .The plugin override works for first request but the 
original methods are restored the next one.
The solution that worked for me was wrapping our monkey-patch in a callback.

Evgeny


On Mon, Jan 30, 2012 at 6:28 PM, Tim Shaffer <timsh...@me.com> wrote:
I think your issue may be a mixup with class instance and instance variables.

However, what's the issue you are having with all this? Are you getting any error messages? 


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/C-uGWVAUau0J.

To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply all
Reply to author
Forward
0 new messages