Add global default field/association value configuration.#128
Add global default field/association value configuration.#128
Conversation
| private | ||
|
|
||
| def default_value(association_options) | ||
| association_options.key?(:default) ? association_options.fetch(:default) : Blueprinter.configuration.association_default |
There was a problem hiding this comment.
Rather than this ternary, I would prefer something like
| association_options.key?(:default) ? association_options.fetch(:default) : Blueprinter.configuration.association_default | |
| assosiation_options.fetch(:default, Blueprinter.configuration.association_default) |
What do you think?
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
Could we switch this example to be nil like the one we discussed above. Or alternatively add another test specifically for that functionality.
There was a problem hiding this comment.
Yeah that's a great suggestion. I'll add another test case for that 👍
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
Field-level/Associaion-level Setting
The default value specified at the field/association level will override the value specified in the global config.