|
289 | 289 | end |
290 | 290 | end |
291 | 291 | end |
| 292 | + |
| 293 | + describe "#associated_resource_options" do |
| 294 | + context "with `order` option" do |
| 295 | + it "returns the resources in correct order" do |
| 296 | + order = create(:order) |
| 297 | + create_list(:customer, 5) |
| 298 | + options = {order: "name"} |
| 299 | + association = Administrate::Field::HasMany.with_options(options) |
| 300 | + |
| 301 | + field = association.new(:customer, [], :show, resource: order) |
| 302 | + |
| 303 | + correct_order = Customer.order("name").pluck(:id) |
| 304 | + |
| 305 | + resources = field.associated_resource_options.compact.to_h.values |
| 306 | + expect(resources).to eq correct_order |
| 307 | + end |
| 308 | + |
| 309 | + it "ignores the order passed in `scope`" do |
| 310 | + order = create(:order) |
| 311 | + create_list(:customer, 3) |
| 312 | + options = { |
| 313 | + order: "name", |
| 314 | + scope: ->(_field) { Customer.order(name: :desc) } |
| 315 | + } |
| 316 | + association = Administrate::Field::HasMany.with_options(options) |
| 317 | + |
| 318 | + field = association.new(:customer, [], :show, resource: order) |
| 319 | + |
| 320 | + correct_order = Customer.order("name").pluck(:id) |
| 321 | + |
| 322 | + resources = field.associated_resource_options.compact.to_h.values |
| 323 | + expect(resources).to eq correct_order |
| 324 | + end |
| 325 | + end |
| 326 | + |
| 327 | + context "with `scope` option" do |
| 328 | + it "returns the resources within the passed scope" do |
| 329 | + # Building instead of creating, to avoid a dependent customer being |
| 330 | + # created, leading to random failures |
| 331 | + order = build(:order) |
| 332 | + |
| 333 | + 1.upto(3) { |i| create :customer, name: "customer-#{i}" } |
| 334 | + scope = ->(_field) { Customer.order(name: :desc).limit(2) } |
| 335 | + |
| 336 | + association = Administrate::Field::HasMany.with_options(scope: scope) |
| 337 | + field = association.new(:customer, [], :show, resource: order) |
| 338 | + resources = field.associated_resource_options.compact.to_h.keys |
| 339 | + |
| 340 | + expect(resources).to eq ["customer-3", "customer-2"] |
| 341 | + end |
| 342 | + end |
| 343 | + end |
292 | 344 | end |
0 commit comments