active_model_validates_intersection_of 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 +7 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/active_model_validates_intersection_of.gemspec +26 -0
- data/lib/active_model_validates_intersection_of.rb +3 -0
- data/lib/active_model_validates_intersection_of/inclusion_intersection_validator.rb +11 -0
- data/lib/active_model_validates_intersection_of/validates_intersection_of_alias.rb +5 -0
- data/lib/active_model_validates_intersection_of/version.rb +3 -0
- data/spec/lib/inclusion_intersection_validator_spec.rb +41 -0
- data/spec/lib/validates_intersection_of_alias_spec.rb +41 -0
- data/spec/shared.rb +25 -0
- data/spec/spec_helper.rb +17 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0ef8977cca2302df90c919bd3212ee38c004a503
|
4
|
+
data.tar.gz: 21dab974646c8a4cfed5742a7ad6627316ea350d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6452a2c54d3754d6a1947b5e30784bea2a899e0e4e47662dd51dcea9487c34ea801d80b264d1127aed41378ec114b912135c95065143bbaf2c1352b520d4d431
|
7
|
+
data.tar.gz: ebdfcd71070d2e4132dfaed773684a9b8374917461ff6e6bd862242b62c5e46c034c18ab6c742c20adfd4e8788b5fe03f62646501d1442db3a57e0b6795f21e7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Active Model Validates Intersection Of
|
2
|
+
|
3
|
+
 [](https://codeclimate.com/github/rafaelbiriba/active_model_validates_intersection_of) [](https://coveralls.io/r/rafaelbiriba/active_model_validates_intersection_of?branch=master) [](https://travis-ci.org/rafaelbiriba/active_model_validates_intersection_of)
|
4
|
+
|
5
|
+
A custom validation for Active Model that check if an array is included in another one.
|
6
|
+
|
7
|
+
Identical to the method `validates_inclusion_of` from ActiveModel but for array comparison.
|
8
|
+
|
9
|
+
Consider an User model that have some set of "default" permissions.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
DEFAULT_PERMISSION = ["read", "write", "share"]
|
14
|
+
validates_intersection_of :permission, in: DEFAULT_ROLES
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
If you want to validate your user based on an array:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
user = User.new(permission: ["read", "share"])
|
22
|
+
user.valid? #true
|
23
|
+
|
24
|
+
user = User.new(permission: ["read", "admin"])
|
25
|
+
user.valid? #false
|
26
|
+
```
|
27
|
+
|
28
|
+
This active model custom validation **is for you**!
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'active_model_validates_intersection_of'
|
36
|
+
```
|
37
|
+
|
38
|
+
And then execute:
|
39
|
+
|
40
|
+
$ bundle
|
41
|
+
|
42
|
+
Or install it yourself as:
|
43
|
+
|
44
|
+
$ gem install active_model_validates_intersection_of
|
45
|
+
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
* `in:` parameter is required
|
49
|
+
* `message:` is optional
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class User < ActiveRecord::Base
|
53
|
+
DEFAULT_PERMISSION = ["read", "write", "share"]
|
54
|
+
validates_intersection_of :permission, in: DEFAULT_ROLES
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class User < ActiveRecord::Base
|
60
|
+
DEFAULT_PERMISSION = ["read", "write", "share"]
|
61
|
+
validates_intersection_of :permission, in: DEFAULT_ROLES, message: "invalid permission"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
You can also use the custom validation directly:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class User < ActiveRecord::Base
|
69
|
+
DEFAULT_PERMISSION = ["read", "write", "share"]
|
70
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: VALID_ARRAY
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
First of all, **thank you** for wanting to help!
|
81
|
+
|
82
|
+
1. [Fork it](https://help.github.com/articles/fork-a-repo).
|
83
|
+
2. Create a feature branch - `git checkout -b more_magic`
|
84
|
+
3. Add tests and make your changes
|
85
|
+
4. Check if tests are ok - `rake spec`
|
86
|
+
5. Commit changes - `git commit -am "Added more magic"`
|
87
|
+
6. Push to Github - `git push origin more_magic`
|
88
|
+
7. Send a [pull request](https://help.github.com/articles/using-pull-requests)! :heart:
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_model_validates_intersection_of/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_model_validates_intersection_of"
|
8
|
+
spec.version = ActiveModelValidatesIntersectionOf::VERSION
|
9
|
+
spec.authors = ["Rafael Biriba"]
|
10
|
+
spec.email = ["biribarj@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A custom validation for your Active Model that check if an array is included in another one"
|
13
|
+
spec.description = "A custom validation for your Active Model that check if an array is included in another one"
|
14
|
+
spec.homepage = "https://github.com/rafaelbiriba/active_model_validates_intersection_of"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", ">= 4.0.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "coveralls"
|
26
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveModelValidatesIntersectionOf
|
2
|
+
class Validator < ActiveModel::EachValidator
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
delimiter = options[:in]
|
5
|
+
raise(ArgumentError, "An array must be supplied as the :in option of the configuration hash") unless delimiter.kind_of?(Array)
|
6
|
+
if (value - delimiter).any?
|
7
|
+
record.errors.add(attribute, :inclusion, options.except(:in).merge!(value: value))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require_relative "../shared.rb"
|
3
|
+
|
4
|
+
RSpec.describe ActiveModelValidatesIntersectionOf::Validator do
|
5
|
+
class BadSetupValidator
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :list
|
8
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list]
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:badsetup) { BadSetupValidator.new }
|
12
|
+
|
13
|
+
context "setup" do
|
14
|
+
describe "without an :in configuration" do
|
15
|
+
it_behaves_like "invalid configuration"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
20
|
+
|
21
|
+
let(:valid_partial_array) { VALID_ARRAY.sample(2) }
|
22
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
23
|
+
|
24
|
+
class ListValidator
|
25
|
+
include ActiveModel::Validations
|
26
|
+
attr_accessor :list
|
27
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list], in: VALID_ARRAY, message: "not valid"
|
28
|
+
end
|
29
|
+
|
30
|
+
subject { List.new }
|
31
|
+
|
32
|
+
context "validation" do
|
33
|
+
describe "with a valid list" do
|
34
|
+
it_behaves_like "valid object"
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "with an invalid list" do
|
38
|
+
it_behaves_like "invalid object"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require_relative "../shared.rb"
|
3
|
+
|
4
|
+
RSpec.describe ActiveModel::Validations::HelperMethods do
|
5
|
+
class BadSetup
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :list
|
8
|
+
validates_intersection_of :list
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:badsetup) { BadSetup.new }
|
12
|
+
|
13
|
+
context "setup" do
|
14
|
+
describe "without an :in configuration" do
|
15
|
+
it_behaves_like "invalid configuration"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
20
|
+
|
21
|
+
let(:valid_partial_array) { VALID_ARRAY.sample(2) }
|
22
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
23
|
+
|
24
|
+
class List
|
25
|
+
include ActiveModel::Validations
|
26
|
+
attr_accessor :list
|
27
|
+
validates_intersection_of :list, in: VALID_ARRAY, message: "not valid"
|
28
|
+
end
|
29
|
+
|
30
|
+
subject { List.new }
|
31
|
+
|
32
|
+
context "validation" do
|
33
|
+
describe "with a valid list" do
|
34
|
+
it_behaves_like "valid object"
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "with an invalid list" do
|
38
|
+
it_behaves_like "invalid object"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/shared.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
RSpec.shared_examples "invalid configuration" do
|
2
|
+
it "should raise an error" do
|
3
|
+
expect { badsetup.valid? }.to raise_error(ArgumentError)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.shared_examples "valid object" do
|
8
|
+
it "the subject should be valid" do
|
9
|
+
subject.list = valid_partial_array
|
10
|
+
expect(subject.valid?).to eq(true)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec.shared_examples "invalid object" do
|
15
|
+
it "the subject should not be valid" do
|
16
|
+
subject.list = invalid_partial_array
|
17
|
+
expect(subject.valid?).to eq(false)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "the subject should have an error" do
|
21
|
+
subject.list = invalid_partial_array
|
22
|
+
subject.valid?
|
23
|
+
expect(subject.errors.messages).to eq({:list=>["not valid"]})
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "active_model"
|
3
|
+
require "active_model_validates_intersection_of"
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
# Enable flags like --only-failures and --next-failure
|
9
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
10
|
+
|
11
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
12
|
+
config.disable_monkey_patching!
|
13
|
+
|
14
|
+
config.expect_with :rspec do |c|
|
15
|
+
c.syntax = :expect
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_validates_intersection_of
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Biriba
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-01 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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A custom validation for your Active Model that check if an array is included
|
84
|
+
in another one
|
85
|
+
email:
|
86
|
+
- biribarj@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- active_model_validates_intersection_of.gemspec
|
98
|
+
- lib/active_model_validates_intersection_of.rb
|
99
|
+
- lib/active_model_validates_intersection_of/inclusion_intersection_validator.rb
|
100
|
+
- lib/active_model_validates_intersection_of/validates_intersection_of_alias.rb
|
101
|
+
- lib/active_model_validates_intersection_of/version.rb
|
102
|
+
- spec/lib/inclusion_intersection_validator_spec.rb
|
103
|
+
- spec/lib/validates_intersection_of_alias_spec.rb
|
104
|
+
- spec/shared.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: https://github.com/rafaelbiriba/active_model_validates_intersection_of
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.6.12
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: A custom validation for your Active Model that check if an array is included
|
130
|
+
in another one
|
131
|
+
test_files:
|
132
|
+
- spec/lib/inclusion_intersection_validator_spec.rb
|
133
|
+
- spec/lib/validates_intersection_of_alias_spec.rb
|
134
|
+
- spec/shared.rb
|
135
|
+
- spec/spec_helper.rb
|