administrate icon indicating copy to clipboard operation
administrate copied to clipboard

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

Open Nitr opened this issue 1 year ago • 0 comments

https://github.com/thoughtbot/administrate/issues/2459

Sometimes need to do a query dependent on resource's state, field and etc in forms.

I used to use combination of Field::Select for form page and Field::BelongsTo for collection page like this:

ATTRIBUTE_TYPES = {
  customer_id: Field::Select.with_option(collection: ->(field) { Customer.some_scope.where(column: field.resource.column) }), 
  customer: Field::BelongsTo
}
COLLECTION_ATTRIBUTES = [:customer]
FORM_ATTRIBUTES = [:customer_id]

But I've noticed that many people are confused by this and that it's more common to have only one field customer like this:

ATTRIBUTE_TYPES = { 
  customer: Field::BelongsTo.with_option(scope: ->(field) { Customer.some_scope.where(column: field.resource.column) }), 
}
COLLECTION_ATTRIBUTES = [:customer]
FORM_ATTRIBUTES = [:customer]

I've just found this gem but it doesn't check arity.

Nitr avatar Aug 27 '24 05:08 Nitr