railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.89k stars 2.26k forks source link

Show custom action for specific model only #1004

Closed JohnGoodman closed 12 years ago

JohnGoodman commented 12 years ago

Hey All,

I wrote a custom action for my rails_admin app. I only want this action to show on in the list view of a specific model. I was finally able to achieve this by adding the action name to the default action list like so:

config.actions do
  # root actions
  dashboard                     # mandatory
  # collection actions
  index                         # mandatory
  new
  export
  history_index
  bulk_delete
  # member actions
  show
  edit
  delete
  history_show
  show_in_app
  duplicate_program # This is the custom action
end

In my rails_admin_duplicate_program.rb file, I set the visibility like this:

register_instance_option :visible do
      bindings[:object].class.name.downcase == 'program'
 end

Gists of these files are here: https://gist.github.com/1894506 https://gist.github.com/1894513

My question is, is there a better way to only show a custom action for a specified model?

Thanks.

stantona commented 12 years ago

I'm having the same issue. It would be nice to have an easy, less verbose way to specify custom actions on models. One use case where this would be appropriate is "reset password" on a user.

phuwanart commented 12 years ago

+1

bbenezech commented 12 years ago

Proposed API:

actions do
  update_password do
    only User, Admin
    # except Stuff, Stuff2
  end
  ...
end

That should do it.

Meanwhile you guys can also use Cancan

can :update_password, [User, Admin]

JohnGoodman commented 12 years ago

Nice, the proposed API looks good. Thanks.

phuwanart commented 12 years ago

Thank for your help.

kritik commented 12 years ago

I can propose to switch on not in config.actions, but

config.model User do
  actions do
    update_password
  end
end
meetme2meat commented 12 years ago

undefined method `only' for #RailsAdmin::Config::Actions::MarkAsManager:0xc209728 when using the above API

actions do
  mark_as_manager do
    only User, Admin
    # except Stuff, Stuff2
  end
  ...
end
kritik commented 12 years ago

@meetme2meat it was just a proposal

ankita14 commented 8 years ago

Thanks bbenezech!