forked from activeadmin/activeadmin
/
resource_spec.rb
253 lines (220 loc) · 7.94 KB
/
resource_spec.rb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require 'spec_helper'
require File.expand_path('config_shared_examples', File.dirname(__FILE__))
module ActiveAdmin
describe Resource do
it_should_behave_like "ActiveAdmin::Config"
before { load_defaults! }
let(:application){ ActiveAdmin::Application.new }
let(:namespace){ Namespace.new(application, :admin) }
def config(options = {})
@config ||= Resource.new(namespace, Category, options)
end
it { respond_to :resource_class }
describe "#resource_table_name" do
it "should return the resource's table name" do
config.resource_table_name.should == '"categories"'
end
context "when the :as option is given" do
it "should return the resource's table name" do
config(:as => "My Category").resource_table_name.should == '"categories"'
end
end
end
describe "controller name" do
it "should return a namespaced controller name" do
config.controller_name.should == "Admin::CategoriesController"
end
context "when non namespaced controller" do
let(:namespace){ ActiveAdmin::Namespace.new(application, :root) }
it "should return a non namespaced controller name" do
config.controller_name.should == "CategoriesController"
end
end
end
describe "#include_in_menu?" do
let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) }
subject{ resource }
context "when regular resource" do
let(:resource){ namespace.register(Post) }
it { should be_include_in_menu }
end
context "when belongs to" do
let(:resource){ namespace.register(Post){ belongs_to :author } }
it { should_not be_include_in_menu }
end
context "when belongs to optional" do
let(:resource){ namespace.register(Post){ belongs_to :author, :optional => true} }
it { should be_include_in_menu }
end
context "when menu set to false" do
let(:resource){ namespace.register(Post){ menu false } }
it { should_not be_include_in_menu }
end
end
describe "menu item name" do
it "should be the resource name when not set" do
config.menu_item_name.should == "Categories"
end
it "should be settable" do
config.menu :label => "My Label"
config.menu_item_name.should == "My Label"
end
end
describe "parent menu item name" do
it "should be nil when not set" do
config.parent_menu_item_name.should == nil
end
it "should return the name if set" do
config.tap do |c|
c.menu :parent => "Blog"
end.parent_menu_item_name.should == "Blog"
end
end
describe "menu item priority" do
it "should be 10 when not set" do
config.menu_item_priority.should == 10
end
it "should be settable" do
config.menu :priority => 2
config.menu_item_priority.should == 2
end
end
describe "menu item display if" do
it "should be a proc always returning true if not set" do
config.menu_item_display_if.should be_instance_of(Proc)
config.menu_item_display_if.call.should == true
end
it "should be settable" do
config.menu :if => proc { false }
config.menu_item_display_if.call.should == false
end
end
describe "route names" do
it "should return the route prefix" do
config.route_prefix.should == "admin"
end
it "should return the route collection path" do
config.route_collection_path.should == :admin_categories_path
end
context "when in the root namespace" do
let(:config){ application.register Category, :namespace => false}
it "should have a nil route_prefix" do
config.route_prefix.should == nil
end
end
context "when registering a plural resource" do
class ::News; def self.has_many(*); end end
let(:config){ application.register News }
it "should return the plurali route with _index" do
config.route_collection_path.should == :admin_news_index_path
end
end
end
describe "scoping" do
context "when using a block" do
before do
@resource = application.register Category do
scope_to do
"scoped"
end
end
end
it "should call the proc for the begin of association chain" do
begin_of_association_chain = @resource.controller.new.send(:begin_of_association_chain)
begin_of_association_chain.should == "scoped"
end
end
context "when using a symbol" do
before do
@resource = application.register Category do
scope_to :current_user
end
end
it "should call the method for the begin of association chain" do
controller = @resource.controller.new
controller.should_receive(:current_user).and_return(true)
begin_of_association_chain = controller.send(:begin_of_association_chain)
begin_of_association_chain.should == true
end
end
context "when not using a block or symbol" do
before do
@resource = application.register Category do
scope_to "Some string"
end
end
it "should raise and exception" do
lambda {
@resource.controller.new.send(:begin_of_association_chain)
}.should raise_error(ArgumentError)
end
end
describe "getting the method for the association chain" do
context "when a simple registration" do
before do
@resource = application.register Category do
scope_to :current_user
end
end
it "should return the pluralized collection name" do
@resource.controller.new.send(:method_for_association_chain).should == :categories
end
end
context "when passing in the method as an option" do
before do
@resource = application.register Category do
scope_to :current_user, :association_method => :blog_categories
end
end
it "should return the method from the option" do
@resource.controller.new.send(:method_for_association_chain).should == :blog_categories
end
end
end
end
describe "sort order" do
context "when resource class responds to primary_key" do
it "should sort by primary key desc by default" do
MockResource.should_receive(:primary_key).and_return("pk")
config = Resource.new(namespace, MockResource)
config.sort_order.should == "pk_desc"
end
end
context "when resource class does not respond to primary_key" do
it "should default to id" do
config = Resource.new(namespace, MockResource)
config.sort_order.should == "id_desc"
end
end
it "should be set-able" do
config.sort_order = "task_id_desc"
config.sort_order.should == "task_id_desc"
end
end
describe "adding a scope" do
it "should add a scope" do
config.scope :published
config.scopes.first.should be_a(ActiveAdmin::Scope)
config.scopes.first.name.should == "Published"
end
it "should retrive a scope by its id" do
config.scope :published
config.get_scope_by_id(:published).name.should == "Published"
end
end
describe "#csv_builder" do
context "when no csv builder set" do
it "should return a default column builder with id and content columns" do
config.csv_builder.columns.size.should == Category.content_columns.size + 1
end
end
context "when csv builder set" do
it "shuld return the csv_builder we set" do
csv_builder = CSVBuilder.new
config.csv_builder = csv_builder
config.csv_builder.should == csv_builder
end
end
end
end
end