Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored filters from ResourceController to Resource

* Moved filters code in ResourceController to Resource
* Added an inheritable setting for enabling and disabling filters
* Added docs for disabling filters
  • Loading branch information...
commit 6b06835c06018ab4e47e1a3b547e4f90fee115b0 1 parent 0bb1cc7
@gregbell gregbell authored
View
23 docs/3-index-pages.md
@@ -54,6 +54,29 @@ You can change the filter label by passing a label option:
By default, Active Admin will try to use ActiveModel I18n to determine the label.
+Filters can also be disabled for a resource, a namespace or the entire
+application.
+
+To disable for a specific resource:
+
+ ActiveAdmin.register Post do
+ config.filters = false
+ end
+
+To disable for a namespace, in the initializer:
+
+ ActiveAdmin.setup do |config|
+ config.namespace :my_namespace do |my_namespace|
+ my_namespace.filters = false
+ end
+ end
+
+Or to disable for the entire application:
+
+ ActiveAdmin.setup do |config|
+ config.filters = false
+ end
+
## Index default sort order
You can define the default sort order for index pages:
View
10 features/index/filters.feature
@@ -66,3 +66,13 @@ Feature: Index Filtering
Then I should see 1 posts in the table
And I should see "Hello World" within ".index_table"
And the "jane_doe" checkbox should be checked
+
+ Scenario: Disabling filters
+ Given an index configuration of:
+ """
+ ActiveAdmin.register Post do
+ config.filters = false
+ end
+ """
+ Then I should not see a sidebar titled "Filters"
+
View
2  lib/active_admin.rb
@@ -130,5 +130,7 @@ def after_load(&block)
ActiveAdmin::DependencyChecker.check!
+# Require internal Plugins
require 'active_admin/comments'
require 'active_admin/batch_actions'
+require 'active_admin/filters'
View
3  lib/active_admin/application.rb
@@ -60,6 +60,9 @@ def self.inheritable_setting(name, default)
# Whether the batch actions are enabled or not
inheritable_setting :batch_actions, true
+ # Whether filters are enabled
+ inheritable_setting :filters, true
+
# The namespace root.
inheritable_setting :root_to, 'dashboard#index'
View
3  lib/active_admin/comments.rb
@@ -23,6 +23,9 @@
namespace.register ActiveAdmin::Comment, :as => "Comment" do
actions :index, :show, :create
+ # Ensure filters are turned on
+ config.filters = true
+
# Don't display in the menu
menu false
View
10 lib/active_admin/filters.rb
@@ -0,0 +1,10 @@
+ActiveAdmin.before_load do |app|
+
+ require 'active_admin/filters/dsl'
+ require "active_admin/filters/resource_extension"
+
+ # Add our Extensions
+ ActiveAdmin::ResourceDSL.send :include, ActiveAdmin::Filters::DSL
+ ActiveAdmin::Resource.send :include, ActiveAdmin::Filters::ResourceExtension
+
+end
View
12 lib/active_admin/filters/dsl.rb
@@ -0,0 +1,12 @@
+module ActiveAdmin
+ module Filters
+ module DSL
+
+ # For docs, please see ActiveAdmin::Filters::ResourceExtension#add_filter
+ def filter(attribute, options = {})
+ config.add_filter(attribute, options)
+ end
+
+ end
+ end
+end
View
92 lib/active_admin/filters/resource_extension.rb
@@ -0,0 +1,92 @@
+module ActiveAdmin
+ module Filters
+
+ module ResourceExtension
+
+ def initialize(*)
+ super
+ add_filters_sidebar_section
+ end
+
+ # Returns the filters for this resource. If filters are not enabled,
+ # it will always return an empty array.
+ #
+ # @return [Array] Filters that apply for this resource
+ def filters
+ return [] unless filters_enabled?
+
+ @filters || default_filters
+ end
+
+ # Setter to enable / disable filters on this resource.
+ #
+ # Set to `nil` to inherit the setting from the namespace
+ def filters=(bool)
+ @filters_enabled = bool
+ end
+
+ # @return [Boolean] If filters are enabled for this resource
+ def filters_enabled?
+ @filters_enabled.nil? ? namespace.filters : @filters_enabled
+ end
+
+ # Add a filter for this resource. If filters are not enabled, this method
+ # will raise a RuntimeError
+ #
+ # @param [Symbol] attribute The attribute to filter on
+ # @param [Hash] options The set of options that are passed through to
+ # metasearch for the field definition.
+ def add_filter(attribute, options = {})
+ unless filters_enabled?
+ raise RuntimeError, "Can't add a filter when filters are disabled. Enable filters with 'config.filters = true'"
+ end
+
+ @filters ||= []
+
+ @filters << options.merge({ :attribute => attribute })
+ end
+
+ # Reset the filters to use defaults
+ def reset_filters!
+ @filters = nil
+ end
+
+ private
+
+ # @return [Array] The array of default for filters for this resource
+ def default_filters
+ default_association_filters + default_content_filters
+ end
+
+ # Returns a default set of filters for the associations
+ def default_association_filters
+ if resource_class.respond_to?(:reflections)
+ resource_class.reflections.collect{|name, r| { :attribute => name }}
+ else
+ []
+ end
+ end
+
+ # Returns a default set of filters for the content columns
+ def default_content_filters
+ if resource_class.respond_to?(:content_columns)
+ resource_class.content_columns.collect{|c| { :attribute => c.name.to_sym } }
+ else
+ []
+ end
+ end
+
+ def add_filters_sidebar_section
+ self.sidebar_sections << filters_sidebar_section
+ end
+
+ def filters_sidebar_section
+ ActiveAdmin::SidebarSection.new(:filters, :only => :index, :if => proc{ active_admin_config.filters.any? } ) do
+ active_admin_filters_form_for assigns[:search], active_admin_config.filters
+ end
+ end
+
+ end
+
+ end
+end
View
13 lib/active_admin/resource/sidebars.rb
@@ -5,11 +5,6 @@ module ActiveAdmin
class Resource
module Sidebars
- def initialize(*args)
- super
- add_default_sidebar_sections
- end
-
def sidebar_sections
@sidebar_sections ||= []
end
@@ -26,14 +21,6 @@ def sidebar_sections?
!!@sidebar_sections && @sidebar_sections.any?
end
- private
-
- def add_default_sidebar_sections
- self.sidebar_sections << ActiveAdmin::SidebarSection.new(:filters, :only => :index) do
- active_admin_filters_form_for assigns[:search], filters_config
- end
- end
-
end
end
View
2  lib/active_admin/resource_controller.rb
@@ -3,7 +3,6 @@
require 'active_admin/resource_controller/action_builder'
require 'active_admin/resource_controller/callbacks'
require 'active_admin/resource_controller/collection'
-require 'active_admin/resource_controller/filters'
require 'active_admin/resource_controller/scoping'
require 'active_admin/resource_controller/resource_class_methods'
@@ -22,7 +21,6 @@ class ResourceController < BaseController
include ActionBuilder
include Callbacks
include Collection
- include Filters
include Scoping
extend ResourceClassMethods
View
58 lib/active_admin/resource_controller/filters.rb
@@ -1,58 +0,0 @@
-module ActiveAdmin
- class ResourceController < BaseController
-
- module Filters
- extend ActiveSupport::Concern
-
- included do
- helper_method :filters_config
- end
-
- module ClassMethods
- def filter(attribute, options = {})
- return false if attribute.nil?
- @filters ||= []
- @filters << options.merge(:attribute => attribute)
- end
-
- def filters_config
- @filters && @filters.any? ? @filters : default_filters_config
- end
-
- def reset_filters!
- @filters = []
- end
-
- # Returns a sane set of filters by default for the object
- def default_filters_config
- default_association_filters + default_content_filters
- end
-
- # Returns a default set of filters for the associations
- def default_association_filters
- if resource_class.respond_to?(:reflections)
- resource_class.reflections.collect{|name, r| { :attribute => name }}
- else
- []
- end
- end
-
- # Returns a default set of filters for the content columns
- def default_content_filters
- if resource_class.respond_to?(:content_columns)
- resource_class.content_columns.collect{|c| { :attribute => c.name.to_sym } }
- else
- []
- end
- end
- end
-
- protected
-
- def filters_config
- self.class.filters_config
- end
- end
-
- end
-end
View
4 lib/active_admin/resource_dsl.rb
@@ -145,10 +145,6 @@ def collection_action(name, options = {}, &block)
delegate :before_save, :after_save, :to => :controller
delegate :before_destroy, :after_destroy, :to => :controller
- # Filters
- delegate :filter, :to => :controller
-
-
# Standard rails filters
delegate :before_filter, :skip_before_filter, :after_filter, :around_filter, :to => :controller
View
60 spec/unit/filters/resource_spec.rb
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe ActiveAdmin::Filters::ResourceExtension do
+
+ let(:resource) do
+ namespace = ActiveAdmin::Namespace.new(ActiveAdmin::Application.new, :admin)
+ namespace.register(Post)
+ end
+
+ it "should return the defaults if no filters are set" do
+ resource.filters.map{|f| f[:attribute].to_s }.sort.should == %w{
+ author body category created_at published_at title updated_at
+ }
+ end
+
+ it "should not have defaults when filters are disabled on the resource" do
+ resource.filters = false
+ resource.filters.should be_empty
+ end
+
+ it "should not have defaults when the filters are disabled on the namespace" do
+ resource.namespace.filters = false
+ resource.filters.should be_empty
+ end
+
+ it "should not have defaults when the filters are disabled on the application" do
+ resource.namespace.application.filters = false
+ resource.filters.should be_empty
+ end
+
+ it "should add a filter" do
+ resource.add_filter :title
+ resource.filters.should == [{:attribute => :title}]
+ end
+
+ it "should add a filter with options" do
+ resource.add_filter :title, :as => :string
+ resource.filters.should == [{:attribute => :title, :as => :string}]
+ end
+
+ it "should raise an exception if trying to add a filter when they are disabled" do
+ resource.filters = false
+ expect {
+ resource.add_filter :title
+ }.should raise_error(RuntimeError)
+ end
+
+ it "should reset filters" do
+ resource.add_filter :title
+ resource.filters.size.should == 1
+ resource.reset_filters!
+ resource.filters.size.should > 1
+ end
+
+ it "should add a sidebar section for the filters" do
+ resource.sidebar_sections.first.name.should == :filters
+ end
+
+
+end
View
7 spec/unit/resource/sidebars_spec.rb
@@ -40,11 +40,4 @@
end
- describe "default sidebar sections" do
-
- it "should have a filters section by default" do
- resource.sidebar_sections.first.name.should == :filters
- end
-
- end
end
View
2  spec/unit/resource_registration_spec.rb
@@ -49,7 +49,7 @@
config_1 = ActiveAdmin.register(Category) { filter :name }
config_2 = ActiveAdmin.register(Category) { filter :id }
config_1.should == config_2
- config_1.controller.filters_config.size.should == 2
+ config_1.filters.size.should == 2
end
context "with different resource classes" do

0 comments on commit 6b06835

Please sign in to comment.
Something went wrong with that request. Please try again.