Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 300
Bishma Stornelli Avatar asked Oct 01 '12 19:10

Bishma Stornelli


3 Answers

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(instance)
    end
end
like image 92
Bishma Stornelli Avatar answered Nov 05 '22 06:11

Bishma Stornelli


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.

like image 41
cdesrosiers Avatar answered Nov 05 '22 06:11

cdesrosiers


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.

like image 41
Pierre-Louis Gottfrois Avatar answered Nov 05 '22 06:11

Pierre-Louis Gottfrois