Skip to content
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

Conversation

hugopeixoto
Copy link
Contributor

@hugopeixoto hugopeixoto commented Oct 15, 2018

Addresses #90.

I removed an extra hash from BlockExtractor. Not sure if should have, but it seemed to be redundant, and tests are passing.

I changed AssociationExtractor so that it accepts a nested extractor. This allowed me to reuse AutoExtractor and BlockExtractor, making #association now very similar to #field.

I noticed that we're passing classes as extractors, and that the class method extract always creates a new instance of itself. This will probably create an unnecessary number of objects. Instantiating them directly in #field and #association might help improve performance. I addressed that in this PR as well.

else
options.merge(extractor: AutoExtractor)
options.merge(extractor: AutoExtractor.new)
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
makes this break: https://github.com/procore/blueprinter/blob/master/spec/integrations/shared/base_render_examples.rb#L41

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 self.extract?

Copy link
Contributor

Choose a reason for hiding this comment

The 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!

self,
options.merge(association: true))
name,
AssociationExtractor.new(options[:extractor]),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need to pass the options[:extractor] here, the AssociationExtractor should already be passed the options from Field https://github.com/procore/blueprinter/blame/master/lib/blueprinter/field.rb#L13

def extract(association_name, object, local_options, options={})
value = object.public_send(association_name)
return (value || options[:default]) if value.nil?
value = @field_extractor.extract(association_name, object, local_options, options)
Copy link
Contributor

@philipqnguyen philipqnguyen Oct 16, 2018

Choose a reason for hiding this comment

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

you should be able to do options[:extractor].extract( .... )

@philipqnguyen
Copy link
Contributor

Would you also add this feature to the README and add the method documentation above def self.association? Similar to how it's documented for field blocks here https://github.com/procore/blueprinter/blob/master/lib/blueprinter/base.rb#L67-L80

@hugopeixoto hugopeixoto force-pushed the feature/pass-a-block-to-an-association branch from 14218f7 to 5073953 Compare October 17, 2018 14:50
{
"uuid": "733f0758-8f21-4719-875f-262c3ec743af",
"projects": [
{"uuid": "b426a1e6-ac41-45ab-bfef-970b9a0b4289", "name": "query-console"},
Copy link
Contributor

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 😂

@philipqnguyen philipqnguyen merged commit 32804d7 into procore-oss:master Oct 17, 2018
@philipqnguyen
Copy link
Contributor

released with 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants