Skip to content

Commit

Permalink
fix #539 by not caching defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Sep 1, 2022
1 parent f50806d commit 99f714f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
21 changes: 9 additions & 12 deletions lib/active_interaction/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/active_interaction/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 99f714f

Please sign in to comment.