procore-oss / blueprinter-activerecord

MIT License
9 stars 3 forks source link

[Feature Request] Support for "draper" gem / Decorator? #52

Open jamesst20 opened 2 weeks ago

jamesst20 commented 2 weeks ago

Hi,

I noticed that no preloading happens if I have decorated records when using Draper gem.

Unfortunately it doesn't seem as simple as changing the condition here https://github.com/procore-oss/blueprinter-activerecord/blob/main/lib/blueprinter-activerecord/preloader.rb#L41 from

-if object.is_a?(ActiveRecord::Relation) && !object.loaded?
+if (object.is_a?(ActiveRecord::Relation) || object.is_a?(ActiveRecord::Relation)) && !(object.try(:object) || object).loaded?

It still will not preload the records.

I tried preloading prior decorating as well without success:

records = records.preload_blueprint.decorate
MyBlueprint.render_as_hash(records)

It would be great if this gem could be supported somehow. Thanks in advance

jhollinger commented 6 days ago

If you want to preload prior, you have include the blueprint (and view name, if it's not :default). The following should work:

records = records
  .preload_blueprint(MyBlueprint, :view_name)
  .decorate