class ActiveAdmin::ResourceDSL
This is the class where all the register blocks are evaluated.
Private Instance Methods
Member Actions give you the functionality of defining both the action and the route directly from your ActiveAdmin
registration block.
For example:
ActiveAdmin.register Post do member_action :comments do @post = Post.find(params[:id] @comments = @post.comments end end
Will create a new controller action comments and will hook it up to the named route (comments_admin_post_path) /admin/posts/:id/comments
You can treat everything within the block as a standard Rails controller action.
# File lib/active_admin/resource_dsl.rb, line 133 def action(set, name, options = {}, &block) warn "Warning: method `#{name}` already defined" if controller.method_defined?(name) set << ControllerAction.new(name, options) title = options.delete(:title) controller do before_action(only: [name]) { @page_title = title } if title define_method(name, &block || Proc.new{}) end end
# File lib/active_admin/resource_dsl.rb, line 26 def belongs_to(target, options = {}) config.belongs_to(target, options) end
# File lib/active_admin/resource_dsl.rb, line 149 def collection_action(name, options = {}, &block) action config.collection_actions, name, options, &block end
Configure the CSV format
For example:
csv do column :name column("Author") { |post| post.author.full_name } end csv col_sep: ";", force_quotes: true do column :name end
# File lib/active_admin/resource_dsl.rb, line 108 def csv(options = {}, &block) options[:resource] = config config.csv_builder = CSVBuilder.new(options, &block) end
# File lib/active_admin/resource_dsl.rb, line 153 def decorate_with(decorator_class) # Force storage as a string. This will help us with reloading issues. # Assuming decorator_class.to_s will return the name of the class allows # us to handle a string or a class. config.decorator_class_name = "::#{ decorator_class }" end
# File lib/active_admin/resource_dsl.rb, line 91 def form(options = {}, &block) config.set_page_presenter :form, ActiveAdmin::PagePresenter.new(options, &block) end
Store relations that should be included
# File lib/active_admin/resource_dsl.rb, line 41 def includes(*args) config.includes.push *args end
Configure the index page for the resource
# File lib/active_admin/resource_dsl.rb, line 81 def index(options = {}, &block) options[:as] ||= :table config.set_page_presenter :index, ActiveAdmin::PagePresenter.new(options, &block) end
# File lib/active_admin/resource_dsl.rb, line 145 def member_action(name, options = {}, &block) action config.member_actions, name, options, &block end
Redefine sort behaviour for column
For example:
# nulls last order_by(:age) do |order_clause| [order_clause.to_sql, 'NULLS LAST'].join(' ') if order_clause.order == 'desc' end # by last_name but in the case that there is no last name, by first_name. order_by(:full_name) do |order_clause| ['COALESCE(NULLIF(last_name, ''), first_name), first_name', order_clause.order].join(' ') end
# File lib/active_admin/resource_dsl.rb, line 22 def order_by(column, &block) config.ordering[column] = block end
Keys included in the `permitted_params` setting are automatically whitelisted.
Either
permit_params :title, :author, :body, tags: []
Or
permit_params do defaults = [:title, :body] if current_user.admin? defaults + [:author] else defaults end end
# File lib/active_admin/resource_dsl.rb, line 63 def permit_params(*args, &block) param_key = config.param_key.to_sym belongs_to_param = config.belongs_to_param create_another_param = :create_another if config.create_another controller do define_method :permitted_params do permitted_params = active_admin_namespace.permitted_params + Array.wrap(belongs_to_param) + Array.wrap(create_another_param) params.permit(*permitted_params, param_key => block ? instance_exec(&block) : args) end end end
Create a scope
# File lib/active_admin/resource_dsl.rb, line 36 def scope(*args, &block) config.scope(*args, &block) end
Scope
collection to a relation
# File lib/active_admin/resource_dsl.rb, line 31 def scope_to(*args, &block) config.scope_to(*args, &block) end
Configure the show page for the resource
# File lib/active_admin/resource_dsl.rb, line 87 def show(options = {}, &block) config.set_page_presenter :show, ActiveAdmin::PagePresenter.new(options, &block) end