Skip to content

Commit 7027cb1

Browse files
committed
Created a nicer syntax for defining many dashboard sections.
Example: ActiveAdmin::Dashboards.build do section "Recent Post" do # return a list of posts end section "New Users This Week" do # return a list of users this week end end
1 parent bd17660 commit 7027cb1

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

lib/active_admin.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,8 @@ def around_filter(*args, &block)
222222

223223
# Helper method to add a dashboard section
224224
def dashboard_section(name, options = {}, &block)
225-
namespace = options.delete(:namespace) || default_namespace
226-
ActiveAdmin::Dashboards.add_section(namespace, name, options, &block)
225+
ActiveAdmin::Dashboards.add_section(name, options, &block)
227226
end
228227

229-
230228
end
231229
end

lib/active_admin/dashboards.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,31 @@ module Dashboards
1010
mattr_accessor :sections
1111

1212
class << self
13-
def add_section(namespace, name, options = {}, &block)
14-
self.sections[namespace] ||= []
15-
self.sections[namespace] << Section.new(namespace, name, options, &block)
16-
self.sections[namespace].sort!
13+
14+
# Eval an entire block in the context of this module to build
15+
# dashboards quicker.
16+
#
17+
# Example:
18+
#
19+
# ActiveAdmin::Dashboards.build do
20+
# section "Recent Post" do
21+
# # return a list of posts
22+
# end
23+
# end
24+
#
25+
def build(&block)
26+
module_eval(&block)
27+
end
28+
29+
# Add a new dashboard section to a namespace. If no namespace is given
30+
# it will be added to the default namespace.
31+
def add_section(name, options = {}, &block)
32+
namespace = options.delete(:namespace) || ActiveAdmin.default_namespace
33+
self.sections[namespace] ||= []
34+
self.sections[namespace] << Section.new(namespace, name, options, &block)
35+
self.sections[namespace].sort!
1736
end
37+
alias_method :section, :add_section
1838

1939
def sections_for_namespace(namespace)
2040
@@sections[namespace] || []
@@ -23,6 +43,7 @@ def sections_for_namespace(namespace)
2343
def clear_all_sections!
2444
@@sections = {}
2545
end
46+
2647
end
2748

2849
end

spec/unit/active_admin_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
22

33
describe ActiveAdmin do
44

spec/unit/dashboards_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@
99
describe "adding sections" do
1010
before do
1111
ActiveAdmin::Dashboards.clear_all_sections!
12-
ActiveAdmin::Dashboards.add_section(:admin, 'Recent Posts')
12+
ActiveAdmin::Dashboards.add_section('Recent Posts')
1313
end
1414
it "should add a new section namespaced" do
1515
ActiveAdmin::Dashboards.sections[:admin].first.should be_an_instance_of(ActiveAdmin::Dashboards::Section)
1616
end
1717
end
1818

19+
describe "adding sections using the build syntax" do
20+
before do
21+
ActiveAdmin::Dashboards.clear_all_sections!
22+
ActiveAdmin::Dashboards.build do
23+
section "Recent Posts" do
24+
end
25+
end
26+
end
27+
28+
it "should add a new section" do
29+
ActiveAdmin::Dashboards.sections[:admin].first.should be_an_instance_of(ActiveAdmin::Dashboards::Section)
30+
end
31+
end
32+
1933
describe "clearing all sections" do
2034
before do
21-
ActiveAdmin::Dashboards.add_section(:admin, 'Recent Posts')
35+
ActiveAdmin::Dashboards.add_section('Recent Posts')
2236
end
2337
it "should clear all sections" do
2438
ActiveAdmin::Dashboards.clear_all_sections!
@@ -29,7 +43,7 @@
2943
describe "finding namespaced sections" do
3044
context "when the namespace exists" do
3145
before do
32-
ActiveAdmin::Dashboards.add_section(:admin, 'Recent Posts')
46+
ActiveAdmin::Dashboards.add_section('Recent Posts')
3347
end
3448
it "should return an array of sections" do
3549
ActiveAdmin::Dashboards.sections_for_namespace(:admin).should_not be_empty

0 commit comments

Comments
 (0)