Active Admin 3.2.0 — Cool Features

How To Use Active Admin 3.2 — Part XIII— Exploring Advanced Features— RailsSeries#Episode 15

J3
Jungletronics

--

Hello! In this episode, let’s explore some of the advanced features available in Active Admin. While this is just scratching the surface, it’s a great starting point for diving deeper into its capabilities.

Please review the Published button. When clicked, it should toggle to Unpublished and save the publication time.
We integrate both published and unpublished scope components. Please review the following two files: post models, and admin model, utilizing action_item in conjunction with member_action.

For further details, refer to the official documentation:

https://activeadmin.info/documentation.html

0#step — Download the last version (v2) post here and prepare your vscode environment.

Let’s open a new Feature Branch: git checkout -b explore_active_admin.

1#step —Goto:

app/admin/posts.rb

scope :all
scope :published
scope :unpublished

2#step — GoTo:

app/models/posts.rb

scope :published, -> { where.not(published_at: nil) }
scope :unpublished, -> { where(published_at: nil) }

3#step — GoTo:

app/admin/posts.rb

  # form do |f|
# f.inputs do
# f.input :user
# f.input :title
# f.input :body
# end
# f.actions
# end

form do |f|
inputs 'Details' do
input :user
input :title
input :body
end
f.actions
end

4#step — GoTo:

app/admin/posts.rb

action_item :publish, only: :show do
link_to 'Publish', publish_admin_post_path(post), method: :put unless post.published_at?
end

action_item :unpublish, only: :show do
link_to 'Unpublish', unpublish_admin_post_path(post), method: :put if post.published_at?
end

member_action :publish, method: :put do
post = Post.find(params[:id])
post.update(published_at: Time.zone.now)
redirect_to admin_post_path(post)
end

member_action :unpublish, method: :put do
post = Post.find(params[:id])
post.update(published_at: nil)
redirect_to admin_post_path(post)
end

5#Step — GoTo:

db/seeds.rb

if Rails.env.development?

AdminUser.create!(email: 'admin@example.com', password: 'password', password_confirmation: 'password')

User.create(email: 'test1@example.com', name: 'test1', password_digest: 'password', password_confirmation: 'password')
User.create(email: 'test2@example.com', name: 'test2', password_digest: 'password', password_confirmation: 'password')

3.times do |i|
Post.create(title: "Title #{i}", body: "Body #{i} words goes here idk...", user_id: User.first.id)
Post.create(title: "Title #{i}", body: "Body #{i} words goes here idk...", user_id: User.second.id)
end

end

6#Step — Run on Terminal:

rails db:drop
railsdb:migrate
rails db:seed
rails s

7#Step —GoTo:

config/locales/en.yml

en:
active_admin:
dashboard: "Welcome to my Active Admin Tutorial !"
call_for_action: "Here you will learn how to use Active Admin version 3.2.0. Please visit the official documentation for more information: https://activeadmin.info/documentation.html"

8#Step — Final Step:

app/admin/dashboard.rb

# frozen_string_literal: true

ActiveAdmin.register_page 'Dashboard' do
menu priority: 1, label: proc { 'Dashboard' }

content title: proc { I18n.t('active_admin.dashboard') } do
div class: 'blank_slate_container', id: 'dashboard_default_message' do
span class: 'blank_slate' do
small I18n.t('active_admin.call_for_action')
end
end
end # content
end
Customizing the Dashboard involves adjusting the locale/en.yml file and dashboard.rb.
We’ve created two users: ‘test1’ and ‘test2’. Take a look at their IDs.
Scope can be created by modifying admin models and model models, utilizing action_item in combination with member_action

That’s all, folks!

GitHub

Bye!

Credits & References

Rails Admin Interfaces with ActiveAdmin by Chris Oliver (GoRails)

git tag -a v3 -m "ActiveAdmin 3.2.0 :  Go to  https://medium.com/jungletronics/navbar-4-active-admin-3-2-0-d9a1d74cd1ab" -m "0- Define scopes in the admin file;" -m "1- Customize form details for better control in the admin file;" -m "2- Implement action_item and member_action to execute Ruby code;" -m "3- Generate a new seed file;" -m "4- Perform database drop, migration, and seeding;" -m "5- Customize en.yml and dashboard.rb to personalize the admin dashboard; You're all set!" -m "Thank you for downloading this project 😍️" 

git push origin v3


git add -A
git commit -m ":lipstick: Active Admin v3: Explore advanced features"
git push --set-upstream origin explore_active_admin

[GoTo your GIT REPO and Merge the Request]

[Returns to vscode]

git checkout main
git status
git fetch
git merge explore_active_admin
git pull

Related Posts:

01# Quick Start — Part I — Active Admin: Get started with a very simple rails 7 frame;

02# Navbar Creation — Part II — Active Admin: Create a Navbar & User Functionalities in Dropdown;

03# Features — Part III — Active Admin: Implements some Cool advanced features.

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!