Drop-down menu for active admin resource - ruby-on-rails

I'm trying to create a drop down menu in active admin's navigation. The docs:
https://activeadmin.info/2-resource-customization.html#customize-the-menu
says all I need to do is create the menu:
config.namespace :admin do |admin|
admin.build_menu do |menu|
menu.add label: 'example', priority: 0
end
end
then add that menu as the parent of the resource:
menu parent: 'example'
But that doesn't work for me.
The only thing that has thus far is this:
config.namespace :admin do |admin|
admin.build_menu do |menu|
menu.add label: 'Appointment', priority: 0, html_options: { target: :blank } do |item|
item.add label: 'Appointment', url: '/admin/appointments'
item.add label: 'AppointmentCommunication', url: '/admin/appointment_communications'
end
end
end
With menu: false in the actual resource file.
But that's pretty messy, and not well organized. I was hoping to have a simpler way of making the menus. But maybe I'm just missing something.
Note: I did try to name the menu something that wasn't the name of one of the resources but alas no changes. Additionally, I tried moving the two resources - Appointment and AppointmentCommunication - into a folder called Appointment but obviously that didn't work.
Any thoughts on this would be appreciated.

A couple of options:
You can just add the parent menu in the resource and it will work without defining it:
menu parent: 'site'
site will become the dropdown name and resource(s) will be menu items.
You can define the parent menu in the initializers/active_admin.rb:
config.namespace :admin do |admin|
super_admin.site_title = "Active Admin"
admin.build_menu do |menu|
menu.add label: 'Configuration', priority: 0
end
end
One thing to note is, if you go with option #2, you have to restart your server in order to view the changes.
Source: https://activeadmin.info/2-resource-customization.html#customize-the-menu

It should work as in the docs
# app/admin/appointment.rb
menu label: 'Appointment', parent: 'Appointments', priority: 0
# app/admin/appointment_communication.rb
menu label: 'Appointment Communication', parent: 'Appointments', priority: 1

Related

Active Scaffold dynamic action link

Maybe someone know of a way to make a dynamic hide/show filter action link that changes on click without writing any javascript or creating partials, just with controller and helpers.
So instead of having two buttons:
config.action_links.add :index, :label=>"Hide", :parameters => {:cancelled => false}, :position => false
config.action_links.add :index, :label=>"Show", :parameters => {}, :position => false
It would be awesome to have a dynamic button that changes on click from hide - show
The important piece is that when you change links, even by using :dynamic_parameters attribute, they won't get re-rendered unless you specify
config.list.refresh_with_header = true
in your configuration. Once you do that, it's just code.
In my case, I have a list of records with optional attached documents. I want to toggle between showing a list of all the records and just the ones with attachments. I have a boolean parameter :with_attachments that is either '0' or '1'.
The action links look like
config.action_links.add :index, label: 'Show All', parameters: { with_attachments: '0' }, position: false
config.action_links.add :index, label: 'Attachment Only', parameters: { with_attachments: '1' }, position: false
config.list.refresh_with_header = true # Refresh action buttons!
The index method applies the hidden tag to the currently selected option.
def index
current = params[:with_attachments] || '0'
active_scaffold_config.action_links.each do |link|
# For :index actions (filtering the list)
if link.action == :index
# hide the one that would refresh with the current parameter value
link.html_options[:hidden] = (link.parameters[:with_attachments] == current)
end
end
super
end
And then there is CSS that makes hidden actions invisible
.active-scaffold-header .actions a[hidden=hidden] {
display: none;
}
You could probably do it with just one button and :dynamic_parameters but since I have icons attached via CSS (omitted for simplicity) that solution wouldn't work for me.

Active admin In Rails 4

I have Rails 4 app, which I want to manage with Active admin. I have 2 models,
Item
Category
Item has 2 fields, name and category_id. The Category model has a field called name. The models are related with has_many :items and belongs_to :Category.
When I try to access the admin panel in Active Admin, after configuring some aspect in Active Admin, in the model Item I have a desplegable menu with the id reference of the category like this #Category:=0x675654. What I want is to get the name of the category. How can I to do this? I don't have access to edit this view.
How about something like:
ActiveAdmin.register Item do
form do |f|
f.inputs "Item" do
f.input :category, as: :select, collection: Category.all.collect {|c| [c.name, c.id] }
end
end
end
If what you want is to show the name in the index page, you need to customize it with something like:
column 'Name' do |item|
name = Category.find(item.category_id).name
end
To simply display the name :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
item.category.name
end
end
end
If you'd like to get a link to the resource on top of that name, use auto_link :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
auto_link(item.category, item.category.name)
end
end
end
The same thing applies for Item views.

