Skip to content

Add field/association level default_if option#191

Merged
mcclayton merged 10 commits intomasterfrom
emptyValueSupport
Dec 19, 2019
Merged

Add field/association level default_if option#191
mcclayton merged 10 commits intomasterfrom
emptyValueSupport

Conversation

@mcclayton
Copy link
Copy Markdown
Contributor

@mcclayton mcclayton commented Dec 19, 2019

Sometimes, you may want certain "empty" values to pass through to the default value other than nil.
This PR gives Blueprinter the ability to treat the following empty types as the default value (or nil if no default provided).

Blueprinter::EMPTY_COLLECTION

An empty array or empty active record collection.

Blueprinter::EMPTY_HASH

An empty hash.

Blueprinter::EMPTY_STRING

An empty string or symbol.

Field-level/Association-level Setting

class UserBlueprint < Blueprinter::Base
  identifier :uuid

  view :normal do
    # If first_name is an empty string, it will become "N/A"
    field :first_name, default_if: Blueprinter::EmptyString, default: "N/A"
    # If the projects association collection is empty, it will become nil
    association :projects, blueprint: ProjectBlueprint, default_if: Blueprinter::EmptyCollection
  end
end

Comment thread lib/blueprinter/empty_types.rb Outdated
EMPTY_HASH = "empty_hash".freeze
EMPTY_STRING = "empty_string".freeze

module EmptyTypeHelper
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

wondering if this module name should be consistent with the file name empty_types.rb or vice versa?

Copy link
Copy Markdown

@wai-procore wai-procore left a comment

Choose a reason for hiding this comment

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

one NB comment about the filepath vs module name discrepancy, otherwise lgtm

Comment thread lib/blueprinter/version.rb Outdated
@@ -1,3 +1,3 @@
module Blueprinter
VERSION = '0.20.0'.freeze
VERSION = '0.20.1'.freeze
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is new feature. What do you think if we make it 0.21.0?

Also need update to CHANGELOG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Of course, great catch!

Comment thread CHANGELOG.md
@@ -1,3 +1,6 @@
## 0.21.0 - 2019/12/19
* 🚀 [FEATURE] Ability to specify `default_if` field/association option for more control on when the default value is applied. [191](https://github.com/procore/blueprinter/pull/191). Thanks to [@mcclayton](https://github.com/mcclayton).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the vanity =D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just following the template 😅

@mcclayton mcclayton merged commit 673aeb5 into master Dec 19, 2019
@mcclayton mcclayton deleted the emptyValueSupport branch December 19, 2019 18:31
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