forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnamespace_spec.rb
More file actions
214 lines (183 loc) · 6.27 KB
/
namespace_spec.rb
File metadata and controls
214 lines (183 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'spec_helper'
describe ActiveAdmin::Namespace do
let(:application){ ActiveAdmin::Application.new }
context "when new" do
let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) }
it "should have an application instance" do
namespace.application.should == application
end
it "should have a name" do
namespace.name.should == :admin
end
it "should have no resources" do
namespace.resources.should == {}
end
it "should have an empty menu" do
namespace.menu.items.should be_empty
end
end
describe "registering a resource" do
let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) }
context "with no configuration" do
before do
namespace.register Category
end
it "should store the namespaced registered configuration" do
namespace.resources.keys.should include('Category')
end
it "should create a new controller in the default namespace" do
defined?(Admin::CategoriesController).should be_true
end
it "should create the dashboard controller" do
defined?(Admin::DashboardController).should be_true
end
it "should create a menu item" do
namespace.load_menu!
namespace.menu["Categories"].should be_an_instance_of(ActiveAdmin::MenuItem)
namespace.menu["Categories"].url.should == "/admin/categories"
end
end
context "with a block configuration" do
it "should be evaluated in the dsl" do
lambda {
namespace.register Category do
raise "Hello World"
end
}.should raise_error
end
end
context "with a resource that's namespaced" do
before do
module ::Mock; class Resource; def self.has_many(arg1, arg2); end; end; end
namespace.register Mock::Resource
end
it "should store the namespaced registered configuration" do
namespace.resources.keys.should include('MockResource')
end
it "should create a new controller in the default namespace" do
defined?(Admin::MockResourcesController).should be_true
end
it "should create a menu item" do
namespace.load_menu!
namespace.menu["Mock Resources"].should be_an_instance_of(ActiveAdmin::MenuItem)
end
it "should use the resource as the model in the controller" do
Admin::MockResourcesController.resource_class.should == Mock::Resource
end
end
describe "finding resource instances" do
let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) }
it "should return the resource when its been registered" do
post = namespace.register Post
namespace.resource_for(Post).should == post
end
it 'should return nil when the resource has not been registered' do
namespace.resource_for(Post).should == nil
end
it "should return the parent when the parent class has been registered and the child has not" do
user = namespace.register User
namespace.resource_for(Publisher).should == user
end
it "should return the resource if it and it's parent were registered" do
user = namespace.register User
publisher = namespace.register Publisher
namespace.resource_for(Publisher).should == publisher
end
end
describe "adding to the menu" do
describe "adding as a top level item" do
before do
namespace.register Category
namespace.load_menu!
end
it "should add a new menu item" do
namespace.menu['Categories'].should_not be_nil
end
end
describe "adding as a child" do
before do
namespace.register Category do
menu :parent => 'Blog'
end
namespace.load_menu!
end
it "should generate the parent menu item" do
namespace.menu['Blog'].should_not be_nil
end
it "should generate its own child item" do
namespace.menu['Blog']['Categories'].should_not be_nil
end
end
describe "disabling the menu" do
before do
namespace.register Category do
menu false
end
namespace.load_menu!
end
it "should not create a menu item" do
namespace.menu["Categories"].should be_nil
end
end
describe "setting menu priority" do
before do
namespace.register Category do
menu :priority => 2
end
namespace.load_menu!
end
it "should have a custom priority of 2" do
namespace.menu["Categories"].priority.should == 2
end
end
describe "setting a condition for displaying" do
before do
namespace.register Category do
menu :if => proc { false }
end
namespace.load_menu!
end
it "should have a proc returning false" do
namespace.menu["Categories"].display_if_block.should be_instance_of(Proc)
namespace.menu["Categories"].display_if_block.call.should == false
end
end
describe "adding as a belongs to" do
context "when not optional" do
before do
namespace.register Post do
belongs_to :author
end
end
it "should not show up in the menu" do
namespace.menu["Posts"].should be_nil
end
end
context "when optional" do
before do
namespace.register Post do
belongs_to :author, :optional => true
end
end
it "should show up in the menu" do
namespace.menu["Posts"].should_not be_nil
end
end
end
end
describe "dashboard controller name" do
context "when namespaced" do
it "should be namespaced" do
namespace = ActiveAdmin::Namespace.new(application, :admin)
namespace.dashboard_controller_name.should == "Admin::DashboardController"
end
end
context "when not namespaced" do
it "should not be namespaced" do
namespace = ActiveAdmin::Namespace.new(application, :root)
namespace.dashboard_controller_name.should == "DashboardController"
end
end
end
end
end