store_model 0.6.2 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4da91e0f6f9e27a71947f5a31b7bb0d936680bd263ad46f69916d995380af27
4
- data.tar.gz: 463730c176403dd845382cd078d90e58faae6d2ee1a7c9670c7f7c48d68032d3
3
+ metadata.gz: 81df7c27571266926547a2a5aad309128741860b15fe94876476744efa4932d9
4
+ data.tar.gz: 33a62eb4baa468872e3b409e34755a8d057841f4b764e0ecac6f50cef4481286
5
5
  SHA512:
6
- metadata.gz: 1cb34bbaf868eea40c26d33b2c15a8fbd8c79f3e5429714172f0f38ea9e11a396241831744be82c280b8ace4de60237a5226d8a9cf77bedf2b200b8bb3e58485
7
- data.tar.gz: 8dbf31fb3f4ece28979fa49d799b1f633e7e9f5007ceb38d0cf883ea214c50dc621c8f5b8c5367cb979910c74af039de675acbf7d69e7720d2f6d61b25a240e4
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
- strategy.call(attribute, record.errors, value.errors) if value.invalid?
25
+ call_json_strategy(attribute, record.errors, value)
26
26
  when :array
27
- record.errors.add(attribute, :invalid) if value.select(&:invalid?).present?
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
- StoreModel::CombineErrorsStrategies::MergeErrorStrategy.new
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 _attribute [String] name of the validated attribute
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(_attribute, base_errors, store_model_errors)
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(:configuration, :invalid, message: error.full_message)
19
+ base_errors.add(attribute, :invalid, message: error.full_message)
20
20
  end
21
21
  end
22
22
  end
@@ -6,5 +6,9 @@ module StoreModel
6
6
  # Controls usage of MergeErrorStrategy
7
7
  # @return [Boolean]
8
8
  attr_accessor :merge_errors
9
+
10
+ # Controls usage of MergeArrayErrorStrategy
11
+ # @return [Boolean]
12
+ attr_accessor :merge_array_errors
9
13
  end
10
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.6.2"
4
+ VERSION = "0.7.0"
5
5
  end
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.6.2
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-17 00:00:00.000000000 Z
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