Time To Pull The Plug

This is a subtitle. There are many like it, but this one is here.

Bending ActiveAdmin to My Will With a Customized Update Action

| Comments

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:

1
attr_accessor :last_foobar

And then in your ActiveAdmin controller:

1
2
3
4
5
6
7
8
9
10
11
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