Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - Using scopes with filters

In my ActiveAdmin model I have a custom scope to show deleted records and several filters for searching records by specific columns.

Using the filters individually or combined together works as expected.

Using a scope works as expected.

The problem is that using a scope seemingly overrides all the filters and after selecting a scope any filter added does nothing.

Anyone have any ideas here? What I want is to be able to show a specific scope and then still be able to filter results within that scope.

    ActiveAdmin.register Example do
      scope :deleted do |example|
        Example.only_deleted
      end

      scope :all do |example|
        Example.with_deleted
      end

      filter :title
      filter :description

      index do
        column :title
        column :description
      end

    end

[update]

Here's the solution I've gone with. I set the with_deleted scope on the model and include filter on the side for showing/hiding deleted results. Not ideal since initially deleted results are also shown, but at least all filters can be used together.

    ActiveAdmin.register Example.with_deleted do

      filter :title
      filter :description
      filter :deleted, :as => :select, :collection => {:true => nil, :false => false }

      index do
        column :title
        column :description
      end

    end
like image 277
xivusr Avatar asked May 09 '12 20:05

xivusr


1 Answers

By default, ActiveAdmin wants scopes to just provide a symbolized method name. When you define scopes this way, they can be chained to the scoping already provided by filters and they work together seamlessly.

So your mistake is explicitly calling Model#class_method instead of providing :symbolized_class_method_name (with an implied owner of the current model).

Filters and scopes will work together if you replace this code:

scope :all do |example|
  Example.with_deleted
end

scope :deleted do |example|
  Example.only_deleted
end

With this:

scope "Deleted", :only_deleted
scope "All", :with_deleted
like image 127
armchairdj Avatar answered Oct 01 '22 06:10

armchairdj