Skip to content

Raise errors for invalid blueprints for associations#233

Merged
philipqnguyen merged 8 commits intoprocore-oss:masterfrom
caws:raise-errors-for-invalid-blueprints
Aug 11, 2020
Merged

Raise errors for invalid blueprints for associations#233
philipqnguyen merged 8 commits intoprocore-oss:masterfrom
caws:raise-errors-for-invalid-blueprints

Conversation

@caws
Copy link
Copy Markdown
Contributor

@caws caws commented Aug 8, 2020

This PR validates that a given Blueprint class passed for an association is valid and inherits from Blueprinter::Base.

The issue.

Take the following Blueprinter.

class PaymentBlueprinter < Blueprinter::Base
    identifier :id
    view :estimate do
      association :client_taxes, blueprint: Client, view: :estimate
     # or 
     # association :client_taxes, blueprint: {}, view: :estimate
    end
end

This raises undefined method `prepare' for #<Class:0x00005649611cdb70> Did you mean? prepend when this line is interpreted.

The fix:

Add logic to raise errors if Blueprinter::Base is not present in the ancestor list of the class provided as the Blueprinter for a given association.

My thoughts:

The undefined method error is not intuitive and does offer any hint as to what's going on to the programmer. As an extra I also added a check to see if the provided Blueprint class has any ancestors at all.

caws added 3 commits August 8, 2020 15:18
…he Blueprinter for a given association. Add spec to cover the case when an object that does not have any ancestors is passed as the blueprint.
…nt for an association has ancestors and inherits from Blueprinter::Base. Raise errors in case a given Blueprint is not valid
@caws caws changed the title Raise errors for invalid blueprints Raise errors for invalid blueprints for associations Aug 8, 2020
Copy link
Copy Markdown
Contributor

@philipqnguyen philipqnguyen left a comment

Choose a reason for hiding this comment

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

Great idea, I have a suggestion.

Comment thread lib/blueprinter/extractors/association_extractor.rb Outdated
Comment thread lib/blueprinter/extractors/association_extractor.rb Outdated
Comment thread lib/blueprinter/helpers/base_helpers.rb Outdated
@philipqnguyen philipqnguyen merged commit f69faed into procore-oss:master Aug 11, 2020
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