activemodel-extra_validations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 13a5ccd8fc08c9d77b2ff8470d8bbf95725d6c0646ea25a13e4e2198ecb27b85
4
+ data.tar.gz: 0b34cda2eebe64d1ade17fbcbcac29e5de845d65985503bc603dc2c5c3af72bd
5
+ SHA512:
6
+ metadata.gz: a0c56369211c56ea556339f336be05277812fd57d9b75041107a2c01551e865b68b5bfbe4804a8b4ca3e677e053a950cd825de27720df17dab15ea08a5093677
7
+ data.tar.gz: 7b850e7ea82d23923591b890cc58d41ee59052998549495553b89d254a2c8b7b5093f95dc82f08023e94a1b16a78f49fc25fc1fe1ed8f6c6499d42b85f033dd8
data/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ # Unix-style newlines with a newline ending every file
2
+ [*]
3
+ end_of_line = lf
4
+ insert_final_newline = true
5
+ max_line_length = 120
6
+ trim_trailing_whitespace = true
7
+
8
+ # 2 space indentation
9
+ [*.rb]
10
+ indent_style = space
11
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /Gemfile.lock
7
+ /measurements/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.overcommit.yml ADDED
@@ -0,0 +1,31 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/sds/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/sds/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ on_warn: fail # Treat all warnings as failures
22
+
23
+ TrailingWhitespace:
24
+ enabled: true
25
+
26
+ #PostCheckout:
27
+ # ALL: # Special hook name that customizes all hooks of this type
28
+ # quiet: true # Change all post-checkout hooks to only display output on failure
29
+ #
30
+ # IndexTags:
31
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,31 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.7
5
+ DisplayCopNames: true
6
+ NewCops: enable
7
+
8
+ # ----------------------- Layout ----------------------
9
+
10
+ Layout:
11
+ Max: 120
12
+
13
+ Layout/FirstArrayElementIndentation:
14
+ Enabled: false
15
+
16
+ # ---------------------- Metrics ----------------------
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - '**/*_spec.rb'
21
+ - activemodel-extra_validations.gemspec
22
+
23
+ # ----------------------- RSpec -----------------------
24
+
25
+ RSpec/ExampleLength:
26
+ Enabled: false
27
+
28
+ # ----------------------- Style -----------------------
29
+
30
+ Style/FrozenStringLiteralComment:
31
+ Enabled: false
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.0.0
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.7
7
+ - 3.0
8
+ - jruby
9
+
10
+ before_install: gem install bundler -v 2.2.3
11
+
12
+ before_script:
13
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
+ - chmod +x ./cc-test-reporter
15
+ - ./cc-test-reporter before-build
16
+
17
+ script:
18
+ - bundle exec bundle-audit
19
+ - bundle exec rspec
20
+ - bundle exec rubocop
21
+ - bundle exec rake yard:junk
22
+
23
+ after_script:
24
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/.yardstick.yml ADDED
@@ -0,0 +1,22 @@
1
+ threshold: 100
2
+ rules:
3
+ ApiTag::Presence:
4
+ enabled: true
5
+ ApiTag::Inclusion:
6
+ enabled: true
7
+ ApiTag::ProtectedMethod:
8
+ enabled: true
9
+ ApiTag::PrivateMethod:
10
+ enabled: true
11
+ ExampleTag:
12
+ enabled: true
13
+ ReturnTag:
14
+ enabled: true
15
+ Summary::Presence:
16
+ enabled: true
17
+ Summary::Length:
18
+ enabled: false
19
+ Summary::Delimiter:
20
+ enabled: true
21
+ Summary::SingleLine:
22
+ enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] - 2021-05-31
8
+ ### Added
9
+ - Added Codebase maintenance tools
10
+ - Added `presence_of_at_least_one_of` validator
11
+
12
+ [0.1.0]: https://github.com/wilsonsilva/activemodel-extra_validations/compare/a03b15...v0.1.0
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wilson.dsigns@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in activemodel-extra_validations.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,20 @@
1
+ guard :bundler do
2
+ watch('activemodel-extra_validations.gemspec')
3
+ end
4
+
5
+ guard :bundler_audit, run_on_start: true do
6
+ watch('Gemfile.lock')
7
+ end
8
+
9
+ group :tests do
10
+ guard :rspec, all_on_start: true, cmd: 'COVERAGE=false bundle exec rspec --format progress' do
11
+ watch(%r{^spec/.+_spec\.rb$})
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
13
+ watch('spec/spec_helper.rb') { 'spec' }
14
+ end
15
+ end
16
+
17
+ guard :rubocop do
18
+ watch(/.+\.rb$/)
19
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Wilson Silva
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,90 @@
1
+ # Activemodel::ExtraValidations
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/activemodel-extra_validations.svg)](https://badge.fury.io/rb/activemodel-extra_validations)
4
+ [![Build Status](https://travis-ci.org/wilsonsilva/activemodel-extra_validations.svg?branch=master)](https://travis-ci.org/wilsonsilva/activemodel-extra_validations)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/7afec90a09db3956b7dc/maintainability)](https://codeclimate.com/github/wilsonsilva/activemodel-extra_validations/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/7afec90a09db3956b7dc/test_coverage)](https://codeclimate.com/github/wilsonsilva/activemodel-extra_validations/test_coverage)
7
+ [![Security](https://hakiri.io/github/wilsonsilva/activemodel-extra_validations/master.svg)](https://hakiri.io/github/wilsonsilva/activemodel-extra_validations/master)
8
+ [![Inline docs](http://inch-ci.org/github/wilsonsilva/activemodel-extra_validations.svg?branch=master)](http://inch-ci.org/github/wilsonsilva/activemodel-extra_validations)
9
+
10
+ Common ActiveModel/ActiveRecord validations.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'activemodel-extra_validations'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install activemodel-extra_validations
27
+
28
+ ## Usage
29
+
30
+ ### presence_of_at_least_one_of
31
+
32
+ Validates that at least one of the supplied attributes are given.
33
+
34
+ ```ruby
35
+ require 'activemodel/extra_validations/presence_of_at_least_one_of'
36
+
37
+ class BillingDetails
38
+ include ActiveModel::Model
39
+ include ActiveModel::Validations
40
+ include ActiveModel::Attributes
41
+
42
+ attribute :credit_card
43
+ attribute :debit_card
44
+
45
+ validate_presence_of_at_least_one_of :credit_card, :debit_card
46
+ end
47
+
48
+ billing_details = BillingDetails.new
49
+ billing_details.valid?
50
+ billing_details.errors.full_messages # => [
51
+ # 'Debit card is missing. At least one of [:credit_card, :debit_card] must be present',
52
+ # 'Credit card is missing. At least one of [:credit_card, :debit_card] must be present'
53
+ # ]
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies, configure git hooks and create support files.
59
+
60
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ The health and maintainability of the codebase is ensured through a set of
63
+ Rake tasks to test, lint and audit the gem for security vulnerabilities and documentation:
64
+
65
+ ```
66
+ rake bundle:audit # Checks for vulnerable versions of gems
67
+ rake qa # Test, lint and perform security and documentation audits
68
+ rake rubocop # Lint the codebase with RuboCop
69
+ rake rubocop:auto_correct # Auto-correct RuboCop offenses
70
+ rake spec # Run RSpec code examples
71
+ rake verify_measurements # Verify that yardstick coverage is at least 100%
72
+ rake yard # Generate YARD Documentation
73
+ rake yard:junk # Check the junk in your YARD Documentation
74
+ rake yardstick_measure # Measure docs in lib/**/*.rb with yardstick
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wilsonsilva/activemodel-extra_validations.
80
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
81
+ the [code of conduct](https://github.com/wilsonsilva/activemodel-extra_validations/blob/master/CODE_OF_CONDUCT.md).
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
86
+
87
+ ## Code of Conduct
88
+
89
+ Everyone interacting in the Activemodel::ExtraValidations project's codebases, issue trackers, chat rooms and mailing
90
+ lists is expected to follow the [code of conduct](https://github.com/wilsonsilva/activemodel-extra_validations/blob/master/CODE_OF_CONDUCT.md).
data/ROADMAP.md ADDED
@@ -0,0 +1,11 @@
1
+ # Roadmap
2
+
3
+ ## Validators
4
+
5
+ - [ ] same_as
6
+ - [ ] mutually_exclusive
7
+ - [ ] exactly_one_of
8
+ - [x] presence_of_at_least_one_of
9
+ - [ ] all_or_none_of
10
+ - [ ] prefix (start_with)
11
+ - [ ] suffix (end_with)
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/audit/task'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
7
+ require 'yaml'
8
+ require 'yard/rake/yardoc_task'
9
+ require 'yard-junk/rake'
10
+ require 'yardstick/rake/measurement'
11
+ require 'yardstick/rake/verify'
12
+
13
+ yardstick_options = YAML.load_file('.yardstick.yml')
14
+
15
+ Bundler::Audit::Task.new
16
+ RSpec::Core::RakeTask.new(:spec)
17
+ RuboCop::RakeTask.new
18
+ YARD::Rake::YardocTask.new
19
+ YardJunk::Rake.define_task
20
+ Yardstick::Rake::Measurement.new(:yardstick_measure, yardstick_options)
21
+ Yardstick::Rake::Verify.new
22
+
23
+ task default: %i[spec rubocop]
24
+
25
+ # Remove the report on rake clobber
26
+ CLEAN.include('measurements', 'doc', '.yardoc', 'tmp')
27
+
28
+ # Delete these files and folders when running rake clobber.
29
+ CLOBBER.include('coverage', '.rspec_status')
30
+
31
+ desc 'Run spec with coverage'
32
+ task :coverage do
33
+ ENV['COVERAGE'] = 'true'
34
+ Rake::Task['spec'].execute
35
+ `open coverage/index.html`
36
+ end
37
+
38
+ desc 'Test, lint and perform security and documentation audits'
39
+ task qa: %w[spec rubocop yard:junk verify_measurements bundle:audit]
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/activemodel/extra_validations/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-extra_validations'
7
+ spec.version = ActiveModel::ExtraValidations::VERSION
8
+ spec.authors = ['Wilson Silva']
9
+ spec.email = ['me@wilsonsilva.net']
10
+
11
+ spec.summary = 'Common validations for ActiveModel'
12
+ spec.description = 'Common validations for ActiveModel'
13
+ spec.homepage = 'https://github.com/wilsonsilva/activemodel-extra_validations'
14
+ spec.license = 'MIT'
15
+
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/wilsonsilva/activemodel-extra_validations'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/wilsonsilva/activemodel-extra_validations/blob/master/CHANGELOG.md'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'activemodel', '~> 6.1'
33
+
34
+ spec.add_development_dependency 'bundler-audit', '~> 0.7'
35
+ spec.add_development_dependency 'guard', '~> 2.16'
36
+ spec.add_development_dependency 'guard-bundler', '~> 3.0'
37
+ spec.add_development_dependency 'guard-bundler-audit', '~> 0.1.4'
38
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
39
+ spec.add_development_dependency 'guard-rubocop', '~> 1.4'
40
+ spec.add_development_dependency 'overcommit', '~> 0.57.0'
41
+ spec.add_development_dependency 'pry', '~> 0.14'
42
+ spec.add_development_dependency 'rake', '~> 13.0'
43
+ spec.add_development_dependency 'rspec', '~> 3.0'
44
+ spec.add_development_dependency 'rubocop', '~> 0.93'
45
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.44'
46
+ spec.add_development_dependency 'simplecov', '~> 0.17.1'
47
+ spec.add_development_dependency 'simplecov-console', '~> 0.9'
48
+ spec.add_development_dependency 'yard', '~> 0.9'
49
+ spec.add_development_dependency 'yard-junk', '~> 0.0.9'
50
+ spec.add_development_dependency 'yardstick', '~> 0.9'
51
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'activemodel/extra_validations'
6
+ require 'pry'
7
+
8
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ if ! which overcommit >/dev/null; then
9
+ echo 'The gem overcommit is not installed. It is necessary to lint the git history through git hooks.'
10
+ echo 'Please install overcommit and run this script again.'
11
+ exit 1
12
+ fi
13
+
14
+ overcommit --install
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+
5
+ require_relative 'extra_validations/version'
6
+
7
+ I18n.load_path += Dir[File.join(__dir__, 'extra_validations/locale/*.yml')]
8
+
9
+ module ActiveModel
10
+ # Common ActiveModel validations
11
+ module ExtraValidations
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ en:
3
+ activemodel: &activemodel
4
+ errors:
5
+ messages:
6
+ presence_of_at_least_one_of: "is missing. At least one of %{one_of} must be present"
7
+
8
+ activerecord:
9
+ <<: *activemodel
@@ -0,0 +1,63 @@
1
+ require 'active_model'
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ # Validates whether at least one of the specified attributes is present.
6
+ class PresenceOfAtLeastOneOfValidator < Validator
7
+ ERROR_MESSAGE = 'This validator requires at least two arguments.'.freeze
8
+
9
+ def initialize(options = {})
10
+ super(options)
11
+
12
+ check_validity!
13
+ end
14
+
15
+ def validate(record)
16
+ at_least_one_present = attribute_names.any? do |attribute_name|
17
+ record.attributes[attribute_name.to_s].present? || !record.attributes[attribute_name.to_s].nil?
18
+ end
19
+
20
+ add_errors_to_record(record) unless at_least_one_present
21
+ end
22
+
23
+ private
24
+
25
+ def add_errors_to_record(record)
26
+ attribute_names.each do |attribute_name|
27
+ record.errors.add(attribute_name, error_message_or_error_type, one_of: attribute_names)
28
+ end
29
+ end
30
+
31
+ def check_validity!
32
+ raise ArgumentError, ERROR_MESSAGE unless options[:attributes].many?
33
+ end
34
+
35
+ def attribute_names
36
+ options[:attributes]
37
+ end
38
+
39
+ def error_message_or_error_type
40
+ options[:message] || :presence_of_at_least_one_of
41
+ end
42
+ end
43
+
44
+ # Methods to be automatically included on every ActiveModel instance
45
+ module HelperMethods
46
+ # Validates whether at least one of the specified attributes is present.
47
+ #
48
+ # class BillingDetails < ActiveRecord::Base
49
+ # validate_presence_of_at_least_one_of :credit_card, :debit_card
50
+ # end
51
+ #
52
+ # Configuration options:
53
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is
54
+ # not included in the list").
55
+ #
56
+ # See <tt>ActiveModel::Validations#validates</tt> for more information
57
+ #
58
+ def validate_presence_of_at_least_one_of(*attribute_names)
59
+ validates_with PresenceOfAtLeastOneOfValidator, _merge_attributes(attribute_names)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ module ExtraValidations
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,321 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-extra_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wilson Silva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-31 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: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-bundler-audit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: overcommit
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.57.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.57.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.14'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.14'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '13.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '13.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.93'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.93'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.44'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.44'
195
+ - !ruby/object:Gem::Dependency
196
+ name: simplecov
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: 0.17.1
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: 0.17.1
209
+ - !ruby/object:Gem::Dependency
210
+ name: simplecov-console
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '0.9'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '0.9'
223
+ - !ruby/object:Gem::Dependency
224
+ name: yard
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '0.9'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '0.9'
237
+ - !ruby/object:Gem::Dependency
238
+ name: yard-junk
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - "~>"
242
+ - !ruby/object:Gem::Version
243
+ version: 0.0.9
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - "~>"
249
+ - !ruby/object:Gem::Version
250
+ version: 0.0.9
251
+ - !ruby/object:Gem::Dependency
252
+ name: yardstick
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - "~>"
256
+ - !ruby/object:Gem::Version
257
+ version: '0.9'
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - "~>"
263
+ - !ruby/object:Gem::Version
264
+ version: '0.9'
265
+ description: Common validations for ActiveModel
266
+ email:
267
+ - me@wilsonsilva.net
268
+ executables: []
269
+ extensions: []
270
+ extra_rdoc_files: []
271
+ files:
272
+ - ".editorconfig"
273
+ - ".gitignore"
274
+ - ".overcommit.yml"
275
+ - ".rspec"
276
+ - ".rubocop.yml"
277
+ - ".tool-versions"
278
+ - ".travis.yml"
279
+ - ".yardstick.yml"
280
+ - CHANGELOG.md
281
+ - CODE_OF_CONDUCT.md
282
+ - Gemfile
283
+ - Guardfile
284
+ - LICENSE.txt
285
+ - README.md
286
+ - ROADMAP.md
287
+ - Rakefile
288
+ - activemodel-extra_validations.gemspec
289
+ - bin/console
290
+ - bin/setup
291
+ - lib/activemodel/extra_validations.rb
292
+ - lib/activemodel/extra_validations/locale/en.yml
293
+ - lib/activemodel/extra_validations/presence_of_at_least_one_of.rb
294
+ - lib/activemodel/extra_validations/version.rb
295
+ homepage: https://github.com/wilsonsilva/activemodel-extra_validations
296
+ licenses:
297
+ - MIT
298
+ metadata:
299
+ homepage_uri: https://github.com/wilsonsilva/activemodel-extra_validations
300
+ source_code_uri: https://github.com/wilsonsilva/activemodel-extra_validations
301
+ changelog_uri: https://github.com/wilsonsilva/activemodel-extra_validations/blob/master/CHANGELOG.md
302
+ post_install_message:
303
+ rdoc_options: []
304
+ require_paths:
305
+ - lib
306
+ required_ruby_version: !ruby/object:Gem::Requirement
307
+ requirements:
308
+ - - ">="
309
+ - !ruby/object:Gem::Version
310
+ version: 2.7.0
311
+ required_rubygems_version: !ruby/object:Gem::Requirement
312
+ requirements:
313
+ - - ">="
314
+ - !ruby/object:Gem::Version
315
+ version: '0'
316
+ requirements: []
317
+ rubygems_version: 3.2.3
318
+ signing_key:
319
+ specification_version: 4
320
+ summary: Common validations for ActiveModel
321
+ test_files: []