Skip to content

Ability to preload on an association by association basis #16

@minglecm

Description

@minglecm

The ability to specify preloads on an association would be a good addition to Brainstem.

These preloads would only be added when a specific association is requested (via includes) instead of all the time in the case of custom_preload.

Behavior around preloading multiple times (IE globally preloading [:workspace] and then preloading workspace in an association) would need to be examined.

class StoryPresenter < Brainstem::Presenter
  presents "Story"

  # This will still be used to globally load associations on all models.
  def custom_preload(models, associations = [])
    ActiveRecord::Associations::Preloader.new(models, [:workspace]).run
  end

  def present(story)
    {
      :implicit_preload   => association(:story) # preloads story implicitly based on method name
      :explicit_preload   => association(:blah, :preloads => [:story]) # will use :story instead of :blah
      :explicit_preload2  => association(:json_name => "time", :preloads => [:line_items]) { |model| model }
      :empty_preload      => association(:json_name => "test") { |model| model } # doesn't preload anything
    }
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions