Bending ActiveAdmin To My Will With a Customized Update Action

We had a situation where we wanted to customize ActiveAdmin’s update action to behave a little differently. We needed to have the update event set an attr_accessor that was necessary for some before_save hooks, and the instructions on the Internet on how to do override the ActiveAdmin actions didn’t cover that case. The solution was painful to figure out and involved a lot diving through ActiveAdmin, InheritedResources, and Formtasic, but the solution turned out to be fairly straightforward.

In this example, you have a Baz that when it’s saved, you want to make sure that it has the current foobar available for the before_save and validation callbacks.

In your model, you have your attr_accessor defined:

  attr_accessor :last_foobar

And then in your ActiveAdmin controller:

controller do
  def update(options={}, &block)
    # This sets the attr_accessor you want later
    params[:baz].merge!({ :last_foobar => current_foobar })
    # This is taken from the active_admin code
    super do |success, failure| 
      block.call(success, failure) if block
      failure.html { render :edit }
    end
  end
end

And whatever you’ve put in that attr_accessor is available for whatever callbacks and validations you might need it for.

 
comments powered by Disqus