validates_associated_with_context 0.1.0 → 0.2.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: 1233b7d28ae93c0fed5840a8f36ee9f8e745e5c09ae7712f634e91d03df6bbfa
4
- data.tar.gz: f835e5d9824cc9aceeeff5686c77fe61068440aff02921d855dd4fe9d2a9c531
3
+ metadata.gz: cadc82e10e44e2ad51e4237f48a2db4693b9d8228d8b81a1f2cca041e12beb82
4
+ data.tar.gz: bec3863a52663e08bf07d709e11262d6c4f9bd63312b79d731f7d03c867473cb
5
5
  SHA512:
6
- metadata.gz: cbe112416406fd2a309216571a2926df4e1e2d5eef68a400c3ad1c18cf6e2bb553e2f7d5871e7498a657ded3e2f245f778930e9018b3e9e91231772046713609
7
- data.tar.gz: e2bb59482622df4ddebc2a366822e533167b1fe549b7c41c1728156adf6d21a6b4736d9199feda623e9ce8056a5e13920677fffaee53dba3d981429ffc5298fa
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.0"
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.0
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
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '7.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '7.0'
27
41
  description: Adds validates_associated_with_context to models to validate associated
28
42
  models with validation context
29
43
  email:
@@ -40,7 +54,6 @@ files:
40
54
  - lib/validates_associated_with_context.rb
41
55
  - lib/validates_associated_with_context/associated_with_context.rb
42
56
  - lib/validates_associated_with_context/version.rb
43
- - validates_associated_with_context.gemspec
44
57
  homepage: https://code.amnesix.eu/guillaume/validates_associated_with_context
45
58
  licenses:
46
59
  - MIT
@@ -1,37 +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
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
37
- end