validates_associated_with_context 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1787057e985031b75e23d9494db47b43d69cdbc38910879d5fc5a60168658d01
4
- data.tar.gz: 246e0fd3d70992b3e6199765d8cb90c7f80e2bb7159102ae9e2c502941044d3a
3
+ metadata.gz: cadc82e10e44e2ad51e4237f48a2db4693b9d8228d8b81a1f2cca041e12beb82
4
+ data.tar.gz: bec3863a52663e08bf07d709e11262d6c4f9bd63312b79d731f7d03c867473cb
5
5
  SHA512:
6
- metadata.gz: 73ba110de0ca4d9f6065ded99c24e99a42864ecadd3e640f38a3b79d8918707937dc43c4357d02df0bfa77cb68019a77fed0bd6bf746733a4de473d7fbeb72e4
7
- data.tar.gz: d41fa60edc5ba6b2efe65cc65ca246b3024fa59cc7b6e7dae6bca51334b61b943a15267c4899bbeb06cb2644b146cc21143336d1ffe992d64bdd383c606a261f
6
+ metadata.gz: 0be5cb7c91b46eb12f7370e72a25d6c8a68435dec3f37cb01a48b086da8d60ea71a671f1481fca40e41d090282d91b7055854853559ead6c441f4eb1e31601ca
7
+ data.tar.gz: b392807810156061ba7dffd8b5333432e9165d9684f4327196d0720b097301b00ed96c6ceab35329aa9549a8c5171d62d71c3c0fd774e9a74f44a04ea5255ea0
@@ -2,20 +2,45 @@
2
2
 
3
3
  module ValidatesAssociatedWithContext
4
4
  class AssociatedWithContextValidator < ActiveModel::EachValidator # :nodoc:
5
- attr_reader :context, :inherit_context
5
+ attr_reader :inherit_context, :bubble_messages
6
6
 
7
7
  def initialize(options)
8
8
  @context = options.delete(:context)
9
9
  @inherit_context = options.delete(:inherit_context)
10
+ @bubble_messages = options.delete(:bubble_messages)
10
11
 
11
12
  super
12
13
  end
13
14
 
15
+ def context(record)
16
+ return record.validation_context if inherit_context
17
+
18
+ if @context.respond_to?(:call)
19
+ @context.call record
20
+ else
21
+ @context
22
+ end
23
+ end
24
+
14
25
  def validate_each(record, attribute, value)
15
- validation_context = inherit_context ? record.validation_context : context
26
+ validation_context = context(record)
27
+
28
+ if bubble_messages
29
+ Array(value).each do |r|
30
+ next if valid_object?(r, validation_context)
16
31
 
17
- if Array(value).reject { |r| valid_object?(r, validation_context) }.any?
18
- record.errors.add(attribute, :invalid, **options.merge(value: value))
32
+ if r.errors.any?
33
+ r.errors.each do |error|
34
+ record.errors.add(attribute, error.full_message, **options.merge(value: r))
35
+ end
36
+ else
37
+ record.errors.add(attribute, :invalid, **options.merge(value: r))
38
+ end
39
+ end
40
+ else
41
+ if Array(value).reject { |r| valid_object?(r, validation_context) }.any?
42
+ record.errors.add(attribute, :invalid, **options.merge(value: value))
43
+ end
19
44
  end
20
45
  end
21
46
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidatesAssociatedWithContext
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_associated_with_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillaume Dott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-08 00:00:00.000000000 Z
11
+ date: 2023-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -54,7 +54,6 @@ files:
54
54
  - lib/validates_associated_with_context.rb
55
55
  - lib/validates_associated_with_context/associated_with_context.rb
56
56
  - lib/validates_associated_with_context/version.rb
57
- - validates_associated_with_context.gemspec
58
57
  homepage: https://code.amnesix.eu/guillaume/validates_associated_with_context
59
58
  licenses:
60
59
  - MIT
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/validates_associated_with_context/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "validates_associated_with_context"
7
- spec.version = ValidatesAssociatedWithContext::VERSION
8
- spec.authors = ["Guillaume Dott"]
9
- spec.email = ["guillaume+github@dott.fr"]
10
-
11
- spec.summary = "validates_associated with validation context"
12
- spec.description = "Adds validates_associated_with_context to models to validate associated models with validation context"
13
- spec.homepage = "https://code.amnesix.eu/guillaume/validates_associated_with_context"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://code.amnesix.eu/guillaume/validates_associated_with_context"
19
- spec.metadata["changelog_uri"] = "https://code.amnesix.eu/guillaume/validates_associated_with_context/src/branch/main/CHANGELOG.md"
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(__dir__) do
24
- `git ls-files -z`.split("\x0").reject do |f|
25
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
26
- end
27
- end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- # Uncomment to register a new dependency of your gem
33
- spec.add_dependency "activemodel", "~> 7.0"
34
- spec.add_dependency "activerecord", "~> 7.0"
35
-
36
- # For more information and examples about making a new gem, check out our
37
- # guide at: https://bundler.io/guides/creating_gem.html
38
- end