validates_associated_with_context 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1787057e985031b75e23d9494db47b43d69cdbc38910879d5fc5a60168658d01
4
- data.tar.gz: 246e0fd3d70992b3e6199765d8cb90c7f80e2bb7159102ae9e2c502941044d3a
3
+ metadata.gz: 5f940506b9e54b6aee923afee7b65480727fb2b55429dd144ad8bc5086dfdf46
4
+ data.tar.gz: 34575c454b24fa26c69bc5a928a332033387e54958d69850782b88d6bf135e8f
5
5
  SHA512:
6
- metadata.gz: 73ba110de0ca4d9f6065ded99c24e99a42864ecadd3e640f38a3b79d8918707937dc43c4357d02df0bfa77cb68019a77fed0bd6bf746733a4de473d7fbeb72e4
7
- data.tar.gz: d41fa60edc5ba6b2efe65cc65ca246b3024fa59cc7b6e7dae6bca51334b61b943a15267c4899bbeb06cb2644b146cc21143336d1ffe992d64bdd383c606a261f
6
+ metadata.gz: 6808b2d3f06af9d31eeecfe0fe638e53290f9d56eeddd32a8b5105a9c1c1dc7d84d302f6c544d12e268f6e8fb6ac17e8dcba257d9719da0bbe4a85afad7d03a0
7
+ data.tar.gz: daffbef719b076344696bcc0286295ac6d1c8d8bb92503397859e435cd52978727a1a05e74e4b25c222be0c1a643256e284cc013406320754f8b4daade668258
@@ -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.3.0"
5
5
  end
metadata CHANGED
@@ -1,43 +1,55 @@
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.3.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: 2024-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '7.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '9.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '7.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '9.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activerecord
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - "~>"
37
+ - - ">="
32
38
  - !ruby/object:Gem::Version
33
39
  version: '7.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '9.0'
34
43
  type: :runtime
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
- - - "~>"
47
+ - - ">="
39
48
  - !ruby/object:Gem::Version
40
49
  version: '7.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '9.0'
41
53
  description: Adds validates_associated_with_context to models to validate associated
42
54
  models with validation context
43
55
  email:
@@ -54,7 +66,6 @@ files:
54
66
  - lib/validates_associated_with_context.rb
55
67
  - lib/validates_associated_with_context/associated_with_context.rb
56
68
  - lib/validates_associated_with_context/version.rb
57
- - validates_associated_with_context.gemspec
58
69
  homepage: https://code.amnesix.eu/guillaume/validates_associated_with_context
59
70
  licenses:
60
71
  - MIT
@@ -77,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
88
  - !ruby/object:Gem::Version
78
89
  version: '0'
79
90
  requirements: []
80
- rubygems_version: 3.3.25
91
+ rubygems_version: 3.5.16
81
92
  signing_key:
82
93
  specification_version: 4
83
94
  summary: validates_associated with validation context
@@ -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