Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Issue #185 - Add support for conditionally display Dashboard sections at #551

Merged
merged 1 commit into from

3 participants

@samvincent

Issue #185 - Add support for conditionally display Dashboard sections at run-time

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? }

@samvincent samvincent Issue #185 - Add support for conditionally display Dashboard sections…
… at run-time

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? }
e98a601
@macfanatic

I would love to see this pull request pulled into the main trunk.

@gregbell gregbell merged commit 21e3ada into activeadmin:master
@gregbell
Owner

Thanks Sam!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 30, 2011
  1. @samvincent

    Issue #185 - Add support for conditionally display Dashboard sections…

    samvincent authored
    … at run-time
    
    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? }
This page is out of date. Refresh to see the latest.
View
4 lib/active_admin/dashboards.rb
@@ -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] ||= []
View
16 lib/active_admin/dashboards/dashboard_controller.rb
@@ -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?('::')
View
6 lib/generators/active_admin/install/templates/dashboards.rb
@@ -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
View
44 spec/unit/dashboard_controller_spec.rb
@@ -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
Something went wrong with that request. Please try again.