diff --git a/CHANGELOG.md b/CHANGELOG.md index 3210f95f..16a59764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# [5.1.1][] (2022-09-01) + +## Fixed + +- [#539][] - Fixed a caching error in default values. + # [5.1.0][] (2022-07-28) ## Added @@ -1112,6 +1118,7 @@ Example.run - Initial release. + [5.1.1]: https://github.com/AaronLasseigne/active_interaction/compare/v5.1.0...v5.1.1 [5.1.0]: https://github.com/AaronLasseigne/active_interaction/compare/v5.0.0...v5.1.0 [5.0.0]: https://github.com/AaronLasseigne/active_interaction/compare/v4.1.0...v5.0.0 [4.1.0]: https://github.com/AaronLasseigne/active_interaction/compare/v4.0.6...v4.1.0 @@ -1339,3 +1346,4 @@ Example.run [#503]: https://github.com/AaronLasseigne/active_interaction/issues/503 [#536]: https://github.com/AaronLasseigne/active_interaction/issues/536 [#537]: https://github.com/AaronLasseigne/active_interaction/issues/537 + [#539]: https://github.com/AaronLasseigne/active_interaction/issues/539 diff --git a/lib/active_interaction/filter.rb b/lib/active_interaction/filter.rb index 39868867..0e49bb57 100644 --- a/lib/active_interaction/filter.rb +++ b/lib/active_interaction/filter.rb @@ -112,24 +112,21 @@ def process(value, context) # @raise [NoDefaultError] If the default is missing. # @raise [InvalidDefaultError] If the default is invalid. def default(context = nil) - return @default if defined?(@default) - raise NoDefaultError, name unless default? value = raw_default(context) raise InvalidDefaultError, "#{name}: #{value.inspect}" if value.is_a?(GroupedInput) - @default = - if value.nil? - nil - else - default = process(value, context) - if default.errors.any? && default.errors.first.is_a?(Filter::Error) - raise InvalidDefaultError, "#{name}: #{value.inspect}" - end - - default.value + if value.nil? + nil + else + default = process(value, context) + if default.errors.any? && default.errors.first.is_a?(Filter::Error) + raise InvalidDefaultError, "#{name}: #{value.inspect}" end + + default.value + end end # Get the description. diff --git a/lib/active_interaction/version.rb b/lib/active_interaction/version.rb index 10344237..0cf29480 100644 --- a/lib/active_interaction/version.rb +++ b/lib/active_interaction/version.rb @@ -4,5 +4,5 @@ module ActiveInteraction # The version number. # # @return [Gem::Version] - VERSION = Gem::Version.new('5.1.0') + VERSION = Gem::Version.new('5.1.1') end diff --git a/spec/active_interaction/filter_spec.rb b/spec/active_interaction/filter_spec.rb index e0ae3eae..eea6949c 100644 --- a/spec/active_interaction/filter_spec.rb +++ b/spec/active_interaction/filter_spec.rb @@ -36,4 +36,25 @@ end end end + + describe '#default' do + subject(:filter) { ActiveInteraction::IntegerFilter.new(:test, default: default) } + + context 'when it is a value' do + let(:default) { 1 } + + it 'returns the default' do + expect(filter.default).to be 1 + end + end + + context 'when it is a proc' do + let(:default) { -> { i + 1 } } + + it 'returns the default' do + expect(filter.default(double(i: 0))).to be 1 # rubocop:disable RSpec/VerifiedDoubles + expect(filter.default(double(i: 1))).to be 2 # rubocop:disable RSpec/VerifiedDoubles + end + end + end end