ActiveAdmin hide Delete action by condition
Asked Answered
L

3

7

I have some problem.

In ActiveAdmin I need hide DELETE action by condition.

I did it for #index page. But I don't know how do this trick with #show page.

Here's code:

index do
    selectable_column
    column :id do |package|
      link_to package.id, admin_subscription_package_path(package)
    end
    column :title
    column :plan_status
    column :duration do |package|
      if package.duration == 1
        "#{package.duration} day"
      else
        "#{package.duration} days"
      end
    end
    column 'Price (USD)', :price do |package|
      number_to_currency(package.price, locale: :en)
    end
    column :actions do |object|
      raw(
          %(
            #{link_to 'View', admin_subscription_package_path(object)}
            #{(link_to 'Delete', admin_subscription_package_path(object),
                       method: :delete) unless object.active_subscription? }
            #{link_to 'Edit', edit_admin_subscription_package_path(object)}
          )
      )

    end
  end

Or maybe I can do it more useful for all pages at once.

Lehmann answered 27/11, 2014 at 12:16 Comment(0)
S
8

Use action_item for this purpose:

ActiveAdmin.register MyModel

  actions :index, :show, :new, :create, :edit, :update, :destroy

  action_item only: :show  do
    if condition
      link_to "Delete whatever", {action: :destroy}, method: :delete, confirm: 'Something will be deleted forever. Sure?'
    end
  end

end
Semimonthly answered 27/11, 2014 at 12:42 Comment(6)
Thanks, but how I can get current object in this case? Like this: unless object.active_subscription?Lehmann
use resource.active_subscription. In activeadmin resource is current_object, you can check it by putting raise resource.inspect inside action_itemSemimonthly
Nice. Thx, but I have some problem. Default action are displayed to. I tried do this config.clear_action_items! But in this case actions from all pages are removed, but I need only on show.Lehmann
Add actions :index, :show, :new, :create, etc to chose which actions you want to be displayed (but still leave the config.clear_action_items!)Semimonthly
Where and how it should be?Lehmann
incomplete answer... it still shows default action.. removing all is not practical.Celestaceleste
W
2

Another solution from here https://groups.google.com/g/activeadmin/c/102jXVwtgcU

ActiveAdmin.register Foo do

  RESTRICTED_ACTIONS = ["edit", "update"]
  actions [:index, :show, :edit, :update]

  controller do
    def action_methods
      if current_admin_user.role?(AdminUser::ADMIN_ROLE)
        super
      else
        super - RESTRICTED_ACTIONS
      end
    end
  end

  ...
end
Widner answered 2/3, 2021 at 14:35 Comment(0)
B
1

I was looking for a way to solve this without needing to hand-write the buttons / actions links myself.

After a bit of reading through the active admin code, I found this hack:


ActiveAdmin.register User do # replace User by the type of the resource in your list

  # ... your config, index column definitions, etc.

  controller do

    # ... maybe some other controller stuff

    def authorized?(action, resource) 
      return false unless super(action, resource)

      if resource.is_a? User # replace User by the type of the resource in your list
        return false if action == ActiveAdmin::Auth::DESTROY && condition  # replace condition with your check involving the resource
      end

      true
    end

  end
end
Baldwin answered 19/10, 2021 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.