Conditionally change resource name Activeadmin - ruby-on-rails-3

I'd like to conditionally change the label of a resource in ActiveAdmin based on the user role. I'd like to keep the default pluralization of the name for admins, but for regular users, I need to change the name.
I.e. I have a resource Users, which I would like to change to My Account for the regular user (since they won't see #index).
I'm trying something like
ActiveAdmin.register User do
menu :if => proc {if !current_user.admin?
menu :label => "My Account"
else
menu :label => "I hate Users"
end}
Anyone know how to conditionally name the resource?
Thanks!

This works:
menu :label => proc { true ? "I Hate Users" : "My Account" }
But then to access the
current_admin_user
or the
current_user
object from within the proc won't work. I haven't found a way to get the logged in user object from within the ActiveAdmin::MenuBuilder scope. There are suggestions, see e.g. https://stackoverflow.com/a/2513456/790737 where you set a variable in
Thread.current
after succesfull login. I guess you will have to hook in to the post-authentication work of devise. Good luck.

Related

Active Admin Rename the resource in different places

The documentation allows you to rename a page pretty easily, but not exactly in the way I'd like.
Given this code: ActiveAdmin.register User, :as => "Static" gives a menu name of statics
I was wondering if there was a way to customize it so the name in the menu is not pluralized => static
Rename the menu item: menu :label => "Static"
Rename the title in the top left corner: index :title => "Static"
Didn't really see them when looking through the documentation, but saw them being used in other questions/code

Custom ransacker returns error - undefined method `gsub' for []:ActiveRecord::Relation

I am using Ransack in my Rails 3.2.11 app and love it. I'm currently searching Profiles that match a given high school like so:
<%= f.check_box :profile_high_school_cont, {}, "#{current_user.profile.high_school}", '' %>
However I'd like to add some additional logic to this and further refine the search to Profiles that allow other users to find them by their high school.
So I'm wondering if there is a way to add a check for whether a Profile's :show_high_school is true in addition to whether a Profile's :high_school contains certain text.
After poking around (specifically here and here), I tried the following custom ransacker:
ransacker :by_high_school,
:formatter => proc {
|high_school| Profile.where(:high_school => high_school, :show_high_school => true)
} do |parent|
parent.table[:user]
end
I added that both to the User model, which has_one profile, and the Profile model, which belongs_to User. In each case I get an error:
undefined method `gsub' for []:ActiveRecord::Relation
When defined in Profile.rb I get the error with the following:
User.search(:profile_by_high_school_cont => "Cent").result
My DB is PostgreSQL

How to hide the resource in activeadmin gem

I create an admin page by using activeadmin gem http://activeadmin.info/
I use cancan to authorize the privilege for 2 kind of user, user manage and book manage user.
My question is: How I hide the resource User when a book manage user log in to admin page?
I tried something like that but it didn't work
menu false if can? :manage, BookHeader
or
menu false if authorize! :manage, BookHeader
Thanks for your help!
You have to use proc for build menu dynamicly
Examples
dynamic label
menu :label => proc{current_admin_user.admin? ? "Accounts" : "My Account"}
Display/Hide
menu :if => proc{ can?(:manage, BookHeader ) }
For more info about integrating AA and CanCan read this article
https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan
i use this:
for me was users for adminuser,
password for none admin user
menu label: proc{I18n.t "#{current_user.is_admin? ? 'users':'password'}"}
Following your example
menu label: proc{I18n.t "#{current_admin_user.admin? ? 'Accounts' : 'My Account'}"}
will help

HAML nesting tags

I'm trying to nest this
%h1 Admin Menu
%small logged in as: #{session[:username]}
To get something like this
<h1>Admin Menu <small>logged in as: something</small></h1>
But the only way I can get it to display without firing an error is putting them at the same level, say
%h1 Admin Menu
%small logged in as: #{session[:username]}
Which outputs:
<h1>Admin Menu</h1>
<small>logged in as: something</small>
It's probaby something silly but I have no idea this why this wouldn't work?
Haml only allows inline nesting if everything that will be nested is inline. So you can do this:
%h1 Admin Menu
or this:
%h1
Admin Menu
%small logged in as: #{session[:username]}
but not this:
%h1 Admin Menu
%small logged in as: #{session[:username]}
The first form should really be thought of as a convenient abbreviation. It exists so you can take something like this:
%li
One
%li
Two
%li
Three
and just say:
%li One
%li Two
%li Three
I haven't tried, but won't the following work for you?
%h1
Admin Menu
%small logged in as: #{session[:username]}
The other trick is this.
%h1
Admin Menu
%small
logged in as:
=session[:username]
because last 2 lines will be rendered inside "small"-tag
Or you could just use simple css to do the trick, as:
%h1
Admin Menu
.small{ style: 'font-size: smaller;' }
logged in as:
= session[:username]
this way you can specify other attributes or the specific size of the font.

Activeadmin disabling the "new resource" method

I'm using Activeadmin for the admin interface on an app I'm working on (loving it) and I am curious if there is a way to disable the "New Resource" link in the upper-right corner of the resource show page?
The particular resource I'm using is nested inside another resource and I have a partial that allows it to be created from the show page on that parent resource.
I have disabled the resource in the menu, but I'd rather leave the resource in the menu so I can see/edit/delete those resources without having to find it by looking through its parent resource.
Previous solution didn`t work for me, so here is general solutions, that works always:
ActiveAdmin.register Book do
actions :index
#or like that
#actions :all, :except => [:destroy]
index do
column :title
column :author
end
end
Try config.clear_action_items! to remove the link to New and other links on top of the table
This removed the "New Resource" button from the top-right:
config.clear_action_items!
This removed both the "New Resource" button as well as the box "There are no resources yet - create one".
actions :all, :except => [:new]
Thank you, Irio
config.clear_action_items!
Will remove all the actions.
If you only want to remove the new action link you can also use:
config.remove_action_item(:new)
I know this is an old question, but I just came up to it (had the same problem), and realized that config.clear_action_items! and actions :all, :except => [:new] are fundamentally different.
config.clear_action_items! will remove the New button from the index page, while actions :all, :except => [:new] will remove both the button, AND the route, meaning you can't call it from another place (which, in my case, is needed).
I did this:
controller do
def action_methods
if some_condition
super
else
super - ['new', 'create', 'destroy']
end
end
end
To disable some of the possible actions. action_methods returns an array of the 7 standard CRUD actions, so you can subtract those you don’t want
Or even:
ActiveAdmin.register Purchase do
config.clear_action_items!
actions :index
end
Worked for me too ! :-)
ActiveAdmin.register AssetSumView do
menu :label => "Asset Summary View", :parent => "Things"
# no button for NEW (since this is a db view)
#---------------------------------------------------------------------------------------------
config.clear_action_items!
enter code here
action_item do
link_to "Assets" , "/admin/assets"
end
action_item do
link_to "AssetCatgCodes", "/admin/asset_catg_codes"
end
#---------------------------------------------------------------------------------------------
config.clear_action_items! does only half of the job. There is one issue though.
In case of empty index table, active admin show this message
There are no [Resources] yet. Create one
which doesn't get hidden by the above command and I don't want to entirely disable the action. So, I kept the link and edited the new action to redirect to the parent resource index with a message.
controller do
def new
if params[:parent_id].present?
super
else
redirect_to parent_resources_path, notice: "Create Resource through ParentResource"
end
end
end