Skip to content

Accept an option 'default' to allow setting default serialized value for association if null#78

Merged
philipqnguyen merged 3 commits intoprocore-oss:masterfrom
vinaya-procore:vs/association-default-value
May 15, 2018
Merged

Accept an option 'default' to allow setting default serialized value for association if null#78
philipqnguyen merged 3 commits intoprocore-oss:masterfrom
vinaya-procore:vs/association-default-value

Conversation

@vinaya-procore
Copy link
Copy Markdown
Contributor

Included in the PR

For models that have belongs_to association that allow null relationship, blueprinter serializes to null by default. This PR adds an ability to add a default option to blueprinter association which can be used as the serialized value instead of null.

In cases when the association company is nil, the behavior looks as follows:

Before

class UserBlueprint < Blueprinter::Base
    identifier :uuid
    association :company, blueprint: CompanyBlueprint
end

UserBlueprint.render(user) renders { "id": 1, "company": null }

After

class UserBlueprint < Blueprinter::Base
    identifier :uuid
    association :company, blueprint: CompanyBlueprint, default: {}
end

UserBlueprint.render(user) renders { "id": 1, "company": {} }

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.

Looks good to me! Since it's a new feature, would you also bump version.rb to 0.5.0 and update the changelog.md as well?

@philipqnguyen philipqnguyen merged commit 1c9a568 into procore-oss:master May 15, 2018
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.

5 participants