Permalink
Browse files

Merge pull request #551 from samvincent/185-conditionally-display-das…

…hboard-sections

Issue #185 - Add support for conditionally display Dashboard sections at
  • Loading branch information...
2 parents 9d6e3c8 + e98a601 commit 21e3ada5cc3ca5b2635c5367ee0f02199c21765c @gregbell gregbell committed Oct 21, 2011
@@ -26,6 +26,10 @@ def build(&block)
# Add a new dashboard section to a namespace. If no namespace is given
# it will be added to the default namespace.
+ #
+ # Options include:
+ # :namespace => only display for specified namespace.
+ # :if => specify a method or block to determine whether the section is rendered at run time.
def add_section(name, options = {}, &block)
namespace = options.delete(:namespace) || ActiveAdmin.application.default_namespace || :root
self.sections[namespace] ||= []
@@ -16,9 +16,21 @@ def set_current_tab
end
def find_sections
- ActiveAdmin::Dashboards.sections_for_namespace(namespace)
+ sections = ActiveAdmin::Dashboards.sections_for_namespace(namespace)
+ sections.select do |section|
+ if section.options.has_key?(:if)
+ symbol_or_proc = section.options[:if]
+ case symbol_or_proc
+ when Symbol, String then self.send(symbol_or_proc)
+ when Proc then instance_exec(&symbol_or_proc)
+ else symbol_or_proc
+ end
+ else
+ true
+ end
+ end
end
-
+
def namespace
class_name = self.class.name
if class_name.include?('::')
@@ -34,5 +34,11 @@
# section "Recent User", :priority => 1
#
# Will render the "Recent Users" then the "Recent Posts" sections on the dashboard.
+
+ # == Conditionally Display
+ # Provide a method name or Proc object to conditionally render a section at run time.
+ #
+ # section "Membership Summary", :if => :memberships_enabled?
+ # section "Membership Summary", :if => Proc.new { current_admin_user.account.memberships.any? }
end
@@ -23,4 +23,48 @@ class DashboardController < ActiveAdmin::Dashboards::DashboardController; end
end
end
+ describe "conditionally displaying sections" do
+ before { ActiveAdmin::Dashboards.clear_all_sections! }
+ let(:controller){ Admin::DashboardController.new }
+
+ context "when :if not specified" do
+ before do
+ @section = ActiveAdmin::Dashboards.add_section('Stats').last
+ end
+
+ it "should include section" do
+ controller.send(:find_sections).should include(@section)
+ end
+ end
+
+ context "when :if option specified as a method" do
+ before do
+ @section = ActiveAdmin::Dashboards.add_section('Secret Codes', :if => :i_am_awesome?).last
+ end
+
+ it "should call the method of the same name" do
+ controller.should_receive(:i_am_awesome?).and_return(true)
+ controller.send(:find_sections).should include(@section)
+
+ controller.should_receive(:i_am_awesome?).and_return(false)
+ controller.send(:find_sections).should_not include(@section)
+ end
+ end
+
+ context "when :if option specified as block" do
+ before do
+ @proc = Proc.new { true }
+ @section = ActiveAdmin::Dashboards.add_section('Secret Codes', :if => proc {}).last
+ end
+
+ it "should evaluate the block" do
+ controller.should_receive(:instance_exec).with(&@proc).and_return(true)
+ controller.send(:find_sections).should include(@section)
+
+ controller.should_receive(:instance_exec).with(&@proc).and_return(false)
+ controller.send(:find_sections).should_not include(@section)
+ end
+ end
+ end
+
end

0 comments on commit 21e3ada

Please sign in to comment.