Skip to content

Commit

Permalink
fix #553 by implementing a better error and updating the README
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed May 6, 2023
1 parent d880915 commit 1a03f38
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Fixed

- [#554][] - Non-detailed error should not lose options when merged.
- [#553][] - Improve error handling and documentation for hash filter defaults.

# [5.2.0][] (2022-10-22)

Expand Down Expand Up @@ -1372,3 +1373,4 @@ Example.run
[#539]: https://github.com/AaronLasseigne/active_interaction/issues/539
[#545]: https://github.com/AaronLasseigne/active_interaction/issues/545
[#554]: https://github.com/AaronLasseigne/active_interaction/issues/554
[#553]: https://github.com/AaronLasseigne/active_interaction/issues/553
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ HashInteraction.run!(preferences: { newsletter: true, 'sweepstakes' => false })

Setting default hash values can be tricky. The default value has to be either
`nil` or `{}`. Use `nil` to make the hash optional. Use `{}` if you want to set
some defaults for values inside the hash.
some defaults for values inside the hash. If any nested filter uses a
[lazy default](#defaults) then the hash must also use a lazy default.

``` rb
hash :optional,
Expand Down
6 changes: 5 additions & 1 deletion lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HashFilter < Filter

register :hash

def process(value, context)
def process(value, context) # rubocop:disable Metrics/AbcSize
input = super

return HashInput.new(self, value: input.value, error: input.errors.first) if input.errors.first
Expand All @@ -36,6 +36,10 @@ def process(value, context)
children = {}

filters.each do |name, filter|
if filter.options[:default].is_a?(Proc) && !options[:default].is_a?(Proc)
raise InvalidDefaultError, "#{self.name}: must use a lazy default if any nested filter uses a lazy default"
end

filter.process(input.value[name], context).tap do |result|
value[name] = result.value
children[name.to_sym] = result
Expand Down
32 changes: 32 additions & 0 deletions spec/active_interaction/integration/hash_interaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,36 @@
end.to raise_error ActiveInteraction::InvalidDefaultError
end
end

context 'with a default' do
context 'with a lazy nested default' do
it 'raises an error' do
expect do
Class.new(ActiveInteraction::Base) do
hash :b, default: {} do
hash :x, default: -> { {} }
end
end
end.to raise_error ActiveInteraction::InvalidDefaultError
end
end
end

context 'with a lazy default' do
context 'with a lazy nested default' do
it 'returns the correct value' do
klass = Class.new(ActiveInteraction::Base) do
hash :b, default: -> { {} } do
hash :x, default: -> { {} }
end

def execute
b
end
end

expect(klass.run!).to eql('x' => {})
end
end
end
end

0 comments on commit 1a03f38

Please sign in to comment.