How to set a parent menu priority in ActiveAdmin?

I have a couple of models in my Ruby on Rails application like "Plan", "Tester", "Module", etc. Using activeadmin gem, I would like to have a page for each of these entities and place each under a couple of different menus. So my code looks like the following:
ActiveAdmin.register Plan do
menu parent: 'Planning', priority: 1
ActiveAdmin.register Tester do
menu parent: 'Planning', priority: 2
ActiveAdmin.register Module do
menu parent: 'Bundle', priority: 1
ActiveAdmin.register User do
menu parent: 'Administration', priority: 1
I don't have a page for the top menus ('Planning', 'Bundle', 'Administration'), but I want to see them in a custom order and not the alphabetical order. So, my question is how could I set the priority (order) of the parent menus without having a corresponding page for each of them?
The items, which non model-based starts their priority from 10, so u can put 10+ priority for model-based menus.
If you need to set priorities among non model-based menus, you can build fake file under admin folder like admin/administration.rb with code:
ActiveAdmin.register_page "Administration" do
menu :label => "Administration", :priority => 15, :url => '#'
end
and admin/bundle.rb:
ActiveAdmin.register_page "Bundle" do
menu :label => "Bundle", :priority => 16, :url => '#'
end
so on
See 'Customizing Parent Menu Items' in the documentation.
# config/initializers/active_admin.rb
config.namespace :admin do |admin|
admin.build_menu do |menu|
menu.add label: 'Blog', priority: 0
end
end
# app/admin/post.rb
ActiveAdmin.register Post do
menu parent: 'Blog'
end

Translating activerecord collection for a dropdown

In a model I have some states
STATES = ["in_progress", "active", "archived"]
In my form I want a drop down/select with
In Progress, Active, Archived for english
and
ชำระ , ขัน , ยื่น in Thai
What is the best way to handle this ?
One option I have thought of is the following
def self.states
#states = {}
STATES.each do |s|
#states[s] = I18n.t(s)
end
#states
end
Is there a better way?
This is supported by simple_form:
In your view, you should just use
<%= f.input :state, collection: ["in_progress", "active", "archived"] %>
In your yaml file, you should have
simple_form:
options:
defaults:
state:
in_progress: In progress
active: Active
archived: Archived

Active Admin Navigation Links

How do you add your own custom links dynamically to the ActiveAdmin global navigation header other than registering the pages/models? For example, if I want a link that can direct users to my home page for instance.
Seems like ActiveAdmin made it somewhat easier. I upgraded to version 0.6.2 and here is what you can do to add custom links anywhere in your navigation (the example below will add one custom menu item, and one custom dropdown):
In # config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
config.namespace :admin do |admin|
admin.build_menu do |menu|
menu.add :label => "My Custom Link", :url => "/", :priority => 0
menu.add :label => "Pages" do |pages|
pages.add :label => "Homepage", :url => "/admin/homepage"
pages.add :label => "About Us", :url => "/admin/about-us"
pages.add :label => "Facebook", :url => "http://www.facebook.com", :html_options => { :target => "_blank" }
end
end
end
end
In case you already registered models with "Pages" as a parent menu (ie: in your app/admin/ folder, a model with menu :priority => 2, parent: 'Pages') it will keep those as well automatically!
I managed to to this by adding the ActiveAdmin::MenuItem to the current AdminAdmin controller. For example,
ActiveAdmin.register User, :name_space => :example_namespace do
controller do
private
def current_menu
item = ActiveAdmin::MenuItem.new :label => "Link Name", :url => 'http://google.com'
ActiveAdmin.application.namespaces[:example_namespace].menu.add(item)
ActiveAdmin.application.namespaces[:example_namespace].menu
end
end
end
I basically created a new ActiveAdmin::MenuItem and add it to the current ActiveAdmin menu with the namespace example_namespace and return the menu in the end of the current_menu method. Note: current_menu is a method expected by ActiveAdmin so don't change the name of it. You can add as many items you like and each of these items will be converted to a link on your navigation header. Note this works for ActiveAdmin version > 0.4.3 so you might need to do your own digging if you want to do it for version <= 0.4.3.
ActiveAdmin.register AdminPage do
menu :url => proc{ "#{AppConfig.url}/checkins/#{current_admin_user.try(:id)}" }
end
Here, you can use any of your db field values in the URL parameter to construct your own URL.
You can configure the site title in your active admin initializer. For example:
config.site_title_link = "/"
This will give you a link to your rootpage.

Resources