Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/brainstem/dsl/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ def method_name
end
end

def run_on(model, context, helper_instance = Object.new)
def run_on(model, context, helper_instance = Object.new, lookup_options = {})
if options[:lookup]
run_on_with_lookup(model, context, helper_instance)
elsif options[:dynamic]
proc = options[:dynamic]
if proc.arity == 1
if proc.arity == -2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the expected association proc format takes in model and options = {}, we start with arity 2, and with one of the params not required, makes it -2.

I'm not sure about the case at the bottom with no arity - as far as I know, our documentation requires that at least a model is passed in, resulting in arity 1?

helper_instance.instance_exec(model, lookup_options, &proc)
elsif proc.arity == 1
helper_instance.instance_exec(model, &proc)
else
helper_instance.instance_exec(&proc)
Expand Down
4 changes: 3 additions & 1 deletion lib/brainstem/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ def load_associations!(model, struct, context, options)
# If this association has been explictly requested, execute the association here. Additionally, store
# the loaded models in the :load_associations_into hash for later use.
if context[:association_objects_by_name][external_name]
associated_model_or_models = association.run_on(model, context, context[:helper_instance])
lookup_options = {}
lookup_options[:firstFiftyAssociations] = true if options[:firstFiftyAssociations]
associated_model_or_models = association.run_on(model, context, context[:helper_instance], lookup_options)

if options[:load_associations_into]
Array(associated_model_or_models).flatten.each do |associated_model|
Expand Down
7 changes: 5 additions & 2 deletions lib/brainstem/presenter_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ def structure_response(presented_class, primary_models, strategy, count, options

if primary_models.length > 0
associated_models = {}
group_present_options = {optional_fields: optional_fields, load_associations_into: associated_models}
if options[:params]["firstFiftyAssociations"]
group_present_options.merge!({firstFiftyAssociations: true})
end
presented_primary_models = options[:primary_presenter].group_present(primary_models,
selected_associations.map(&:name),
optional_fields: optional_fields,
load_associations_into: associated_models)
group_present_options)

struct[brainstem_key] = presented_primary_models.each.with_object({}) { |model, obj| obj[model['id']] = model }
struct['results'] = presented_primary_models.map { |model| { 'key' => brainstem_key, 'id' => model['id'] } }
Expand Down