Skip to content

Content rendering API

Igor Zubkov edited this page Jun 25, 2015 · 13 revisions

Goal of this page - give you same overview of configuration commands and options available in ActiveAdmin. Some kind of DSL API Reference.

Here I will try to show you all power of ActiveAdmin.

OOP access

You can access your ActiveAdmin resources and pages as objects too.

ActiveAdmin.register Bank do
  controller do
    p self # => Admin::BanksController
  end
end
>> puts Admin::BanksController.ancestors[0..12]
Admin::BanksController
ActiveAdmin::ResourceController
ActiveAdmin::BatchActions::Controller
ActiveAdmin::ResourceController::Sidebars
ActiveAdmin::ResourceController::Scoping
ActiveAdmin::ResourceController::DataAccess
ActiveAdmin::ScopeChain
ActiveAdmin::Callbacks
ActiveAdmin::ResourceController::Decorators
ActiveAdmin::ResourceController::ActionBuilder
ActiveAdmin::BaseController
ActiveAdmin::BaseController::Authorization
MethodOrProcHelper

Pages as well:

ActiveAdmin.register_page "Dashboard" do

end

Becomes: Admin::DashboardController inherited from ActiveAdmin::PageController

How to extend?

For pages you can extend ActiveAdmin::PageController and for resource controllers -- ActiveAdmin::ResourceController.


DSLs

There is several types of DSLs in active admin.

ActiveAdmin.register Bank do
  p self.class # => ActiveAdmin::ResourceDSL
end

ActiveAdmin::ResourceDSL contains also

  • ActiveAdmin::Filters::DSL
  • ActiveAdmin::DSL

ActiveAdmin::DSL

action_item(options = {}, &block)

Adds a new action link to the resource (top bar). Available options: :only, :except, :if. For :if option you can pass block/lambda or string/symbol (name of method). :except and :only can symbol or array of symbols.

action_item :only => :show, :if => :not_approved? do
  link_to('Approve', [:approve, :admin, resource])
end

Block passed to action_item will be executed once for page. If it "show" page - you can use resource variable. If you want to make custom controller action, make sure this route defined. You can use member_action and collection_action to define custom controller actions. Block executing inside ActiveAdmin::Views::ActionItems instance.

To disable several actions use:

actions :all, :except => [:new, :destroy]

RDoc


config

The instance of ActiveAdmin::Resource that's being registered currently. You can use this within your registration blocks to modify options:

ActiveAdmin.register Post do
  config.sort_order = "id_desc"

  config.belongs_to(target, options = {})
  config.belongs_to?
  config.belongs_to_config

  config.clear_collection_actions!
  config.clear_member_actions!
  config.decorator_class
  config.defined_actions
  config.resource_class_name
  config.namespace
  config.member_actions
  config.breadcrumb
  config.collection_actions
  config.csv_builder
  config.decorator_class_name
  config.dsl

  # from ActiveAdmin::BatchActions::ResourceExtension
  config.add_batch_action
  config.batch_actions
  config.clear_batch_actions!
  config.add_default_batch_action
  config.batch_actions=
  config.batch_action_path
  config.batch_actions_enabled?
  config.remove_batch_action 

  # from ActiveAdmin::Comments::ResourceHelper
  config.comments?

  # from ActiveAdmin::Filters::ResourceExtension
  config.add_filter
  config.filters_enabled?
  config.add_filters_sidebar_section
  config.filters_sidebar_section
  config.default_association_filters
  config.default_content_filters
  config.preserve_default_filters!
  config.default_filters
  config.preserve_default_filters?
  config.filter_lookup
  config.remove_filter
  config.filters
  config.reset_filters!
  config.filters=

  # from ActiveAdmin::Resource::Routes
  config.route_collection_path
  config.route_instance_path
  config.route_prefix
  config.route_uncountable?

  # from ActiveAdmin::Resource::Sidebars
  config.clear_sidebar_sections!
  config.sidebar_sections?
  config.sidebar_sections
  config.sidebar_sections_for

  # from ActiveAdmin::Resource::ScopeTo
  config.scope_to
  config.scope_to_association_method
  config.scope_to_method
  config.scope_to?
  config.scope_to_config

  # from ActiveAdmin::Resource::Scopes
  config.default_scope
  config.get_scope_by_id
  config.scope
  config.scopes

  # from ActiveAdmin::Resource::Pagination
  config.paginate
  config.paginate=
  config.per_page
  config.per_page=

  # from ActiveAdmin::Resource::PagePresenters
  config.default_index_class
  config.get_page_presenter
  config.set_index_presenter
  config.find_index_class
  config.page_presenters
  config.set_page_presenter

  # from ActiveAdmin::Resource::Naming
  config.plural_resource_label
  config.resource_label
  config.resource_name

  # from ActiveAdmin::Resource::Menu
  config.add_to_menu
  config.menu_item
  config.navigation_menu
  config.default_menu_options
  config.menu_item_options
  config.navigation_menu_name
  config.include_in_menu?
  config.menu_item_options=
  config.navigation_menu_name=

  # from ActiveAdmin::Resource::Controllers
  config.controller
  config.controller_name

  # from ActiveAdmin::Resource::ActionItems
  config.action_items
  config.action_items_for
  config.add_default_action_items
  config.action_items?
  config.add_action_item
  config.clear_action_items!

