active_model-unvalidate 1.0.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
+ SHA1:
3
+ metadata.gz: a3e690a73ad1b74d5e1a7e26b38bbdcfa00e9d96
4
+ data.tar.gz: de183dd04fe7668bb15dda2f3ca5e914ecf5d459
5
+ SHA512:
6
+ metadata.gz: 04a96daf74ec9f1fc08fab7266a5ed07c6286482639f5b15e06a9fafb150c8fc5423f5429a27137079203df650e8563a3573352b4ca253ccc7e8cb8937b2b57b
7
+ data.tar.gz: e9fd58eb4d13854c1a122fa008a235de4593b278b719c3254c61261da2be6624d857fdeb2e5655a94e30be8bff6f0f5955495b66c4ffead8018ac60980584716
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.4
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_model-unvalidate (1.0.0)
5
+ activemodel (> 4.0.0)
6
+ activesupport (> 4.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.2.2)
12
+ activesupport (= 5.2.2)
13
+ activesupport (5.2.2)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ concurrent-ruby (1.1.4)
19
+ i18n (1.5.1)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.11.3)
22
+ rake (10.5.0)
23
+ thread_safe (0.3.6)
24
+ tzinfo (1.2.5)
25
+ thread_safe (~> 0.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ active_model-unvalidate!
32
+ bundler (~> 1.16)
33
+ minitest (~> 5.0)
34
+ rake (~> 10.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 WebLinc Corp.
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,80 @@
1
+ # ActiveModel::Unvalidate
2
+
3
+ Intended for working with models outside of your control, `ActiveModel::Unvalidate` provides methods for cleanly removing existing `ActiveModel::Validations` validators from existing models. This allows your project to loosen or change the validations to suite the needs of your application without the need to dig into the implmentation details of validations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_model-unvalidate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model-unvalidate
20
+
21
+ ## Usage
22
+
23
+ Assume a model exists that you cannot modify its original definition.
24
+
25
+ ```ruby
26
+ class Example
27
+ include ActiveModel::Validations
28
+
29
+ attr_accessor :name, :email
30
+
31
+ validates :name, presence: true, length: { minimum: 2 }
32
+ validates :email, presence: true
33
+
34
+ validate :real_email
35
+
36
+ private
37
+
38
+ def real_email
39
+ unless email =~ /[a-z]+@[a-z]+\.[a-z]{1,5}/
40
+ errors.add(:email, 'is not formatted correctly.')
41
+ end
42
+ end
43
+ end
44
+ ```
45
+
46
+ With `active_model-unvalidate` included in your bundle, the `Unvalidate` methods are automatically available to any class that includes `ActiveModel::Validations`. Here you can remove individual validations on specific fields or attributes, such as the length validator on `name`. You can also remove a method-base validation, such as the `real_email` validator.
47
+
48
+ ```ruby
49
+ class Example
50
+ unvalidates :name, :length
51
+ unvalidate :real_email
52
+ end
53
+ ```
54
+
55
+ You can also remove all validations associated to a specific field/attribute.
56
+
57
+ ```ruby
58
+ class Example
59
+ unvalidates :name
60
+ unvalidates :email
61
+
62
+ unvalidate :real_email
63
+ end
64
+ ```
65
+
66
+ This would remove all the validations provided in the example, allowing your application to either redefine new validation rules or leave the model without validations.
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/weblinc/activemodel-unvalidate.
77
+
78
+ ## License
79
+
80
+ 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,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_model/unvalidate/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model-unvalidate"
8
+ spec.version = ActiveModel::Unvalidate::VERSION
9
+ spec.authors = ["Matt Duffy"]
10
+ spec.email = ["mduffy@weblinc.com"]
11
+
12
+ spec.summary = %q{Provides an ActiveModel::Unvalidate module to remove validations}
13
+ spec.description = %q{ActiveModel::Unvalidate provides a clean api for removing existing validations from models}
14
+ spec.homepage = "https://github.com/weblinc/active_model-unvalidate."
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activemodel", '> 4.0.0'
27
+ spec.add_dependency "activesupport", '> 4.0.0'
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ end
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_model"
5
+ require "active_support"
6
+ require "active_model/unvalidate"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,89 @@
1
+ require "active_model/unvalidate/version"
2
+
3
+ module ActiveModel
4
+ module Unvalidate
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ # Copies callbacks to subclasses to ensure ancestor validations
9
+ # are not affected by unvalidating.
10
+ #
11
+ def inherited(base)
12
+ base._validate_callbacks = _validate_callbacks.dup
13
+ super
14
+ end
15
+
16
+ # Removes specified validations from existing models for a given field
17
+ #
18
+ # @param [Symbol] field the field to remove validations from
19
+ # @param [Array<Symbol> | Symbol] validations validations to remove from field
20
+ #
21
+ # @return nil
22
+ #
23
+ def unvalidates(field, validations = nil)
24
+ return unvalidates_all(field) if validations.nil?
25
+
26
+ validations = Array(validations).map do |validation|
27
+ [validation, 'validator'].join('_').classify
28
+ end
29
+ _validators.reject! do |key, validators|
30
+ if field == key
31
+ validators.any? do |validator|
32
+ validations.include?(extract_validator_name(validator))
33
+ end
34
+ end
35
+ end
36
+ _validate_callbacks.select do |callback|
37
+ is_callback_class?(callback) &&
38
+ callback.raw_filter.attributes.include?(field) &&
39
+ validations.include?(extract_validator_name(callback.raw_filter))
40
+ end.each do |callback|
41
+ _validate_callbacks.delete(callback)
42
+ end
43
+ end
44
+
45
+ # Removes all validations from a given field
46
+ #
47
+ # @param [Symbol] field field to remove validations from
48
+ #
49
+ # @return nil
50
+ #
51
+ def unvalidates_all(field)
52
+ _validators.reject! do |key, _|
53
+ key == field
54
+ end
55
+ _validate_callbacks.select do |callback|
56
+ is_callback_class?(callback) && callback.raw_filter.attributes.include?(field)
57
+ end.each do |callback|
58
+ _validate_callbacks.delete(callback)
59
+ end
60
+ end
61
+
62
+ # Removes method-based validation
63
+ #
64
+ # @param [Symbol] method name of validation method to remove
65
+ #
66
+ # @return nil
67
+ #
68
+ def unvalidate(method)
69
+ _validate_callbacks.select do |callback|
70
+ callback.raw_filter == method
71
+ end.each do |callback|
72
+ _validate_callbacks.delete(callback)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def extract_validator_name(validator)
79
+ validator.class.to_s.demodulize
80
+ end
81
+
82
+ def is_callback_class?(callback)
83
+ callback.raw_filter.respond_to?(:attributes)
84
+ end
85
+ end
86
+ end
87
+
88
+ Validations.include Unvalidate
89
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module Unvalidate
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model-unvalidate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Duffy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: ActiveModel::Unvalidate provides a clean api for removing existing validations
84
+ from models
85
+ email:
86
+ - mduffy@weblinc.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - active_model-unvalidate.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/active_model/unvalidate.rb
102
+ - lib/active_model/unvalidate/version.rb
103
+ homepage: https://github.com/weblinc/active_model-unvalidate.
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.14.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Provides an ActiveModel::Unvalidate module to remove validations
127
+ test_files: []