active_interaction 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -1
- data/README.md +1 -1
- data/lib/active_interaction/base.rb +9 -5
- data/lib/active_interaction/errors.rb +1 -0
- data/lib/active_interaction/filter.rb +2 -0
- data/lib/active_interaction/filter_method.rb +0 -4
- data/lib/active_interaction/filters/array_filter.rb +2 -0
- data/lib/active_interaction/filters/hash_filter.rb +3 -1
- data/lib/active_interaction/version.rb +1 -1
- data/spec/active_interaction/filter_method_spec.rb +0 -5
- data/spec/active_interaction/filters/array_filter_spec.rb +3 -1
- data/spec/active_interaction/filters/hash_filter_spec.rb +3 -1
- data/spec/active_interaction/integration/array_interaction_spec.rb +16 -10
- data/spec/active_interaction/integration/hash_interaction_spec.rb +16 -10
- data/spec/support/interactions.rb +5 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58a3ed3559d4e08664cc19fb81100a947bb597e8
|
4
|
+
data.tar.gz: d7d1152399851b34028504b90b05a5cfda76e14f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96954a543eb586113be298cef976bbbdc5bb1eeed62de460d5b42ac3ad3c2c56896483729ad1c4a012ef08757b1e92a47848b9b7db7c903ebea3f3455967cfba
|
7
|
+
data.tar.gz: 046393507e5bb5acfea4db4bd0b1e95222d8dfafd156bb5289668c0139153ce1c9e9a643d1b3fc7c41d5bcdd444df5abc47c300301a9721a8757471eea2976e7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# [Master][]
|
2
2
|
|
3
|
+
# [0.3.0][] (2013-08-07)
|
4
|
+
|
5
|
+
- Give better error messages for nested attributes.
|
6
|
+
- Use default value when given an explicit `nil`.
|
7
|
+
- Allow nested default values.
|
8
|
+
|
3
9
|
# [0.2.2][] (2013-08-07)
|
4
10
|
|
5
11
|
- Fix support for `ActiveSupport::TimeWithZone`.
|
@@ -33,7 +39,8 @@
|
|
33
39
|
|
34
40
|
- Initial release.
|
35
41
|
|
36
|
-
[master]: https://github.com/orgsync/active_interaction/compare/v0.
|
42
|
+
[master]: https://github.com/orgsync/active_interaction/compare/v0.3.0...master
|
43
|
+
[0.3.0]: https://github.com/orgsync/active_interaction/compare/v0.2.2...v0.3.0
|
37
44
|
[0.2.2]: https://github.com/orgsync/active_interaction/compare/v0.2.1...v0.2.2
|
38
45
|
[0.2.1]: https://github.com/orgsync/active_interaction/compare/v0.2.0...v0.2.1
|
39
46
|
[0.2.0]: https://github.com/orgsync/active_interaction/compare/v0.1.3...v0.2.0
|
data/README.md
CHANGED
@@ -121,7 +121,9 @@ module ActiveInteraction
|
|
121
121
|
# @return The return value of {#execute}.
|
122
122
|
def self.run!(options = {})
|
123
123
|
outcome = run(options)
|
124
|
-
|
124
|
+
if outcome.invalid?
|
125
|
+
raise InteractionInvalid, outcome.errors.full_messages.join(', ')
|
126
|
+
end
|
125
127
|
outcome.result
|
126
128
|
end
|
127
129
|
|
@@ -144,7 +146,7 @@ module ActiveInteraction
|
|
144
146
|
begin
|
145
147
|
default = filter.
|
146
148
|
prepare(attribute, options[:default], options, &block)
|
147
|
-
rescue InvalidValue
|
149
|
+
rescue InvalidNestedValue, InvalidValue
|
148
150
|
raise InvalidDefaultValue
|
149
151
|
end
|
150
152
|
end
|
@@ -170,7 +172,7 @@ module ActiveInteraction
|
|
170
172
|
value =
|
171
173
|
begin
|
172
174
|
filter.prepare(attribute, value, options, &block)
|
173
|
-
rescue InvalidValue, MissingValue
|
175
|
+
rescue InvalidNestedValue, InvalidValue, MissingValue
|
174
176
|
value
|
175
177
|
end
|
176
178
|
instance_variable_set("@#{attribute}", value)
|
@@ -188,11 +190,13 @@ module ActiveInteraction
|
|
188
190
|
define_method(validator) do
|
189
191
|
begin
|
190
192
|
filter.prepare(attribute, send(attribute), options, &block)
|
191
|
-
rescue
|
192
|
-
errors.add(attribute, 'is
|
193
|
+
rescue InvalidNestedValue
|
194
|
+
errors.add(attribute, 'is invalid')
|
193
195
|
rescue InvalidValue
|
194
196
|
errors.add(attribute,
|
195
197
|
"is not a valid #{type.to_s.humanize.downcase}")
|
198
|
+
rescue MissingValue
|
199
|
+
errors.add(attribute, 'is required')
|
196
200
|
end
|
197
201
|
end
|
198
202
|
private validator
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ActiveInteraction
|
2
2
|
class InteractionInvalid < ::StandardError; end
|
3
3
|
class InvalidDefaultValue < ::StandardError; end
|
4
|
+
class InvalidNestedValue < ::StandardError; end
|
4
5
|
class InvalidValue < ::StandardError; end
|
5
6
|
class MissingValue < ::StandardError; end
|
6
7
|
end
|
@@ -25,7 +25,7 @@ module ActiveInteraction
|
|
25
25
|
def self.prepare(key, value, options = {}, &block)
|
26
26
|
case value
|
27
27
|
when Hash
|
28
|
-
convert_values(value.
|
28
|
+
convert_values(value.merge(options[:default] || {}), &block)
|
29
29
|
else
|
30
30
|
super
|
31
31
|
end
|
@@ -41,6 +41,8 @@ module ActiveInteraction
|
|
41
41
|
end
|
42
42
|
|
43
43
|
hash
|
44
|
+
rescue InvalidValue, MissingValue
|
45
|
+
raise InvalidNestedValue
|
44
46
|
end
|
45
47
|
private_class_method :convert_values
|
46
48
|
end
|
@@ -32,11 +32,6 @@ describe ActiveInteraction::FilterMethod do
|
|
32
32
|
before { args << options }
|
33
33
|
|
34
34
|
include_examples 'instance variable assignment'
|
35
|
-
|
36
|
-
it 'does not allow :default' do
|
37
|
-
options.merge!(default: nil)
|
38
|
-
expect { filter_method }.to raise_error ArgumentError
|
39
|
-
end
|
40
35
|
end
|
41
36
|
|
42
37
|
context 'with a block' do
|
@@ -28,7 +28,9 @@ describe ActiveInteraction::ArrayFilter do
|
|
28
28
|
let(:value) { [Object.new] }
|
29
29
|
|
30
30
|
it 'raises an error' do
|
31
|
-
expect {
|
31
|
+
expect {
|
32
|
+
result
|
33
|
+
}.to raise_error ActiveInteraction::InvalidNestedValue
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
@@ -28,7 +28,9 @@ describe ActiveInteraction::HashFilter do
|
|
28
28
|
let(:value) { { a: Object.new } }
|
29
29
|
|
30
30
|
it 'raises an error' do
|
31
|
-
expect {
|
31
|
+
expect {
|
32
|
+
result
|
33
|
+
}.to raise_error ActiveInteraction::InvalidNestedValue
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
@@ -53,17 +53,23 @@ describe ArrayInteraction do
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
context 'with
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
array :
|
61
|
-
array default: []
|
62
|
-
end
|
56
|
+
context 'with a validly nested default' do
|
57
|
+
let(:described_class) do
|
58
|
+
Class.new(ActiveInteraction::Base) do
|
59
|
+
array :a do
|
60
|
+
array default: [rand]
|
63
61
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
def execute; a end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
let(:options) { { a: [] } }
|
66
|
+
|
67
|
+
it 'does not raise an error' do
|
68
|
+
expect { described_class.run(options) }.to_not raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'ignores the nested default value' do
|
72
|
+
expect(described_class.run!(options)).to eq options[:a]
|
67
73
|
end
|
68
74
|
end
|
69
75
|
end
|
@@ -53,17 +53,23 @@ describe HashInteraction do
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
context 'with
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
hash :
|
61
|
-
hash :x, default: {}
|
62
|
-
end
|
56
|
+
context 'with a validly nested default' do
|
57
|
+
let(:described_class) do
|
58
|
+
Class.new(ActiveInteraction::Base) do
|
59
|
+
hash :a do
|
60
|
+
hash :x, default: { y: rand }
|
63
61
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
def execute; a end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
let(:options) { { a: { x: {} } } }
|
66
|
+
|
67
|
+
it 'does not raise an error' do
|
68
|
+
expect { described_class.run(options) }.to_not raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'merges the nested default value' do
|
72
|
+
expect(described_class.run!(options)[:x]).to have_key(:y)
|
67
73
|
end
|
68
74
|
end
|
69
75
|
end
|
@@ -57,6 +57,11 @@ shared_examples_for 'an interaction' do |type, generator, filter_options = {}|
|
|
57
57
|
expect(result[:default]).to_not be_nil
|
58
58
|
end
|
59
59
|
|
60
|
+
it 'does not return nil for :default when given nil' do
|
61
|
+
options.merge!(default: nil)
|
62
|
+
expect(result[:default]).to_not be_nil
|
63
|
+
end
|
64
|
+
|
60
65
|
it 'returns nil for :nil_default' do
|
61
66
|
expect(result[:nil_default]).to be_nil
|
62
67
|
end
|