end

include(mod)

Include a module with this resource. The modules's included method is called with the instance of the ActiveAdmin::DSL passed into it.

eg:

module HelpSidebar

  def self.included(dsl)
    dsl.sidebar "Help" do
      "Call us for Help"
    end
  end

end

ActiveAdmin.register Post do
  include HelpSidebar
end

RDoc


navigation_menu(menu_name = nil, &block)

Set the name of the navigation menu to display. This is mainly used in conjuction with the #belongs_to functionality.

Pass a block returning the name of a menu you want rendered for the request, being executed in the context of the controller

Parameters: menu_name (Symbol) (defaults to: nil) — The name of the menu to display as the global navigation when viewing this resource. Defaults to a menu named :default.

ActiveAdmin.register Ticket do
  belongs_to :project
  navigation_menu :project
end

ActiveAdmin.register Milestone do
  belongs_to :project
  navigation_menu :project
end

RDoc

TODO: describe difference with menu :parent


batch_action(title, options = {}, &block)

Main article about batch actions.

Add a new batch action item to the resource. By default batch_actions are enabled, and you need to make selectable_column in index page. To disable batch actions, put config.batch_actions = false.

Available options: :if, :sort_order, :confirm, also can pass false to remove action.

batch_action :flag do |selection|
  Post.find(selection).each { |p| p.flag! }
  redirect_to collection_path, :notice => "Posts flagged!"
end

batch_action :destroy, false

index do
  selectable_column
  # ..
end

RDoc


controller

run_registration_block

breadcrumb

decorate_with

menu

sidebar(name, options = {}, &block)

Add extra block (panel) to sidebar area. It will be shown on all pages by default. On index page it will appear below filters section. Reflect to class ActiveAdmin::SidebarSection. For title can be localised, see source code below.

def title
  I18n.t("active_admin.sidebars.#{name.to_s}", :default => name.to_s.titlecase)
end

Available options: :partial, :icon, :only, :except, :if.

RDoc


ActiveAdmin::Filters::DSL

filter

preserve_default_filters!

remove_filter

ActiveAdmin::ResourceDSL

  • action
  • after_update
  • before_update
  • permit_params
  • actions
  • around_filter
  • belongs_to
  • scope
  • after_build
  • before_build
  • collection_action
  • scope_to
  • after_create
  • before_create
  • csv
  • show
  • after_destroy
  • before_destroy
  • form
  • skip_after_filter
  • after_filter
  • before_filter
  • index
  • skip_before_filter
  • after_save
  • before_save
  • member_action
  • skip_filter

Rendering DSLs

When we do

ActiveAdmin.register Bank do
  show do
    # here is a differnet
    self.class # => ActiveAdmin::Views::IndexAsTable
  end
end

ActiveAdmin::Views::IndexAsTable

Includes:

  • ActiveAdmin::Component
  • Arbre::Component
  • Arbre::HTML::Div
  • Arbre::HTML::Tag
  • Arbre::Element
  • Arbre::Rails::Rendering
  • Arbre::Element::BuilderMethods

ActiveAdmin::Views::Pages::Show

  • ActiveAdmin::Comments::ShowPageHelper
  • ActiveAdmin::Views::Pages::Show::DefaultMainContent
  • ActiveAdmin::Views::Pages::Base
  • Arbre::HTML::Document
  • Arbre::HTML::Tag
  • Arbre::Element
  • Arbre::Rails::Rendering
  • Arbre::Element::BuilderMethods
Clone this wiki locally