conditional_validation 0.1.0 → 1.0.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 +5 -5
- data/.gitignore +11 -0
- data/.travis.yml +21 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -41
- data/Rakefile +6 -28
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/conditional_validation.gemspec +40 -0
- data/lib/conditional_validation.rb +3 -3
- data/lib/conditional_validation/validation_flag.rb +33 -37
- data/lib/conditional_validation/version.rb +1 -1
- metadata +104 -112
- data/MIT-LICENSE +0 -20
- data/lib/conditional_validation/validation_accessor.rb +0 -51
- data/test/conditional_validation_test.rb +0 -101
- data/test/dummy/README.rdoc +0 -28
- data/test/dummy/Rakefile +0 -6
- data/test/dummy/app/assets/javascripts/application.js +0 -13
- data/test/dummy/app/assets/stylesheets/application.css +0 -13
- data/test/dummy/app/controllers/application_controller.rb +0 -5
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/user.rb +0 -21
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/bin/bundle +0 -3
- data/test/dummy/bin/rails +0 -4
- data/test/dummy/bin/rake +0 -4
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -23
- data/test/dummy/config/boot.rb +0 -5
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -29
- data/test/dummy/config/environments/production.rb +0 -80
- data/test/dummy/config/environments/test.rb +0 -36
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/test/dummy/config/initializers/inflections.rb +0 -16
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -12
- data/test/dummy/config/initializers/session_store.rb +0 -3
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -23
- data/test/dummy/config/routes.rb +0 -56
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20131119215801_create_users.rb +0 -9
- data/test/dummy/db/schema.rb +0 -24
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +0 -17
- data/test/dummy/log/test.log +0 -0
- data/test/dummy/public/404.html +0 -58
- data/test/dummy/public/422.html +0 -58
- data/test/dummy/public/500.html +0 -57
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/users.yml +0 -8
- data/test/dummy/test/models/user_test.rb +0 -7
- data/test/test_helper.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 30e3326be3141037e97c1cb5d73b2a0e1adc0d7726cf31abe88900243b51eeaa
|
4
|
+
data.tar.gz: c042b1ba75f2f3bade811bbbe8aead65ccbc87b176450771b3cbb240ff5a492b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 238787bd9346e5806b96e9d9b6d31786ff7dd715e382236d8f5098c76766cb240459623fa056f1b68cce552493d0d4815328cba604ded3fd40ca78213665f5f9
|
7
|
+
data.tar.gz: a9c98d23238f3ef3275ecb0ac926e3ec64717cd9f13161de3d13903814145ffb392c5ce64e9350735c7fb94d6e03e3033219e456cb736e30b2cdd69b13dfd502
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
env:
|
2
|
+
global:
|
3
|
+
- CC_TEST_REPORTER_ID=ce4fc4d08e215d267d51d1add4379d2ee836127c64bbd1bc5bf48537fa9ea2ff
|
4
|
+
sudo: false
|
5
|
+
language: ruby
|
6
|
+
rvm:
|
7
|
+
- 2.2.10
|
8
|
+
- 2.3.7
|
9
|
+
- 2.4.4
|
10
|
+
- 2.5.1
|
11
|
+
- ruby-head
|
12
|
+
notifications:
|
13
|
+
email: false
|
14
|
+
before_install: gem install bundler -v 1.16.1
|
15
|
+
cache: bundler
|
16
|
+
before_script:
|
17
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
18
|
+
- chmod +x ./cc-test-reporter
|
19
|
+
- ./cc-test-reporter before-build
|
20
|
+
after_script:
|
21
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
### 1.0.0 - 2018-04-16
|
2
|
+
- Remove the deprecated `validation_accessor` macro in favor of `validation_flag`.
|
3
|
+
- Remove ActiveRecord monkey patch. Instead, use `extend ConditionalValidation::ValidationFlag` in ApplicationRecord.
|
4
|
+
- Refresh gem. Remove Rails dependency.
|
5
|
+
- Improve README.
|
6
|
+
|
7
|
+
|
8
|
+
### 0.1.0
|
9
|
+
|
10
|
+
- Deprecate `validation_accessor` macro in favor of `validation_flag`.
|
11
|
+
- Documentation updates.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
conditional_validation (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
11
|
+
byebug (10.0.2)
|
12
|
+
coderay (1.1.2)
|
13
|
+
docile (1.3.0)
|
14
|
+
json (2.1.0)
|
15
|
+
method_source (0.9.0)
|
16
|
+
minitest (5.11.3)
|
17
|
+
minitest-reporters (1.2.0)
|
18
|
+
ansi
|
19
|
+
builder
|
20
|
+
minitest (>= 5.0)
|
21
|
+
ruby-progressbar
|
22
|
+
pry (0.11.3)
|
23
|
+
coderay (~> 1.1.0)
|
24
|
+
method_source (~> 0.9.0)
|
25
|
+
pry-byebug (3.6.0)
|
26
|
+
byebug (~> 10.0)
|
27
|
+
pry (~> 0.10)
|
28
|
+
rake (10.5.0)
|
29
|
+
ruby-progressbar (1.9.0)
|
30
|
+
simplecov (0.16.1)
|
31
|
+
docile (~> 1.1)
|
32
|
+
json (>= 1.8, < 3)
|
33
|
+
simplecov-html (~> 0.10.0)
|
34
|
+
simplecov-html (0.10.2)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
bundler (~> 1.16)
|
41
|
+
byebug (~> 10.0)
|
42
|
+
conditional_validation!
|
43
|
+
minitest (~> 5.0)
|
44
|
+
minitest-reporters (~> 1.2)
|
45
|
+
pry (~> 0.11)
|
46
|
+
pry-byebug (~> 3.6)
|
47
|
+
rake (~> 10.0)
|
48
|
+
simplecov (~> 0.16)
|
49
|
+
|
50
|
+
BUNDLED WITH
|
51
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Paul Dobbins
|
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
CHANGED
@@ -1,23 +1,13 @@
|
|
1
1
|
# Conditional Validation
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/conditional_validation)
|
4
|
+
[](https://travis-ci.org/pdobb/conditional_validation)
|
5
|
+
[](https://codeclimate.com/github/pdobb/conditional_validation/test_coverage)
|
6
|
+
[](https://codeclimate.com/github/pdobb/conditional_validation/maintainability)
|
4
7
|
|
5
|
-
|
6
|
-
certain validations should be run on a model. The idea being that, while models
|
7
|
-
tend to have a set of core validations that should always be run, some
|
8
|
-
validations may be specific to a certain context or state of the object. Typical
|
9
|
-
use-case, then, is to flag a model's non-core validations to run from specific
|
10
|
-
controller actions, while they default to not run from all others.
|
8
|
+
ConditionalValidation simplifies adding validation flags and then querying state to determine when associated validations should be run on models. While models tend to have a set of core validations that should always be run, some validations may be specific to a certain context.
|
11
9
|
|
12
|
-
|
13
|
-
## Compatibility
|
14
|
-
|
15
|
-
Tested with:
|
16
|
-
|
17
|
-
* Ruby: MRI 1.9.3
|
18
|
-
* Ruby: MRI 2.0.0 +
|
19
|
-
* Rails: 3+
|
20
|
-
* Rails: 4+
|
10
|
+
The typical use-case for ConditionalValidation is to flag a model's non-core validations to run from specific controller actions, while they default to not run from all others.
|
21
11
|
|
22
12
|
|
23
13
|
## Installation
|
@@ -25,34 +15,85 @@ Tested with:
|
|
25
15
|
Add this line to your application's Gemfile:
|
26
16
|
|
27
17
|
```ruby
|
28
|
-
gem
|
18
|
+
gem "conditional_validation"
|
29
19
|
```
|
30
20
|
|
31
21
|
And then execute:
|
32
22
|
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install conditional_validation
|
28
|
+
|
29
|
+
|
30
|
+
## Upgrading from v0.1.0
|
31
|
+
|
32
|
+
### Monkey Patching Eliminated
|
33
|
+
|
34
|
+
v0.1.0 automatically added ConditionalValidation::ValidationFlag to ActiveRecord::Base. Since monkey patching was never a good idea, ConditionalValidation should now be added to Models manually.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# app/models/my_model.rb
|
38
|
+
class MyModel
|
39
|
+
extend ConditionalValidation::ValidationFlag
|
40
|
+
|
41
|
+
validation_flag :test_attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
my_model = MyModel.new
|
45
|
+
|
46
|
+
my_model.enable_test_attributes_validation
|
47
|
+
# => #<MyModel:0x007fd76bbecb80 @_test_attributes_validation_flag=true>
|
48
|
+
|
49
|
+
my_model.validate_on_test_attributes?
|
50
|
+
# => true
|
51
|
+
```
|
52
|
+
|
53
|
+
Or, to use ConditionalValidation globally in a Rails app, call extend ConditionalValidation::ValidationFlag within ApplicationRecord.
|
54
|
+
|
33
55
|
```ruby
|
34
|
-
|
56
|
+
# app/models/application_record.rb
|
57
|
+
class ApplicationRecord < ActiveRecord::Base
|
58
|
+
extend ConditionalValidation::ValidationFlag
|
59
|
+
end
|
35
60
|
```
|
36
61
|
|
37
62
|
|
63
|
+
### `validation_accessor` Macro Removed
|
64
|
+
|
65
|
+
Additionally, the old ConditionalValidation::ValidationAccessor module and its `validation_accessor` macro are gone. To fix: Search and replace `validation_accessor` with `validation_flag`.
|
66
|
+
|
67
|
+
|
68
|
+
## Compatibility
|
69
|
+
|
70
|
+
Tested MRI Ruby Versions:
|
71
|
+
* 2.2.10
|
72
|
+
* 2.3.7
|
73
|
+
* 2.4.4
|
74
|
+
* 2.5.1
|
75
|
+
* edge
|
76
|
+
|
77
|
+
VersionCompare has no other dependencies.
|
78
|
+
|
79
|
+
|
38
80
|
## Usage
|
39
81
|
|
40
82
|
First, define a validation flag:
|
41
83
|
|
42
84
|
```ruby
|
43
|
-
# app/models/
|
44
|
-
class
|
85
|
+
# app/models/my_model.rb
|
86
|
+
class MyModel
|
45
87
|
validation_flag :<flag_name>
|
46
88
|
end
|
47
89
|
```
|
48
90
|
|
49
|
-
|
50
|
-
validation:
|
91
|
+
The `validation_flag` macro will define the following instance methods on MyModel for conditional validation:
|
51
92
|
|
52
93
|
```ruby
|
53
|
-
enable_<flag_name>_validation
|
54
|
-
disable_<flag_name>_validation
|
55
|
-
validate_on_<flag_name>?
|
94
|
+
enable_<flag_name>_validation # Enables conditional validation
|
95
|
+
disable_<flag_name>_validation # Disables conditional validation
|
96
|
+
validate_on_<flag_name>? # Check if conditional validation is enabled or not
|
56
97
|
```
|
57
98
|
|
58
99
|
|
@@ -60,41 +101,60 @@ validate_on_<flag_name>? # Check if conditional validation is enabled
|
|
60
101
|
|
61
102
|
```ruby
|
62
103
|
# app/models/user.rb
|
63
|
-
User <
|
104
|
+
class User < ApplicationRecord
|
64
105
|
# Initialize conditional validation on address attributes
|
65
106
|
validation_flag :address_attributes
|
66
107
|
|
67
|
-
with_options if: :validate_on_address_attributes? do |
|
68
|
-
|
69
|
-
|
108
|
+
with_options if: :validate_on_address_attributes? do |o|
|
109
|
+
o.validates :street, presence: true
|
110
|
+
o.validates :city, presence: true
|
70
111
|
# ...
|
71
112
|
end
|
72
113
|
end
|
73
114
|
|
74
115
|
# app/controllers/user_controller.rb
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
current_user.
|
81
|
-
|
116
|
+
class UsersController < ApplicationController
|
117
|
+
def update
|
118
|
+
# Enable conditional validation on address attributes
|
119
|
+
current_user.enable_address_attributes_validation
|
120
|
+
|
121
|
+
if current_user.save
|
122
|
+
# Not typically needed, but disables conditional validation on address attributes
|
123
|
+
current_user.disable_address_attributes_validation
|
124
|
+
|
125
|
+
# ...
|
126
|
+
end
|
82
127
|
end
|
83
128
|
end
|
84
129
|
```
|
85
130
|
|
131
|
+
|
86
132
|
### Method Chaining
|
87
133
|
|
88
|
-
The
|
89
|
-
validation flags may be enabled/disabled at once:
|
134
|
+
The `enable_<flag_name>_validation` and `disable_<flag_name>_validation` methods allow for method chaining so that multiple validation flags may be enabled/disabled at once:
|
90
135
|
|
91
136
|
```ruby
|
92
|
-
if current_user.
|
137
|
+
if current_user.
|
138
|
+
enable_address_attributes_validation.
|
139
|
+
enable_some_other_validation.
|
140
|
+
save
|
93
141
|
# ...
|
94
142
|
end
|
95
143
|
```
|
96
144
|
|
97
145
|
|
98
|
-
##
|
146
|
+
## Development
|
147
|
+
|
148
|
+
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.
|
149
|
+
|
150
|
+
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).
|
151
|
+
|
152
|
+
|
153
|
+
## Contributing
|
154
|
+
|
155
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pdobb/conditional_validation.
|
156
|
+
|
157
|
+
|
158
|
+
## License
|
99
159
|
|
100
|
-
|
160
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,32 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
rescue LoadError
|
4
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
-
end
|
6
|
-
|
7
|
-
require 'rdoc/task'
|
8
|
-
|
9
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'ConditionalValidation'
|
12
|
-
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.rdoc')
|
14
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
Bundler::GemHelper.install_tasks
|
21
|
-
|
22
|
-
require 'rake/testtask'
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
23
3
|
|
24
4
|
Rake::TestTask.new(:test) do |t|
|
25
|
-
t.libs <<
|
26
|
-
t.libs <<
|
27
|
-
t.
|
28
|
-
t.verbose = false
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
29
8
|
end
|
30
9
|
|
31
|
-
|
32
|
-
task default: :test
|
10
|
+
task :default => :test
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "conditional_validation"
|
5
|
+
require "ostruct"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
require "pry"
|
12
|
+
Pry.start
|
13
|
+
|
14
|
+
# require "irb"
|
15
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "conditional_validation/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "conditional_validation"
|
7
|
+
spec.version = ConditionalValidation::VERSION
|
8
|
+
spec.authors = ["Paul Dobbins"]
|
9
|
+
spec.email = ["paul.dobbins@icloud.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{ConditionalValidation simplifies adding validation flags to models.}
|
12
|
+
spec.description = %q{ConditionalValidation simplifies adding validation flags and then querying state to determine when associated validations should be run on models.}
|
13
|
+
spec.homepage = "https://github.com/pdobb/conditional_validation"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
35
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.2"
|
36
|
+
spec.add_development_dependency "simplecov", "~> 0.16"
|
37
|
+
spec.add_development_dependency "byebug", "~> 10.0"
|
38
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
39
|
+
spec.add_development_dependency "pry-byebug", "~> 3.6"
|
40
|
+
end
|