-
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 identifiers to be defined with a block #110
Merged
mcclayton
merged 1 commit into
procore-oss:master
from
hugopeixoto:feature/pass-a-block-to-identifier
Oct 26, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ class Base | |
# @param name [Symbol] to rename the identifier key in the JSON | ||
# output. Defaults to method given. | ||
# @param extractor [AssociationExtractor,AutoExtractor,BlockExtractor,HashExtractor,PublicSendExtractor] | ||
# @yield [object, options] The object and the options passed to render are | ||
# also yielded to the block. | ||
# | ||
# Kind of extractor to use. | ||
# Either define your own or use Blueprinter's premade extractors. | ||
# Defaults to AutoExtractor | ||
|
@@ -36,13 +39,22 @@ class Base | |
# # other code | ||
# end | ||
# | ||
# @example Passing a block to be evaluated as the value. | ||
# class UserBlueprint < Blueprinter::Base | ||
# identifier :uuid do |user, options| | ||
# options[:current_user].anonymize(user.uuid) | ||
# end | ||
# end | ||
# | ||
# @return [Field] A Field object | ||
def self.identifier(method, name: method, extractor: AutoExtractor.new) | ||
view_collection[:identifier] << Field.new(method, name, extractor, self) | ||
end | ||
|
||
def self.inherited(subclass) | ||
subclass.send(:view_collection).inherit(view_collection) | ||
def self.identifier(method, name: method, extractor: AutoExtractor.new, &block) | ||
view_collection[:identifier] << Field.new( | ||
method, | ||
name, | ||
extractor, | ||
self, | ||
block: block, | ||
) | ||
end | ||
|
||
# Specify a field or method name to be included for serialization. | ||
|
@@ -98,16 +110,13 @@ def self.inherited(subclass) | |
# | ||
# @return [Field] A Field object | ||
def self.field(method, options = {}, &block) | ||
options = if block_given? | ||
{name: method, extractor: BlockExtractor.new, block: block} | ||
else | ||
{name: method, extractor: AutoExtractor.new} | ||
end.merge(options) | ||
current_view << Field.new(method, | ||
options[:name], | ||
options[:extractor], | ||
self, | ||
options) | ||
current_view << Field.new( | ||
method, | ||
options.fetch(:name) { method }, | ||
options.fetch(:extractor) { AutoExtractor.new }, | ||
self, | ||
options.merge(block: block), | ||
) | ||
end | ||
|
||
# Specify an associated object to be included for serialization. | ||
|
@@ -141,19 +150,12 @@ def self.field(method, options = {}, &block) | |
# @return [Field] A Field object | ||
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) | ||
end | ||
|
||
current_view << Field.new(method, | ||
name, | ||
AssociationExtractor.new, | ||
self, | ||
options.merge(association: true)) | ||
field( | ||
method, | ||
options.merge(association: true, extractor: AssociationExtractor.new), | ||
&block | ||
) | ||
end | ||
|
||
# Generates a JSON formatted String. | ||
|
@@ -313,6 +315,11 @@ def self.view(view_name) | |
|
||
private | ||
|
||
def self.inherited(subclass) | ||
subclass.send(:view_collection).inherit(view_collection) | ||
end | ||
private_class_method :inherited | ||
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. Great catch! |
||
|
||
def self.object_to_hash(object, view_name:, local_options:) | ||
view_collection.fields_for(view_name).each_with_object({}) do |field, hash| | ||
next if field.skip?(object, local_options) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would you mind adding to the method documentation for this and adding it to the README similar to how you did for the associations?
https://github.com/procore/blueprinter/blob/f849049fedf7adea5f13d11b2514b6c8b54f7efa/lib/blueprinter/base.rb#L124-L139
https://github.com/procore/blueprinter/blob/master/README.md#defining-an-association-directly-in-the-blueprint
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.
👍