Active Admin Hidden Features
π Unlocking Hidden Gems in Active Admin for Ruby on Rails π
Active Admin is a powerful tool for building elegant backends for Ruby on Rails applications. While its primary features like resource management and dashboards are well-known, there are some hidden gems and lesser-known features that can supercharge your admin panel! Letβs dive into these unique tricks and features with examples to help you get the most out of Active Admin.
1. Customizing Resource Forms Dynamically π
Active Admin allows you to dynamically customize forms based on conditions. For instance, letβs say you want to show additional fields only for certain roles:
ActiveAdmin.register User do
form do |f|
f.inputs "Basic Details" do
f.input :name
f.input :email
end
if current_user.admin?
f.inputs "Admin Details" do
f.input :admin_notes
end
end
f.actions
end
end
This adds role-specific fields, making your admin panel smarter and more context-aware.
2. Using Custom Pages for Specialized Actions π
Need a custom page for a special report or admin action? Active Admin makes this possible with ease:
ActiveAdmin.register_page "Monthly Report" do
content do
panel "Summary" do
para "This is a custom page for monthly reports."
end
end
end
Access this page at /admin/monthly_report
, and you can add any custom functionality you need!
3. Conditional Scopes for Enhanced Filtering π
Scopes in Active Admin are great for filtering records, but did you know you can make them conditional? Hereβs how:
ActiveAdmin.register Order do
scope :all
scope("High Value Orders") { |orders| orders.where("total_price > ?", 500) }
scope :today do
Time.zone.now.hour > 12 ? Order.where(created_at: Date.today) : Order.none
end
end
This lets you define highly contextual and useful filters for your admin users.
4. Custom Action Items for Better Workflow βοΈ
Action items are those handy buttons in the admin interface. You can add custom actions like this:
ActiveAdmin.register Post do
action_item :publish, only: :show do
link_to "Publish", publish_admin_post_path(post), method: :put if !post.published?
end
member_action :publish, method: :put do
resource.update(published: true)
redirect_to resource_path, notice: "Post published successfully!"
end
end
Your admin panel now has a custom Publish button for unpublished posts, boosting productivity.
5. Interactive Dashboards with Charts and Stats π¨
Active Admin dashboards are great for summaries, but you can make them interactive with charts. Use the chartkick gem:
ActiveAdmin.register_page "Dashboard" do
content do
panel "User Signups" do
line_chart User.group_by_day(:created_at).count
end
end
end
This provides an insightful and visually appealing dashboard for your app.
6. Batch Actions for Bulk Operations π
You can add custom batch actions to handle bulk tasks efficiently:
ActiveAdmin.register User do
batch_action :approve do |ids|
User.where(id: ids).update_all(approved: true)
redirect_to collection_path, notice: "Users approved successfully!"
end
end
This lets your admins perform bulk updates, saving time and effort.
7. Extending Active Admin with Custom Views π¨
Want more control over how data is displayed? Use custom partials:
index as: :block do |user|
div class: "user-block" do
h3 user.name
para user.email
end
end
This renders a completely customized block layout for your admin index page.
8. Integration with Pundit for Granular Authorization βοΈ
While Active Admin comes with built-in authorization, integrating with Pundit gives you more granular control:
ActiveAdmin.register User do
controller do
include Pundit
def scoped_collection
policy_scope(User)
end
end
end
Admins now see only the data theyβre authorized to view, enhancing security.
9. Improving Performance with Paginated Associations π
If youβre dealing with large datasets, loading associated records can slow things down. Use :includes
for eager loading:
ActiveAdmin.register Order do
includes :user, :products
end
This speeds up your admin panel significantly for large associations.
10. Adding Emojis and Styles for Fun π
Make your admin panel lively by using emojis and CSS:
ActiveAdmin.register Post do
index do
column(:status) { |post| post.published? ? "π₯ Published" : "β Draft" }
actions
end
end
A small touch like this makes your admin panel more engaging and user-friendly.
Wrapping Up π
Active Admin is more than just a CRUD generator; itβs a powerful tool that can be tailored to fit any admin need. By leveraging these hidden gems, you can create a more dynamic, efficient, and user-friendly admin interface. Which of these tricks are you excited to try? Share your thoughts in the comments!
Happy coding! π
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.