active admin
ActiveAdmin's functionality is built on top of several 3rd party gems:
- Inherited Resources by Jose Valim (RESTful base controller class) inherited_resources
- Formtastic (DSL for creating forms). Note that the version used by AA is the 1.2 stable branch that works with both Rails 2.x and 3.x. formtastic
- Meta Search for implementing filters meta_search
Glossary
- Action Item - button displayed under the menu. The default action items are New, Edit and Delete.
Key components
- active_admin/dsl.rb - constitutes the AA public API. All the registration blocks are instance eval'd in this class.
- active_admin/resource.rb - abstraction provided by AA for wrapping the
registered models. The instance of the current resource is available in
ResourceController and views by calling the
active_admin_configmethod.
Examples
Actions
Show action item only on certain pages:
# link to geocode promo
action_item :only => [:show, :edit] do
link_to('Geocode', :action => :geocode, :id => resource.id)
end
Define action for individual records:
member_action :geocode, :method => :get do
promo = Promo.find(params[:id])
promo.geocode
promo.save
redirect_to :action => :edit, :notice => 'Promo geocoded'
end
Manually initialize and render an index table inside an .arb file:
config = ActiveAdmin::PageConfig.new(:as => :batch_table) do |instance|
column :id
column :title
column :whatever
end
paginated_collection(collection, :entry_name => active_admin_config.resource_name) do
div :class => 'index_content' do
insert_tag(ActiveAdmin::Views::IndexAsTable, config, collection)
end
end
Alternatively, use the config defined in the register block:
config = active_admin_config.page_configs[:index]
Filters
Add a filter to search by associated model's title (Author hasMany Posts):
ActiveAdmin.register Author do
# for filters like this, :label and :as seem to be mandatory
filter :posts_title, :label => "Author's Posts", :as => :string
end
Internals
ActiveAdmin gem defines an Engine class that extends Rails::Engine and allows the gem to behave like a Rails engine. One of the responsibilities of a Rails engine is to load models, controllers and helpers inside the app directory.
It also defines a set of paths, autoloaded when needed, that contain various classes that comprise ActiveAdmin.
Controllers
All AA controllers inherit from ApplicationController.
Scopes
ActiveAdmin.register_page "Translations" do
content do
# ActiveAdmin::Views::Pages::Page
end
end
ActiveAdmin.register Design do
form do |f|
# ActiveAdmin::Views::Pages::Form
# f is ActiveAdmin::FormBuilder
f.inputs do
# ActiveAdmin::Views::Pages::Form
end
end
end