How to set a parent menu priority in ActiveAdmin? - ruby-on-rails

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

Related

Drop-down menu for active admin resource

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

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.

ActiveAdmin status_tag not rendering inside table

I'm working on my first activeadmin project and I'm trying to create a panel on the dashboard that displays a status for each of my estimates.
The status_tag is displaying outside the table though. If i change it to just some text value rather than the status_tag it works just fine.
Why is the status_tag way up top and how can I fix it?
Example screenshot
http://snag.gy/iHYz2.jpg
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Recent Estimates" do
table_for Estimate.last(5) do
column :description
column :name
column :status, status_tag('In Progress')
end
end
end
end
end # content
end
I figured it out. It seems that instead of this:
column :status, status_tag('In Progress')
I needed to do:
column(:status) {|estimate| status_tag(estimate.status)}

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.

Rails Beginner - How to define 3 buttons enabling a user to make a choice?

I am aware this is a very basic question, we are very new to rails and have been unable to find a specific answer to this question.
Background: We have just 1 database containing product information (called Product), one column (type) contains information regarding the product type and is either a value of 1 or 2.
Aim: create 3 buttons on a page which correspond to different user choices
e.g. Button 1 - show items of type 1; Button 2 - show items of type 2; Button 3 - show all items.
Ideally the information regarding the button pressed should be visible to a number of pages within a class (we have an index page, as well as 3 others in the controller)
Would somebody be able to provide an outline of the code required to do this please?
I am guessing it is some combination involving the ..._controller.rb and..._helper.rb?
Thanks a lot for your patience
What I would do is the following.
First, create a scope or named_scope in your Project model for finding projects by type. You'll then be able to use this scope to query your projects depending on type.
# Rails 3
class Project
scope :by_type, lambda{ |type| where(type: type.to_i) unless type.nil? }
end
#Rails 2
class Project
named_scope :by_type, lambda do |type|
{ :conditions => { :type => type.to_i } } unless type.nil?
end
end
Next, create a before filter in your controller to load the projects of that type. The before filter should be applied to all pages where you want the buttons to be present:
class ProjectsController
before_filter :load_projects, :only => [:index, :action1, :action2]
protected
def load_projects
#projects = Project.by_type(params[:type])
end
end
Finally, create a partial for the buttons that you can include in the views that have the option of displaying different project types:
# _project_options.html.erb
<%= link_to "Button 1", :controller => params[:controller], :action => params[:action], :type => '1' %>
<%= link_to "Button 2", :controller => params[:controller], :action => params[:action], :type => '2' %>
<%= link_to "Button 3", :controller => params[:controller], :action => params[:action], :type => '' %>
You can then include this partial in each of your related views. And you'll be able to display the projects by doing something like this (if you have an _projects.html.erb partial defined):
render #projects
You can load all the Products and then hide them selectively with some javascript. Just add a class to your markup for each type of product, like this:
<%= link_to #product.name, product_path(#product), :class => #product.type %>

Resources