Activeadmin Cheat Sheet

From WikiOD

Here is the cheatsheat of Activeadmin and basic guide on how to use Activeadmin

Listing scopes[edit | edit source]

Allows you to filter listings by a certain scope. {: .-setup}

scope :draft
scope :for_approval
scope :public, if: ->{ current_admin_user.can?(...) }
scope "Unapproved", :pending
scope("Published") { |books| books.where(:published: true) }

Sidebar filters[edit | edit source]

filter :email
filter :username

Custom actions[edit | edit source]

You can define custom actions for models. {: .-setup}

before_filter only: [:show, :edit, :publish] do
  @post = Post.find(params[:id])
end

Make the route[edit | edit source]

member_action :publish, method: :put do
  @post.publish!
  redirect_to admin_posts_path, notice: "The post '#{@post}' has been published!"
end

Link it in the index[edit | edit source]

index do
  column do |post|
    link_to 'Publish', publish_admin_post_path(post), method: :put
  end
end

And link it in show/edit[edit | edit source]

action_item only: [:edit, :show] do
  @post = Post.find(params[:id])
  link_to 'Publish', publish_admin_post_path(post), method: :put
end

Columns[edit | edit source]

column :foo
column :title, sortable: :name do |post|
  strong post.title
end

Other helpers[edit | edit source]

status_tag "Done"           # Gray
status_tag "Finished", :ok  # Green
status_tag "You", :warn     # Orange
status_tag "Failed", :error # Red

Disabling ‘new post’[edit | edit source]

ActiveAdmin.register Post do
  actions :index, :edit
  # or: config.clear_action_items!
end

Credit:rstacruz