Skip to content

Instantly share code, notes, and snippets.

@codespore
Last active August 30, 2022 14:17
Show Gist options
  • Save codespore/5366994 to your computer and use it in GitHub Desktop.
Save codespore/5366994 to your computer and use it in GitHub Desktop.
Quick reference to useful ActiveAdmin customizations
# Add Action button on the top navigation menu. Useful for resource with nested resources
action_item :only => :show do
link_to('Show Users', admin_group_users_path(resource))
end
# Custom show page with children items
show do
attributes_table *default_attribute_table_rows # Shows the default attributes listing by AA
panel "Users" do
table_for resource.users do
column :first_name
column :last_name
column '' do |user|
links = ''.html_safe
if controller.action_methods.include?('show')
links << link_to(I18n.t('active_admin.view'), admin_user_path(user), :class => "member_link view_link")
end
if controller.action_methods.include?('edit')
links << link_to(I18n.t('active_admin.edit'), edit_admin_user_path(user), :class => "member_link edit_link")
end
if controller.action_methods.include?('destroy')
links << link_to(I18n.t('active_admin.delete'), admin_user_path(user), :method => :delete, :data => {:confirm => I18n.t('active_admin.delete_confirmation')}, :class => "member_link delete_link")
end
links
end
end
end
active_admin_comments # Shows the default comments panel by AA
end
# Pagination on table_for inside sections and panels
panel :test do
paginated_collection(my_collection.page(params[:page]).per(15), download_links: false) do
table_for(collection, sortable: false) do
column :start
column :stop
column :duration
end
end
end
# Conditional page action methods
ActiveAdmin.register Foo do
actions :all, :except => [:new]
controller do
def action_methods # Rails ActionController::Base
if current_admin_user.role?(AdminUser::ADMIN_ROLE)
super
else
super - ['edit', 'destroy']
end
end
end
...
end
# To support multiple belongs_to, work around
# To summarize, if you need to implement polymorphic belongs_to for now, the work around is to add:
# config/routes.rb
namespace :admin do
resources :users do
resources :group_users
end
resources :groups do
resources :group_users
end
end
# app/admin/images.rb
ActiveAdmin.register GroupUser do
menu false
controller.belongs_to :user, :group, polymorphic: true
end
# ActiveAdmin uses Kaminari for pagination and if you use will_paginate in your app,
# you need to configure an initializer for Kaminari to avoid conflicts. Put this in config/initializers/kaminari.rb
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
end
# OR - put this in the same initializer if that doesn't work
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
def per(value = nil) per_page(value) end
def total_count() count end
end
end
module CollectionMethods
alias_method :num_pages, :total_pages
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment