-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
has_many_spec.rb
298 lines (246 loc) · 8.25 KB
/
has_many_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require "rails_helper"
require "administrate/field/has_many"
require "support/constant_helpers"
require "support/mock_relation"
describe Administrate::Field::HasMany do
describe "#to_partial_path" do
it "returns a partial based on the page being rendered" do
page = :show
items = double
field = Administrate::Field::HasMany.new(:items, items, page)
path = field.to_partial_path
expect(path).to eq("/fields/has_many/#{page}")
end
end
describe "#associated_collection" do
it "returns an index page for the dashboard of the associated attribute" do
customer = build(:customer)
field = Administrate::Field::HasMany.new(
:orders,
Order.all,
:show,
resource: customer,
)
page = field.associated_collection
expect(page).to be_instance_of(Administrate::Page::Collection)
end
end
describe "class_name option" do
let(:dashboard_double) { double(collection_attributes: []) }
before do
allow(Administrate.deprecator).to receive(:warn)
FooDashboard = Class.new
allow(FooDashboard).to receive(:new).and_return(dashboard_double)
end
after do
remove_constants :FooDashboard
end
it "determines what dashboard is used to present the association" do
association = Administrate::Field::HasMany.
with_options(class_name: "Foo")
field = association.new(:customers, [], :show)
collection = field.associated_collection
attributes = collection.attribute_names
expect(dashboard_double).to have_received(:collection_attributes)
expect(attributes).to eq([])
end
it "triggers a deprecation warning" do
association = Administrate::Field::HasMany.
with_options(class_name: "Foo")
field = association.new(:customers, [], :show)
field.associated_collection
expect(Administrate.deprecator).to have_received(:warn).
with(/:class_name is deprecated/)
end
end
describe "primary_key option" do
before do
allow(Administrate.deprecator).to receive(:warn)
Foo = Class.new
FooDashboard = Class.new
uuid = SecureRandom.uuid
allow(Foo).to receive(:all).and_return([Foo])
allow(Foo).to receive(:uuid).and_return(uuid)
allow(Foo).to receive(:id).and_return(1)
allow_any_instance_of(FooDashboard).to(
receive(:display_resource).and_return(uuid),
)
end
after do
remove_constants :Foo, :FooDashboard
end
it "is the key matching the associated foreign key" do
association =
Administrate::Field::HasMany.with_options(
primary_key: "uuid", class_name: "Foo",
)
field = association.new(:customers, [], :show)
field.associated_resource_options
expect(Foo).to have_received(:all)
expect(Foo).to have_received(:uuid)
expect(Foo).not_to have_received(:id)
end
it "triggers a deprecation warning" do
association =
Administrate::Field::HasMany.with_options(
primary_key: "uuid", class_name: "Foo",
)
field = association.new(:customers, [], :show)
field.associated_resource_options
expect(Administrate.deprecator).to have_received(:warn).
with(/:primary_key is deprecated/)
end
end
describe "#more_than_limit?" do
it "returns true if record count > limit" do
limit = Administrate::Field::HasMany::DEFAULT_LIMIT
value = MockRelation.new([:a] * (limit + 1))
association = Administrate::Field::HasMany
field = association.new(:customers, value, :show)
expect(field.more_than_limit?).to eq(true)
end
it "returns false if record count <= limit" do
limit = Administrate::Field::HasMany::DEFAULT_LIMIT
value = MockRelation.new([:a] * limit)
association = Administrate::Field::HasMany
field = association.new(:customers, value, :show)
expect(field.more_than_limit?).to eq(false)
end
context "when there are no records" do
it "returns false" do
customer = build(:customer)
value = nil
field = Administrate::Field::HasMany.new(
:orders,
value,
:show,
resource: customer,
)
expect(field.more_than_limit?).to eq(false)
end
end
end
describe "#resources" do
it "limits the number of records shown" do
limit = Administrate::Field::HasMany::DEFAULT_LIMIT
customer = create(:customer, :with_orders, order_count: 10)
value = customer.orders
field = Administrate::Field::HasMany.new(
:orders,
value,
:show,
resource: customer,
)
expect(field.resources.size).to eq(limit)
end
context "when there are no records" do
it "returns an empty collection" do
customer = build(:customer)
value = nil
field = Administrate::Field::HasMany.new(
:orders,
value,
:show,
resource: customer,
)
expect(field.resources).to eq([])
end
end
context "with `limit` option" do
it "limits the number of items returned" do
customer = create(:customer, :with_orders)
value = customer.orders
association = Administrate::Field::HasMany.with_options(limit: 1)
field = association.new(:orders, value, :show, resource: customer)
expect(field.resources).to be_one
end
end
context "with `sort_by` option" do
it "returns the resources in correct order" do
customer = create(:customer, :with_orders)
options = { sort_by: :address_line_two }
association = Administrate::Field::HasMany.with_options(options)
field = association.new(
:orders,
customer.orders,
:show,
resource: customer,
)
correct_order = customer.orders.sort_by(&:address_line_two).map(&:id)
reversed_order = customer.orders.sort do |a, b|
b.address_line_two <=> a.address_line_two
end
expect(field.resources.map(&:id)).to eq correct_order
expect(field.resources.map(&:id)).to_not eq reversed_order.map(&:id)
end
end
context "with `direction` option" do
it "returns the resources in correct order" do
customer = create(:customer, :with_orders)
options = { sort_by: :address_line_two, direction: :desc }
association = Administrate::Field::HasMany.with_options(options)
field = association.new(
:orders,
customer.orders,
:show,
resource: customer,
)
reversed_order = customer.orders.sort_by(&:address_line_two).map(&:id)
correct_order = customer.orders.sort do |a, b|
b.address_line_two <=> a.address_line_two
end
expect(field.resources.map(&:id)).to eq correct_order.map(&:id)
expect(field.resources.map(&:id)).to_not eq reversed_order
end
end
end
describe "#selected_options" do
it "returns a collection of keys to use for the association" do
associated_resource1 = double(
"AssociatedResource1",
associated_resource_key: "associated-1",
)
associated_resource2 = double(
"AssociatedResource2",
associated_resource_key: "associated-2",
)
attribute_value = MockRelation.new(
[
associated_resource1,
associated_resource2,
],
)
primary_resource = double(
"Resource",
class: double(
"ResourceClass",
reflect_on_association: double(
"ResourceReflection",
association_primary_key: "associated_resource_key",
),
),
)
association = Administrate::Field::HasMany
field = association.new(
:customers,
attribute_value,
:show,
resource: primary_resource,
)
expect(field.selected_options).to eq(["associated-1", "associated-2"])
end
context "when there are no records" do
it "returns an empty collection" do
customer = build(:customer)
value = nil
field = Administrate::Field::HasMany.new(
:orders,
value,
:show,
resource: customer,
)
expect(field.selected_options).to be_nil
end
end
end
end