Skip to content

Commit feb2a7a

Browse files
ugisozolsparndt
authored andcommitted
When registering tab return an instance of itself.
Main reason for this change is specs. It's so much easier to get ahold of the tab I just registered than to select it from Refinery::Pages.tabs array. * Add specs for Refinery::Pages::Tab. * Return tabs incase user isn't using view_templates. * Removed attr_accessor from Refinery::Pages module because it's not needed and it's not even working that way.
1 parent 3b2633c commit feb2a7a

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

pages/lib/refinery/pages/tab.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Refinery
22
module Pages
33

4-
attr_accessor :tabs
5-
64
def self.tabs
75
@tabs ||= []
86
end
97

108
def self.tabs_for_template(template)
9+
return tabs unless template
10+
1111
tabs.select do |tab|
1212
tab.templates.include?('all') || tab.templates.include?(template)
1313
end
@@ -26,6 +26,8 @@ def self.register(&block)
2626

2727
tab.templates = %w[all] if tab.templates.blank?
2828
tab.templates = Array(tab.templates)
29+
30+
tab
2931
end
3032

3133
protected

pages/spec/lib/pages/tab_spec.rb

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require "spec_helper"
2+
3+
module Refinery
4+
module Pages
5+
6+
describe ".tabs" do
7+
after do
8+
Refinery::Pages.instance_eval { @tabs = [] }
9+
end
10+
11+
it "returns an array of registered tabs" do
12+
rspec_tab = Refinery::Pages::Tab.register do |tab|
13+
tab.name = "rspec"
14+
tab.partial = "rspec"
15+
end
16+
17+
Refinery::Pages.tabs.should include(rspec_tab)
18+
end
19+
end
20+
21+
describe ".tabs_for_template" do
22+
after do
23+
Refinery::Pages.instance_eval { @tabs = [] }
24+
end
25+
26+
it "returns all tabs for which #templates hasn't been set" do
27+
rspec_tab = Refinery::Pages::Tab.register do |tab|
28+
tab.name = "rspec"
29+
tab.partial = "rspec"
30+
end
31+
32+
Refinery::Pages.tabs_for_template("huh").should include(rspec_tab)
33+
end
34+
35+
it "returns tabs with matched template" do
36+
rspec_tab = Refinery::Pages::Tab.register do |tab|
37+
tab.name = "rspec"
38+
tab.partial = "rspec"
39+
tab.templates = "rspec"
40+
end
41+
42+
Refinery::Pages.tabs_for_template("rspec").should include(rspec_tab)
43+
end
44+
end
45+
46+
describe Tab do
47+
after do
48+
Refinery::Pages.instance_eval { @tabs = [] }
49+
end
50+
51+
describe ".register" do
52+
it "requires name to be set" do
53+
lambda {
54+
Refinery::Pages::Tab.register do |tab|
55+
tab.partial = "rspec"
56+
end
57+
}.should raise_error(/A tab MUST have a name!/)
58+
end
59+
60+
it "requires partial to be set" do
61+
lambda {
62+
Refinery::Pages::Tab.register do |tab|
63+
tab.name = "rspec"
64+
end
65+
}.should raise_error(/A tab MUST have a partial!/)
66+
end
67+
68+
it "sets #templates if it's not set" do
69+
rspec_tab = Refinery::Pages::Tab.register do |tab|
70+
tab.name = "rspec"
71+
tab.partial = "rspec"
72+
end
73+
74+
rspec_tab.templates.should eq(["all"])
75+
end
76+
77+
it "converts #templates to array if it's not an array already" do
78+
rspec_tab = Refinery::Pages::Tab.register do |tab|
79+
tab.name = "rspec"
80+
tab.partial = "rspec"
81+
tab.templates = "rspec"
82+
end
83+
84+
rspec_tab.templates.should eq(["rspec"])
85+
end
86+
end
87+
end
88+
end
89+
end

0 commit comments

Comments
 (0)