From 48717eef2fad8e0fbd1d6b9b99a46d0698b83de5 Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Sat, 19 Feb 2022 19:57:12 +0100 Subject: [PATCH] fix: Follow AR options naming style ActiveModel validations usually have `:allow_nil` keyword option. Ruby method naming also follow this theme. --- lib/default_value_for.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/default_value_for.rb b/lib/default_value_for.rb index fc5b573..3c8f2a0 100644 --- a/lib/default_value_for.rb +++ b/lib/default_value_for.rb @@ -54,15 +54,22 @@ module ClassMethods # # The options can be used to specify the following things: # * value - Sets the default value. - # * allows_nil (default: true) - Sets explicitly passed nil values if option is set to true. + # * allow_nil (default: true) - Sets explicitly passed nil values if option is set to true. def default_value_for(attribute, options = {}, &block) - value = options - allows_nil = true + value = options + allow_nil = true if options.is_a?(Hash) - opts = options.stringify_keys - value = opts.fetch('value', options) - allows_nil = opts.fetch('allows_nil', true) + opts = options.stringify_keys + value = opts.fetch('value', options) + allow_nil = opts.fetch('allow_nil') do + if opts.key?('allows_nil') + # TODO: Add deprecation warning? + opts['allows_nil'] + else + true + end + end end if !method_defined?(:set_default_values) @@ -90,7 +97,7 @@ def default_value_for(attribute, options = {}, &block) container = NormalValueContainer.new(value) end _default_attribute_values[attribute.to_s] = container - _default_attribute_values_not_allowing_nil << attribute.to_s unless allows_nil + _default_attribute_values_not_allowing_nil << attribute.to_s unless allow_nil end def default_values(values)