Reputation: 11235
I have a search collection action which I want to be accessible and overridable by all of my resource controllers in ActiveAdmin. It's trivial to create controller actions in a single Active Admin resource with the following code:
collection_action :autocomplete_user_last_name, method: :get
def autocomplete_user
term = params[:term]
users = User.where('first_name LIKE ? OR last_name LIKE ? OR email LIKE ?', "%#{term}%", "%#{term}%", "%#{term}%").order(:first_name)
render json: users.map { |user|
{
id: user.id,
label: user.full_name,
value: user.full_name
}
}
end
But how would I create a similar action within the ActiveAdmin base controller? Within Rails, ApplicationController
behaves just like any other controller where we can create routes, actions, helper methods, etc., however, I only want this action to be scoped to Active Admin, not my entire App. Is there an equivalent to ApplicationController in ActiveAdmin?
Looking through the AA code, I found the following base controllers which are the superclasses of all resource controllers:
ActiveAdmin::PageController
ActiveAdmin::BaseController
ActiveAdmin::ResourceController
However, the collection
and member
actions aren't defined within these classes.
Upvotes: 1
Views: 1867
Reputation: 11235
I just ended up scoping the route/controller action to a specific resource. Not sure if what I'm asking is currently possible with AA's API.
Upvotes: 0
Reputation: 393
I believe that all AA resource controllers inherit from `ActiveAdmin::ResourceController'. See documentation here Class: ActiveAdmin::ResourceController
I'm working from memory here, but I would start by wrapping your controller code in active_admin.rb
with the following:
ActiveAdmin::ResourceController.class_eval do
collection_action :autocomplete_user_last_name, method: :get do
def autocomplete_user
term = params[:term]
users = User.where('first_name LIKE ? OR last_name LIKE ? OR email LIKE ?', "%#{term}%", "%#{term}%", "%#{term}%").order(:first_name)
render json: users.map { |user|
{
id: user.id,
label: user.full_name,
value: user.full_name
}
}
end
end
end
Upvotes: 1