administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Allow the specification of resource class names for associative fields

Open orenyk opened this issue 4 years ago • 3 comments

  • What would you like to be able to do? Can you provide some examples?
    • I would like to create a HasMany field for an association whose name is not the same as the associated class name, e.g.
      class Foo < ApplicationRecord
        has_many :foo_bars
        has_many :pubs, through: :foo_bars, source: :bar
      end
      
      class FooBar < ApplicationRecord
        belongs_to :foo
        belongs_to :bar
      end
      
      If I add a field pubs: Field::HasMany without specifying class_name: Bar in the FooDashboard, Administrate will look for a PubDashboard instead of the BarDashboard. As per #1633 class_name is now deprecated, but all of the field should accept the resource_class option. However, it currently isn't being used to determine the class name for (at least) the HasMany field type.
  • How could we go about implementing that?
    • Leverage the non-deprecated resource_class option in the associative fields to determine the resource class if the association has a different name.
  • Can you think of other approaches to the problem?
    • Not at the moment

Thanks!

orenyk avatar Nov 05 '20 00:11 orenyk

Thank you for reporting this issue. In theory, the correct class should be detected through reflection at Administrate::Field::Associative, specifically here:

https://github.com/thoughtbot/administrate/blob/84084235463a6761f25108382cb81c13632d2052/lib/administrate/field/associative.rb#L14-L16

Would you be able to have a look there and see what's going on?

pablobm avatar Nov 05 '20 11:11 pablobm

Yeah let me do a little more digging and follow up. Thanks!

orenyk avatar Nov 05 '20 14:11 orenyk

I'm seeing this too, on a has_one with a class_name: option. When we test reflection(resource_class, attr).class_name from the console we do get the expected class name.

I believe the issue has to do something with class load order – since we're declaring those attributes as statics on the dashboard class, they're getting loaded before the AR models are completed initialized. I'll try to make some time to dig into this deeper.

cyu avatar Feb 26 '21 15:02 cyu

I think this should be fixed by https://github.com/thoughtbot/administrate/pull/2292

pablobm avatar Apr 06 '23 14:04 pablobm