store_model 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_model/validations/store_model_validator.rb +15 -3
- data/lib/store_model/combine_errors_strategies.rb +23 -1
- data/lib/store_model/combine_errors_strategies/merge_array_error_strategy.rb +22 -0
- data/lib/store_model/combine_errors_strategies/merge_error_strategy.rb +3 -3
- data/lib/store_model/configuration.rb +4 -0
- data/lib/store_model/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81df7c27571266926547a2a5aad309128741860b15fe94876476744efa4932d9
|
4
|
+
data.tar.gz: 33a62eb4baa468872e3b409e34755a8d057841f4b764e0ecac6f50cef4481286
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '019a485b12c9216f493198d6e02d03284e9f912c3eea96ee4c0fb1d2a4be3284c1febfc92e7045c28b6a5266bc8a7cd5eca663536fe4d928d27cf5dfdb31b688'
|
7
|
+
data.tar.gz: b7aaea3584e1f59caea549b9f98e7ee310677746d1d7e58c8b741c01d899592697a6b831fe6a28615fcb6253d38fea9ce57bcb046bb97e6587601dc94ceea224
|
@@ -22,16 +22,28 @@ module ActiveModel
|
|
22
22
|
|
23
23
|
case record.type_for_attribute(attribute).type
|
24
24
|
when :json
|
25
|
-
|
25
|
+
call_json_strategy(attribute, record.errors, value)
|
26
26
|
when :array
|
27
|
-
record.errors
|
27
|
+
call_array_strategy(attribute, record.errors, value)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
+
def call_json_strategy(attribute, record_errors, value)
|
34
|
+
strategy.call(attribute, record_errors, value.errors) if value.invalid?
|
35
|
+
end
|
36
|
+
|
37
|
+
def call_array_strategy(attribute, record_errors, value)
|
38
|
+
array_strategy.call(attribute, record_errors, value) if value.select(&:invalid?).present?
|
39
|
+
end
|
40
|
+
|
33
41
|
def strategy
|
34
|
-
StoreModel::CombineErrorsStrategies.configure(options)
|
42
|
+
@strategy ||= StoreModel::CombineErrorsStrategies.configure(options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def array_strategy
|
46
|
+
@array_strategy ||= StoreModel::CombineErrorsStrategies.configure_array(options)
|
35
47
|
end
|
36
48
|
end
|
37
49
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "store_model/combine_errors_strategies/mark_invalid_error_strategy"
|
4
4
|
require "store_model/combine_errors_strategies/merge_error_strategy"
|
5
|
+
require "store_model/combine_errors_strategies/merge_array_error_strategy"
|
5
6
|
|
6
7
|
module StoreModel
|
7
8
|
# Module with built-in strategies for combining errors.
|
@@ -16,10 +17,31 @@ module StoreModel
|
|
16
17
|
def configure(options)
|
17
18
|
configured_strategy = options[:merge_errors] || StoreModel.config.merge_errors
|
18
19
|
|
20
|
+
get_configured_strategy(
|
21
|
+
configured_strategy,
|
22
|
+
StoreModel::CombineErrorsStrategies::MergeErrorStrategy
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Finds a array strategy based on +options+ and global config.
|
27
|
+
#
|
28
|
+
# @param options [Hash]
|
29
|
+
#
|
30
|
+
# @return [Object] strategy
|
31
|
+
def configure_array(options)
|
32
|
+
configured_strategy = options[:merge_array_errors] || StoreModel.config.merge_array_errors
|
33
|
+
|
34
|
+
get_configured_strategy(
|
35
|
+
configured_strategy,
|
36
|
+
StoreModel::CombineErrorsStrategies::MergeArrayErrorStrategy
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_configured_strategy(configured_strategy, true_strategy_class)
|
19
41
|
if configured_strategy.respond_to?(:call)
|
20
42
|
configured_strategy
|
21
43
|
elsif configured_strategy == true
|
22
|
-
|
44
|
+
true_strategy_class.new
|
23
45
|
elsif configured_strategy.nil?
|
24
46
|
StoreModel::CombineErrorsStrategies::MarkInvalidErrorStrategy.new
|
25
47
|
else
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module StoreModel
|
4
|
+
module CombineErrorsStrategies
|
5
|
+
# +MergeArrayErrorStrategy+ copies errors from the StoreModel::Model to the parent
|
6
|
+
# record attribute errors.
|
7
|
+
class MergeArrayErrorStrategy
|
8
|
+
# Merges errors on +attribute+ from the child model with parent errors.
|
9
|
+
#
|
10
|
+
# @param attribute [String] name of the validated attribute
|
11
|
+
# @param base_errors [ActiveModel::Errors] errors object of the parent record
|
12
|
+
# @param store_models [Array] an array or store_models that have been validated
|
13
|
+
def call(attribute, base_errors, store_models)
|
14
|
+
store_models.each_with_index do |store_model, index|
|
15
|
+
store_model.errors.full_messages.each do |full_message|
|
16
|
+
base_errors.add(attribute, :invalid, message: "[#{index}] #{full_message}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -7,16 +7,16 @@ module StoreModel
|
|
7
7
|
class MergeErrorStrategy
|
8
8
|
# Merges errors on +attribute+ from the child model with parent errors.
|
9
9
|
#
|
10
|
-
# @param
|
10
|
+
# @param attribute [String] name of the validated attribute
|
11
11
|
# @param base_errors [ActiveModel::Errors] errors object of the parent record
|
12
12
|
# @param store_model_errors [ActiveModel::Errors] errors object of the StoreModel::Model
|
13
13
|
# attribute
|
14
|
-
def call(
|
14
|
+
def call(attribute, base_errors, store_model_errors)
|
15
15
|
if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero?
|
16
16
|
base_errors.copy!(store_model_errors)
|
17
17
|
else
|
18
18
|
store_model_errors.errors.each do |error|
|
19
|
-
base_errors.add(
|
19
|
+
base_errors.add(attribute, :invalid, message: error.full_message)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- lib/store_model.rb
|
95
95
|
- lib/store_model/combine_errors_strategies.rb
|
96
96
|
- lib/store_model/combine_errors_strategies/mark_invalid_error_strategy.rb
|
97
|
+
- lib/store_model/combine_errors_strategies/merge_array_error_strategy.rb
|
97
98
|
- lib/store_model/combine_errors_strategies/merge_error_strategy.rb
|
98
99
|
- lib/store_model/configuration.rb
|
99
100
|
- lib/store_model/enum.rb
|