-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow associations to be defined with a block #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ class Base | |
# end | ||
# | ||
# @return [Field] A Field object | ||
def self.identifier(method, name: method, extractor: AutoExtractor) | ||
def self.identifier(method, name: method, extractor: AutoExtractor.new) | ||
view_collection[:identifier] << Field.new(method, name, extractor, self) | ||
end | ||
|
||
|
@@ -97,9 +97,9 @@ def self.inherited(subclass) | |
# @return [Field] A Field object | ||
def self.field(method, options = {}, &block) | ||
options = if block_given? | ||
{name: method, extractor: BlockExtractor, block: {method => block}} | ||
{name: method, extractor: BlockExtractor.new, block: block} | ||
else | ||
{name: method, extractor: AutoExtractor} | ||
{name: method, extractor: AutoExtractor.new} | ||
end.merge(options) | ||
current_view << Field.new(method, | ||
options[:name], | ||
|
@@ -119,6 +119,8 @@ def self.field(method, options = {}, &block) | |
# JSON output. | ||
# @option options [Symbol] :view Specify the view to use or fall back to | ||
# to the :default view. | ||
# @yield [Object] The object passed to `render` is also passed to the | ||
# block. | ||
# | ||
# @example Specifying an association | ||
# class UserBlueprint < Blueprinter::Base | ||
|
@@ -127,15 +129,29 @@ def self.field(method, options = {}, &block) | |
# # code | ||
# end | ||
# | ||
# @example Passing a block to be evaluated as the value. | ||
# class UserBlueprint < Blueprinter::Base | ||
# association :vehicles, blueprint: VehiclesBlueprint do |user| | ||
# user.vehicles + user.company.vehicles | ||
# end | ||
# end | ||
# | ||
# @return [Field] A Field object | ||
def self.association(method, options = {}) | ||
def self.association(method, options = {}, &block) | ||
raise BlueprinterError, 'blueprint required' unless options[:blueprint] | ||
name = options.delete(:name) || method | ||
|
||
options = if block_given? | ||
options.merge(extractor: BlockExtractor.new, block: block) | ||
else | ||
options.merge(extractor: AutoExtractor.new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're already instantiating these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing https://github.com/procore/blueprinter/blob/master/lib/blueprinter/extractor.rb#L11-L13 This may be a breaking change, so I'm not sure if we want to change this here. Maybe I should just apply the other changes you mentioned but leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't realize there were tests for that. OK let's leave that for now. Thanks! |
||
end | ||
|
||
current_view << Field.new(method, | ||
name, | ||
AssociationExtractor, | ||
self, | ||
options.merge(association: true)) | ||
name, | ||
AssociationExtractor.new, | ||
self, | ||
options.merge(association: true)) | ||
end | ||
|
||
# Generates a JSON formatted String. | ||
|
@@ -220,7 +236,7 @@ def self.prepare(object, view_name:, local_options:) | |
# @return [Array<Symbol>] an array of field names | ||
def self.fields(*field_names) | ||
field_names.each do |field_name| | ||
current_view << Field.new(field_name, field_name, AutoExtractor, self) | ||
current_view << Field.new(field_name, field_name, AutoExtractor.new, self) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Blueprinter | ||
class BlockExtractor < Extractor | ||
def extract(field_name, object, local_options, options = {}) | ||
options[:block][field_name].call(object, local_options) | ||
options[:block].call(object, local_options) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you've seen my library 😂