I have an active admin resource like this:
ActiveAdmin.register Snippet do
menu label: "Text Snippets"
config.clear_sidebar_sections!
index download_links: false do
column :key if current_admin_user.developer?
column :description
column :contents
default_actions
end
form do |f|
f.inputs do
f.input :description
f.input :contents
end
f.buttons
end
end
Notice in the index
block, I'm only adding the key
column if the current admin user is a developer. I want to apply this kind of filtering to the available actions.
I tried added this at the top of the resource definition:
actions current_admin_user.developer ? :all : :index, :edit
But I get a NameError
on current_admin_user
. For some reason, outside of the configuration blocks, the active admin current_admin_user
helper doesn't exist.
So, how could I go about filtering actions based on the current user's priviliges?
you have to use a proc... current_admin_user works only when the app it's running, not when you declare your class..
example..
action_item :only => [:edit], :if => proc { current_admin_user.developer? } do
#enter code here
end
You can also use CanCan for this.. and place controller.authorize_resource
at the beginning. Check the activeadmin documentation for this.
Another way to do it is overriding the action_methods in the ActiveAdmin controller.. like this
actions :all
controller do
def action_methods
if current_admin_user.developer?
super
else
super - ['show', 'destroy', 'new', 'create']
end
end
end
this works cool if you have multiple roles.
btw you should use developer?
instead of developer
(off course if the developer
method returns a boolean as I suspect)
UPDATE
in version 0.6+ you should stick with CanCan, for more information check the activeadmin documentation http://www.activeadmin.info/docs/13-authorization-adapter.html#using_the_cancan_adapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With