-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
belongs_to_spec.rb
303 lines (262 loc) · 8.84 KB
/
belongs_to_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
299
300
301
302
303
require "rails_helper"
require "administrate/field/belongs_to"
require "support/constant_helpers"
require "support/field_matchers"
describe Administrate::Field::BelongsTo do
include FieldMatchers
it do
should_permit_param(
"country_code",
on_model: Customer,
for_attribute: :territory,
)
end
describe "#to_partial_path" do
it "returns a partial based on the page being rendered" do
page = :show
owner = double
field = Administrate::Field::BelongsTo.new(:owner, owner, page)
path = field.to_partial_path
expect(path).to eq("/fields/belongs_to/#{page}")
end
end
describe "#associated_class" do
it "is automatically resolved for a conventional association" do
line_item = create(:line_item)
field = Administrate::Field::BelongsTo.new(
:product,
nil,
:show,
resource: line_item,
)
expect(field.associated_class).to eq(Product)
end
it "is automatically resolved for a custom association" do
customer = create(:customer)
field = Administrate::Field::BelongsTo.new(
:territory,
nil,
:show,
resource: customer,
)
expect(field.associated_class).to eq(Country)
end
end
describe "#display_associated_resource" do
it "renders the associated resource for a conventional association" do
product = create(:product, name: "Associated Product")
line_item = create(:line_item, product: product)
field = Administrate::Field::BelongsTo.new(
:product,
line_item.product,
:show,
resource: line_item,
)
allow_any_instance_of(ProductDashboard).to(
receive(:display_resource) do |_, resource|
"Mock #{resource.name}"
end,
)
expect(field.display_associated_resource).to eq("Mock Associated Product")
end
it "renders the associated resource for a custom association" do
country = create(:country, name: "Associated Country")
customer = create(:customer)
field = Administrate::Field::BelongsTo.new(
:territory,
country,
:show,
resource: customer,
)
allow_any_instance_of(CountryDashboard).to(
receive(:display_resource) do |_, resource|
"Mock #{resource.name}"
end,
)
expect(field.display_associated_resource).to eq("Mock Associated Country")
end
end
describe "class_name option" do
before do
allow(Administrate.deprecator).to receive(:warn)
end
it "determines the associated_class" do
line_item = create(:line_item)
field_class = Administrate::Field::BelongsTo.with_options(
class_name: "Customer",
)
field = field_class.new(
:product,
line_item.product,
:show,
resource: line_item,
)
expect(field.associated_class).to eq(Customer)
end
it "uses it to determine how to render the associated resource" do
product = create(:product, name: "Associated Product")
line_item = create(:line_item, product: product)
field_class = Administrate::Field::BelongsTo.with_options(
class_name: "LineItem",
)
field = field_class.new(
:product,
line_item.product,
:show,
resource: line_item,
)
expect(field.display_associated_resource).to match(
/^Line Item \#\d\d\d\d$/,
)
end
it "triggers a deprecation warning" do
line_item = create(:line_item)
field_class = Administrate::Field::BelongsTo.with_options(
class_name: "Customer",
)
field = field_class.new(
:product,
line_item.product,
:show,
resource: line_item,
)
field.associated_class
expect(Administrate.deprecator).to have_received(:warn).
with(/:class_name is deprecated/)
end
end
describe ":include_blank option" do
context "default value as true" do
it "determines if choices has blank option or not" do
customer = create(:customer, territory: nil)
association = Administrate::Field::BelongsTo
field = association.new(
:territory,
[],
:edit,
resource: customer,
)
candidates = field.associated_resource_options
expect(field.include_blank_option). to eq(true)
expect(candidates).to eq([])
end
end
context "set value as false" do
it "determines if choices has blank option or not" do
customer = create(:customer, territory: nil)
association = Administrate::Field::BelongsTo.with_options(
include_blank: false,
)
field = association.new(
:territory,
[],
:edit,
resource: customer,
)
candidates = field.associated_resource_options
expect(field.include_blank_option). to eq(false)
expect(candidates).to eq([])
end
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 associated table key that matches our foreign key" do
association =
Administrate::Field::BelongsTo.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::BelongsTo.with_options(
primary_key: "uuid",
)
field = association.new(:foo, double(uuid: nil), :baz)
field.selected_option
expect(Administrate.deprecator).to have_received(:warn).
with(/:primary_key is deprecated/)
end
end
describe "foreign_key option" do
before do
allow(Administrate.deprecator).to receive(:warn)
end
it "determines what foreign key is used on the relationship for the form" do
association = Administrate::Field::BelongsTo.with_options(
foreign_key: "foo_uuid", class_name: "Foo",
)
field = association.new(:customers, [], :show)
permitted_attribute = field.permitted_attribute
expect(permitted_attribute).to eq("foo_uuid")
end
it "triggers a deprecation warning" do
association = Administrate::Field::BelongsTo.with_options(
foreign_key: "foo_uuid", class_name: "Foo",
)
field = association.new(:customers, [], :show)
field.permitted_attribute
expect(Administrate.deprecator).to have_received(:warn).
with(/:foreign_key is deprecated/)
end
end
describe "#associated_resource_options" do
context "with `order` option" do
it "returns the resources in correct order" do
order = create(:order)
create_list(:customer, 5)
options = { order: "name" }
association = Administrate::Field::BelongsTo.with_options(options)
field = association.new(:customer, [], :show, resource: order)
correct_order = Customer.order("name").pluck(:id)
resources = field.associated_resource_options.compact.to_h.values
expect(resources).to eq correct_order
end
it "ignores the order passed in `scope`" do
order = create(:order)
create_list(:customer, 3)
options = {
order: "name",
scope: -> { Customer.order(name: :desc) },
}
association = Administrate::Field::BelongsTo.with_options(options)
field = association.new(:customer, [], :show, resource: order)
correct_order = Customer.order("name").pluck(:id)
resources = field.associated_resource_options.compact.to_h.values
expect(resources).to eq correct_order
end
end
context "with `scope` option" do
it "returns the resources within the passed scope" do
# Building instead of creating, to avoid a dependent customer being
# created, leading to random failures
order = build(:order)
1.upto(3) { |i| create :customer, name: "customer-#{i}" }
scope = -> { Customer.order(name: :desc).limit(2) }
association = Administrate::Field::BelongsTo.with_options(scope: scope)
field = association.new(:customer, [], :show, resource: order)
resources = field.associated_resource_options.compact.to_h.keys
expect(resources).to eq ["customer-3", "customer-2"]
end
end
end
end