validates_associated_with_context 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1233b7d28ae93c0fed5840a8f36ee9f8e745e5c09ae7712f634e91d03df6bbfa
4
+ data.tar.gz: f835e5d9824cc9aceeeff5686c77fe61068440aff02921d855dd4fe9d2a9c531
5
+ SHA512:
6
+ metadata.gz: cbe112416406fd2a309216571a2926df4e1e2d5eef68a400c3ad1c18cf6e2bb553e2f7d5871e7498a657ded3e2f245f778930e9018b3e9e91231772046713609
7
+ data.tar.gz: e2bb59482622df4ddebc2a366822e533167b1fe549b7c41c1728156adf6d21a6b4736d9199feda623e9ce8056a5e13920677fffaee53dba3d981429ffc5298fa
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-03-08
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in validates_associated_with_context.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Guillaume Dott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ValidatesAssociatedWithContext
2
+
3
+ Adds `validates_associated_with_context` to models to validate associated models with validation context.
4
+
5
+ ## Installation
6
+
7
+
8
+ Add it to your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'validates_associated_with_context'
12
+ ```
13
+
14
+ Run the following command to install it:
15
+
16
+ ```
17
+ bundle install
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Use `validates_associated_with_context` in your models:
23
+ ```ruby
24
+ class Book < ActiveRecord::Base
25
+ has_many :pages
26
+ belongs_to :library
27
+
28
+ validates_associated_with_context :pages, :library, context: :custom_validation_context
29
+ end
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesAssociatedWithContext
4
+ class AssociatedWithContextValidator < ActiveModel::EachValidator # :nodoc:
5
+ attr_reader :context, :inherit_context
6
+
7
+ def initialize(options)
8
+ @context = options.delete(:context)
9
+ @inherit_context = options.delete(:inherit_context)
10
+
11
+ super
12
+ end
13
+
14
+ def validate_each(record, attribute, value)
15
+ validation_context = inherit_context ? record.validation_context : context
16
+
17
+ if Array(value).reject { |r| valid_object?(r, validation_context) }.any?
18
+ record.errors.add(attribute, :invalid, **options.merge(value: value))
19
+ end
20
+ end
21
+
22
+ private
23
+ def valid_object?(record, validation_context)
24
+ (record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid?(validation_context)
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ # Validates whether the associated object or objects are all valid.
30
+ # Works with any kind of association.
31
+ #
32
+ # class Book < ActiveRecord::Base
33
+ # has_many :pages
34
+ # belongs_to :library
35
+ #
36
+ # validates_associated_with_context :pages, :library
37
+ # end
38
+ #
39
+ # WARNING: This validation must not be used on both ends of an association.
40
+ # Doing so will lead to a circular dependency and cause infinite recursion.
41
+ #
42
+ # NOTE: This validation will not fail if the association hasn't been
43
+ # assigned. If you want to ensure that the association is both present and
44
+ # guaranteed to be valid, you also need to use
45
+ # {validates_presence_of}[rdoc-ref:Validations::ClassMethods#validates_presence_of].
46
+ #
47
+ # Configuration options:
48
+ #
49
+ # * <tt>:context</tt> - The validation context to use.
50
+ # * <tt>:inherit_context</tt> - When <tt>true</tt>, the validation context
51
+ # is passed to the associated model.
52
+ # * <tt>:message</tt> - A custom error message (default is: "is invalid").
53
+ # * <tt>:on</tt> - Specifies the contexts where this validation is active.
54
+ # Runs in all validation contexts by default +nil+. You can pass a symbol
55
+ # or an array of symbols. (e.g. <tt>on: :create</tt> or
56
+ # <tt>on: :custom_validation_context</tt> or
57
+ # <tt>on: [:create, :custom_validation_context]</tt>)
58
+ # * <tt>:if</tt> - Specifies a method, proc, or string to call to determine
59
+ # if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
60
+ # or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
61
+ # proc or string should return or evaluate to a +true+ or +false+ value.
62
+ # * <tt>:unless</tt> - Specifies a method, proc, or string to call to
63
+ # determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
64
+ # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
65
+ # method, proc, or string should return or evaluate to a +true+ or +false+
66
+ # value.
67
+ def validates_associated_with_context(*attr_names)
68
+ validates_with ValidatesAssociatedWithContext::AssociatedWithContextValidator, _merge_attributes(attr_names)
69
+ end
70
+ end
71
+ end
72
+
73
+ ActiveRecord::Validations::ClassMethods.include ValidatesAssociatedWithContext::ClassMethods
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesAssociatedWithContext
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "validates_associated_with_context/version"
4
+ require_relative "validates_associated_with_context/associated_with_context"
@@ -0,0 +1,37 @@
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
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_associated_with_context
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Dott
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description: Adds validates_associated_with_context to models to validate associated
28
+ models with validation context
29
+ email:
30
+ - guillaume+github@dott.fr
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/validates_associated_with_context.rb
41
+ - lib/validates_associated_with_context/associated_with_context.rb
42
+ - lib/validates_associated_with_context/version.rb
43
+ - validates_associated_with_context.gemspec
44
+ homepage: https://code.amnesix.eu/guillaume/validates_associated_with_context
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://code.amnesix.eu/guillaume/validates_associated_with_context
49
+ source_code_uri: https://code.amnesix.eu/guillaume/validates_associated_with_context
50
+ changelog_uri: https://code.amnesix.eu/guillaume/validates_associated_with_context/src/branch/main/CHANGELOG.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.6.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.25
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: validates_associated with validation context
70
+ test_files: []