Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scope to accept field as argument for Field::BelongsTo #2652

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ the table views and in the dropdown menu on the record forms.
You can set multiple columns as well with direction. E.g.: `"name, email DESC"`.

`:scope` - Specifies a custom scope inside a callable. Useful for preloading.
Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
Example #1: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
Example #2: `.with_options(scope: -> (field) { field.resource.my_models.includes(:rel).limit(5) })`

`:include_blank` - Specifies if the select element to be rendered should include
blank option. Default is `true`.
Expand Down Expand Up @@ -113,7 +114,7 @@ association `belongs_to :country`, from your model.

**Field::HasMany**

`:collection_attributes` - Set the columns to display in the show view.
`:collection_attributes` - Set the columns to display in the show view.
Default is COLLECTION_ATTRIBUTES in dashboard.

`:limit` - The number of resources (paginated) to display in the show view. To disable pagination,
Expand Down
7 changes: 6 additions & 1 deletion lib/administrate/field/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def include_blank_option
private

def candidate_resources
scope = options[:scope] ? options[:scope].call : associated_class.all
scope =
if options[:scope]
options[:scope].arity.positive? ? options[:scope].call(self) : options[:scope].call
else
associated_class.all
end

order = options.delete(:order)
order ? scope.reorder(order) : scope
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@

expect(resources).to eq ["customer-3", "customer-2"]
end

context "when scope with argument" 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 = ->(_field) { 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
end
Loading