Bishma Stornelli
Bishma Stornelli

Reputation: 2569

ActiveAdmin action items depending on status of data

I want to hide the edit path if the object to edit has a certain status.

How can I do that?

Upvotes: 8

Views: 11663

Answers (6)

Bishma Stornelli
Bishma Stornelli

Reputation: 2569

I finally did it. I needed two things:

Redirect when access directly and hide buttons to the edit page.

To redirect when the user try to access directly to the edit page I use a before_filter:

before_filter :some_method, :only => [:edit, :update]
def some_method
    redirect_to action: :show if status == something
end 

To hide the buttons I do it like this:

ActiveAdmin.register Model do
    config.clear_action_items!
    action_item :only => [:show] , :if => proc { instance.status == something } do
        link_to 'Edit', edit_model_path(model)
    end
end

Upvotes: 13

thisismydesign
thisismydesign

Reputation: 25082

A fully customizable solution would be to use an authorization adapter, either a custom one or a library such as pundit or cancan: https://activeadmin.info/13-authorization-adapter.html

My use case was around restricting actions based on the context (e.g. the user editing). I solved it locally like this:

controller do
  def action_methods
    if condition?
      super
    else
      super - ['edit', 'update']
    end
  end
end

Upvotes: 0

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

This can be achieve using the following:

ActiveAdmin.register Object do
  index do
    column :name
    actions defaults: true do |object|
      link_to 'Archive', archive_admin_post_path(post) if object.status?
    end
  end
end

Note that using defaults: true will append your custom action to active admin default actions.

Upvotes: 3

cdesrosiers
cdesrosiers

Reputation: 8892

If you are talking about hiding the edit link that is shown by default (along with the view and delete links) in the index action, you can customize the index view as follows:

ActiveAdmin.register Model do

  index do

    column :actions do |object|

      raw( %(#{link_to "View", [:admin, object]} 
        #{link_to "Delete", [:admin, object], method: :delete} 
        #{(link_to"Edit", [:edit, :admin, object]) if object.status? }) )

    end
  end
end

Because the content of the column will be only what is returned by the column block, you need to return all three (or two) links at once as a string. Here raw is used so that the actual links will be displayed and not the html for the links.

Upvotes: 7

Prasad Surase
Prasad Surase

Reputation: 6574

if u want to hide the "edit" link (in active_admin views) for object if the object holds some specific value, u can override the default view for the method and add condition before the link is displayed.

Upvotes: -2

jameswilliamiii
jameswilliamiii

Reputation: 878

You could create a before_filter in your controller that only applies to edit action. It could check the status, and allow it to run or redirect_to depending on the return of the method.

Something like this in your applications controller:

def some_method
  if foo.bar == true
    redirect_to foos_path
  end
end

Then in the beginning of your controller of question

before_filter :some_method, :only => :edit

Upvotes: 0

Related Questions