Scopes as filters in rails_admin

I know it is a very old issue but someone redirected me to this thread. You can achieve this easily by configuring the rails_admin as follows

# /config/initializers/rails_admin.rb
config.model User do
    list do
        scopes [nil, :unconfirmed]
    end
end

This will insert two tabs on top of the list labelled All and Unconfirmed with the records filtered in respective tabs. Clicking those tabs will fire a query applying your custom scope

Hope it helps.


I've managed to do this successfully by adding a custom rails_admin action.

More details: https://github.com/sferik/rails_admin/wiki/Custom-action

For example:

# in lib/rails_admin/unconfirmed.rb

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdminUnconfirmed
end

module RailsAdmin
  module Config
    module Actions

      class Unconfirmed < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :controller do
          Proc.new do
            @objects = User.unconfirmed
            render "index"
          end
        end

        register_instance_option :collection do
          true
        end
      end
    end
  end
end

The key is that it's a 'collection' action. Then you add it to the rails_admin setup:

# in config/initializers/rails_admin.rb
# require File.join(Rails.root, "lib", "rails_admin", "unconfirmed")

RailsAdmin.config do |config|
  config.actions do
    # root actions
    dashboard
    # collection actions
    index
    unconfirmed do
      only 'User'
    end
  end
  # snip!
end

This new action will appear at the index level of the User model.