From c12dc45cdb1922e131044f75bbdcace0213518db Mon Sep 17 00:00:00 2001 From: Drew Ulmer Date: Tue, 14 Feb 2012 09:27:49 -0600 Subject: [PATCH] Adds ability to suppress scope count on a per-scope basis Suppress scope count on the index page by passing :show_count => false to a scope definition. --- features/index/index_scopes.feature | 13 +++++++++++++ lib/active_admin/scope.rb | 3 ++- lib/active_admin/views/components/scopes.rb | 2 +- spec/unit/scope_spec.rb | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/features/index/index_scopes.feature b/features/index/index_scopes.feature index 834843c33a6..68101f58f9c 100644 --- a/features/index/index_scopes.feature +++ b/features/index/index_scopes.feature @@ -56,6 +56,19 @@ Feature: Index Scoping And I should see the scope "All" with no count And I should see 10 posts in the table + @scope + Scenario: Viewing resources with a scope and scope count turned off for a single scope + Given 10 posts exist + And an index configuration of: + """ + ActiveAdmin.register Post do + scope :all, :default => true, :show_count => false + end + """ + Then I should see the scope "All" selected + And I should see the scope "All" with no count + And I should see 10 posts in the table + Scenario: Viewing resources when scoping Given 6 posts exist And 4 published posts exist diff --git a/lib/active_admin/scope.rb b/lib/active_admin/scope.rb index dfe043c519b..5f2a00f8862 100644 --- a/lib/active_admin/scope.rb +++ b/lib/active_admin/scope.rb @@ -1,7 +1,7 @@ module ActiveAdmin class Scope - attr_reader :name, :scope_method, :id, :scope_block, :display_if_block + attr_reader :name, :scope_method, :id, :scope_block, :display_if_block, :show_count # Create a Scope # @@ -30,6 +30,7 @@ def initialize(name, method = nil, options = {}, &block) @scope_block = block end + @show_count = options[:show_count].nil? ? true : options[:show_count] @display_if_block = options[:if] end diff --git a/lib/active_admin/views/components/scopes.rb b/lib/active_admin/views/components/scopes.rb index dee4da1f6ca..0f896bca43b 100644 --- a/lib/active_admin/views/components/scopes.rb +++ b/lib/active_admin/views/components/scopes.rb @@ -38,7 +38,7 @@ def build_scope(scope, options) text_node scope_name span :class => 'count' do "(" + get_scope_count(scope).to_s + ")" - end if options[:scope_count] + end if options[:scope_count] && scope.show_count end end end diff --git a/spec/unit/scope_spec.rb b/spec/unit/scope_spec.rb index 5de1a45e96d..a7afe3b6e8b 100644 --- a/spec/unit/scope_spec.rb +++ b/spec/unit/scope_spec.rb @@ -52,4 +52,18 @@ end + describe "show_count" do + + it "should allow setting of show_count to prevent showing counts" do + scope = ActiveAdmin::Scope.new(:default, nil, :show_count => false) + scope.show_count.should == false + end + + it "should set show_count to true if not passed in" do + scope = ActiveAdmin::Scope.new(:default) + scope.show_count.should == true + end + + end + end