Skip to content

Add global default field/association value configuration.#128

Merged
mcclayton merged 4 commits intomasterfrom
globalDefaultValues
Jan 24, 2019
Merged

Add global default field/association value configuration.#128
mcclayton merged 4 commits intomasterfrom
globalDefaultValues

Conversation

@mcclayton
Copy link
Copy Markdown
Contributor

Previously we only supported being able to set default values at the field/association level. This PR enables setting a global default for all fields/associations that evaluate to nil.

Global Config Setting

Blueprinter.configure do |config|
  config.field_default = "N/A"
  config.association_default = {}
end

Field-level/Associaion-level Setting

class UserBlueprint < Blueprinter::Base
  identifier :uuid

  view :normal do
    field :first_name, default: "N/A"
    association :company, blueprint: CompanyBlueprint, default: {}
  end
end

The default value specified at the field/association level will override the value specified in the global config.

private

def default_value(association_options)
association_options.key?(:default) ? association_options.fetch(:default) : Blueprinter.configuration.association_default
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.

Rather than this ternary, I would prefer something like

Suggested change
association_options.key?(:default) ? association_options.fetch(:default) : Blueprinter.configuration.association_default
assosiation_options.fetch(:default, Blueprinter.configuration.association_default)

What do you think?

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.

I think that makes sense to me, but I avoided this initially, because I literally wanted to do a check on whether the :default key was present in the options in case it was explicitly set to nil, we'd want to actually render nil instead of pass through to the global Blueprinter default.
Make sense? I don't know if that's an important enough behavior to preserve or not. Thoughts? 🤔

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.

Yeah that totally makes sense. I could see someone switching it up like that if only one or two endpoints ignore the default and use nil instead.

let(:blueprint) do
Class.new(Blueprinter::Base) do
field :id
field :first_name, default: "Unknown"
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.

Could we switch this example to be nil like the one we discussed above. Or alternatively add another test specifically for that functionality.

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.

Yeah that's a great suggestion. I'll add another test case for that 👍

@mcclayton mcclayton changed the base branch from globalIfUnlessProcs to master January 18, 2019 23:20
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.

lgtm

@mcclayton mcclayton merged commit 64546aa into master Jan 24, 2019
@mcclayton mcclayton deleted the globalDefaultValues branch January 24, 2019 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants