thoughtbot / administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
http://administrate-demo.herokuapp.com
MIT License
5.88k stars 1.11k forks source link

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

Open Nitr opened 1 month ago

Nitr commented 1 month ago

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.

nickcharlton commented 5 days ago

I just let the test suite run, could you take a look at those failures when you get a chance?

Nitr commented 5 days ago

I just let the test suite run, could you take a look at those failures when you get a chance?

@nickcharlton Yeah! It's done! Could you please run it again

Nitr commented 4 days ago

I've just sync again with main branch in hope it helps. And my local bundle-audit telling me No vulnerabilities found. If it fails again do I need to update webrick in this PR or separately? Or will it be done by dependabot soon? @nickcharlton

nickcharlton commented 4 days ago

I was surprised it wasn't caught already by Dependabot, but I've not looked into it. I'm generally comfortable ignoring that check as long as we know why it's failing, though so it won't hold this back.