module ActiveRecord::Userstamp::ControllerAdditions
The ControllerAdditions
module, when included into a controller, adds an before_action
callback (named set_stamper
) and an after_action
callback (named reset_stamper
). These methods assume a couple of things, but can be re-implemented in your controller to better suit your application.
See the documentation for ‘set_stamper` and `reset_stamper` for specific implementation details.
Private Instance Methods
The {#reset_stamper} method as implemented here assumes that a User
model is being used as the stamper. If this is not the case then you will need to manually add your own implementation of this method to the private section of your ApplicationController
# File lib/active_record/userstamp/controller_additions.rb, line 40 def reset_stamper ActiveRecord::Userstamp.config.default_stamper_class.stamper = @_userstamp_stamper @_userstamp_stamper = nil end
The {#set_stamper} method as implemented here assumes a couple of things. First, that you are using a User
model as the stamper and second that your controller has a current_user
method that contains the currently logged in stamper. If either of these are not the case in your application you will want to manually add your own implementation of this method to the private section of your ApplicationController
# File lib/active_record/userstamp/controller_additions.rb, line 32 def set_stamper @_userstamp_stamper = ActiveRecord::Userstamp.config.default_stamper_class.stamper ActiveRecord::Userstamp.config.default_stamper_class.stamper = current_user end
This {#with_stamper} method sets the stamper for the duration of the action. This ensures that exceptions raised within the controller action would properly restore the previous stamper.
TODO: Remove set_stamper/reset_stamper
# File lib/active_record/userstamp/controller_additions.rb, line 20 def with_stamper set_stamper yield ensure reset_stamper end