active_model_validates_intersection_of 0.1.0 → 3.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/.travis.yml +13 -2
- data/CHANGELOG.md +20 -0
- data/README.md +35 -21
- data/active_model_validates_intersection_of.gemspec +4 -3
- data/lib/active_model_validates_intersection_of.rb +3 -2
- data/lib/active_model_validates_intersection_of/validator.rb +36 -0
- data/lib/active_model_validates_intersection_of/version.rb +1 -1
- data/lib/intersection_validator.rb +1 -0
- data/lib/{active_model_validates_intersection_of/validates_intersection_of_alias.rb → validates_intersection_of_alias.rb} +0 -0
- data/spec/lib/intersection_validator_spec.rb +149 -0
- data/spec/lib/validates_intersection_of_alias_spec.rb +47 -19
- data/spec/lib/validator_spec.rb +66 -0
- data/spec/shared.rb +0 -6
- data/spec/spec_helper.rb +6 -2
- metadata +31 -14
- data/lib/active_model_validates_intersection_of/inclusion_intersection_validator.rb +0 -11
- data/spec/lib/inclusion_intersection_validator_spec.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ed1bdf75c9a416f57b7bfbde026eaf31a2b9663386df953ff7ed828ce41b5b6
|
4
|
+
data.tar.gz: 835d965a9ffc3e2727271a7a2e35b1e936be77d7ae93b12896d0a1693bb52116
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31a485ee38ce7e938530eb28bbef35b9be11a003b41c3cd12294c4b71bdabc4fc7ddbed461118445ac1ff9d798a79e54d9bce5c3628ef9feb1e5e4033a5ba29a
|
7
|
+
data.tar.gz: 2f0384c4a8f23d7554df00a1d5300d6c125109f1b334f317ba4105c1af558d1526f0bddd6cd75bd9e0f9590fc745979db497a98612fef70f0f80c155deb1b3c2
|
data/.travis.yml
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
4
|
+
- 2.3.6
|
5
|
+
- 2.4.3
|
6
|
+
- 2.5.0
|
7
|
+
- 3.0.1
|
8
|
+
before_install: gem install bundler -v 2.2.16
|
9
|
+
before_script:
|
10
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
11
|
+
- chmod +x ./cc-test-reporter
|
12
|
+
- ./cc-test-reporter before-build
|
13
|
+
script:
|
14
|
+
- bundle exec rspec
|
15
|
+
after_script:
|
16
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
## v3.0.0 (2021-04-13)
|
2
|
+
|
3
|
+
- Ruby 3 support.
|
4
|
+
|
5
|
+
## v1.2.0 (2017-12-20)
|
6
|
+
|
7
|
+
- Support for proc and lambda
|
8
|
+
|
9
|
+
## v1.1.0 (2017-06-06)
|
10
|
+
|
11
|
+
- Implement `validates :list, intersection: { in: ANOTHER_LIST }` see the README for more information
|
12
|
+
|
13
|
+
## v1.0.0 (2017-06-06)
|
14
|
+
|
15
|
+
- Release 1.0 version
|
16
|
+
- Add `:within` parameter support as an alias for `:in`
|
17
|
+
|
18
|
+
## v0.1.0 (2017-06-01)
|
19
|
+
|
20
|
+
- Initial release
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Active Model Validates Intersection Of
|
2
2
|
|
3
|
-
 [ [](https://codeclimate.com/github/rafaelbiriba/active_model_validates_intersection_of/maintainability) [](https://codeclimate.com/github/rafaelbiriba/active_model_validates_intersection_of/test_coverage) [](https://coveralls.io/r/rafaelbiriba/active_model_validates_intersection_of?branch=master) [](https://travis-ci.org/rafaelbiriba/active_model_validates_intersection_of)
|
4
4
|
|
5
5
|
A custom validation for Active Model that check if an array is included in another one.
|
6
6
|
|
@@ -11,7 +11,7 @@ Consider an User model that have some set of "default" permissions.
|
|
11
11
|
```ruby
|
12
12
|
class User < ActiveRecord::Base
|
13
13
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
14
|
-
validates_intersection_of :permission, in:
|
14
|
+
validates_intersection_of :permission, in: DEFAULT_PERMISSION
|
15
15
|
end
|
16
16
|
```
|
17
17
|
|
@@ -21,8 +21,8 @@ If you want to validate your user based on an array:
|
|
21
21
|
user = User.new(permission: ["read", "share"])
|
22
22
|
user.valid? #true
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
user = User.new(permission: ["read", "admin"])
|
25
|
+
user.valid? #false
|
26
26
|
```
|
27
27
|
|
28
28
|
This active model custom validation **is for you**!
|
@@ -32,7 +32,7 @@ user.valid? #true
|
|
32
32
|
Add this line to your application's Gemfile:
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
gem
|
35
|
+
gem "active_model_validates_intersection_of"
|
36
36
|
```
|
37
37
|
|
38
38
|
And then execute:
|
@@ -43,31 +43,45 @@ Or install it yourself as:
|
|
43
43
|
|
44
44
|
$ gem install active_model_validates_intersection_of
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
* `in:` parameter is required
|
49
|
-
* `message:` is optional
|
46
|
+
If your framework doesn't auto require gems, don't forget to do it after require of `active_model` gem.
|
50
47
|
|
51
48
|
```ruby
|
52
|
-
|
53
|
-
|
54
|
-
validates_intersection_of :permission, in: DEFAULT_ROLES
|
55
|
-
end
|
49
|
+
require "active_model"
|
50
|
+
require "active_model_validates_intersection_of"
|
56
51
|
```
|
57
52
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
53
|
+
## Usage
|
54
|
+
|
55
|
+
### Parameters
|
56
|
+
|
57
|
+
* `:in` - Parameter is required. (Supports: Array, proc and lambda)
|
58
|
+
* `:within` - *Optional:* A synonym(alias) for `:in`
|
59
|
+
* `:message` - *Optional:* Specifies a custom error message
|
64
60
|
|
65
|
-
|
61
|
+
### Validation
|
62
|
+
|
63
|
+
You can use the intersection validation in three differents ways: (Feel free to use what you liked more :))
|
66
64
|
|
67
65
|
```ruby
|
68
66
|
class User < ActiveRecord::Base
|
69
67
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
70
|
-
|
68
|
+
|
69
|
+
# Using the alias validates_intersection_of
|
70
|
+
validates_intersection_of :permission, in: DEFAULT_PERMISSION, message: "invalid permission"
|
71
|
+
validates_intersection_of :permission, within: DEFAULT_PERMISSION, message: "invalid permission"
|
72
|
+
|
73
|
+
# proc and lambda support
|
74
|
+
validates_intersection_of :permission, in: proc { DEFAULT_PERMISSION }, message: "invalid permission"
|
75
|
+
validates_intersection_of :permission, in: lambda { |l| DEFAULT_PERMISSION }, message: "invalid permission"
|
76
|
+
|
77
|
+
# Using the validator explicit:
|
78
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
|
79
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], within: DEFAULT_PERMISSION
|
80
|
+
|
81
|
+
#Using the validator implicit:
|
82
|
+
validates :permission, intersection: { in: DEFAULT_PERMISSION, message: "invalid permission" }
|
83
|
+
validates :permission, intersection: { within: DEFAULT_PERMISSION, message: "invalid permission" }
|
84
|
+
|
71
85
|
end
|
72
86
|
```
|
73
87
|
|
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activemodel", ">=
|
22
|
-
spec.add_development_dependency "bundler", "~> 1
|
23
|
-
spec.add_development_dependency "rake", "~>
|
21
|
+
spec.add_dependency "activemodel", ">= 5.0.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 2.1"
|
23
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
25
|
spec.add_development_dependency "coveralls"
|
26
|
+
spec.add_development_dependency "simplecov"
|
26
27
|
end
|
@@ -1,3 +1,4 @@
|
|
1
1
|
require "active_model_validates_intersection_of/version.rb"
|
2
|
-
require "active_model_validates_intersection_of/
|
3
|
-
require "
|
2
|
+
require "active_model_validates_intersection_of/validator.rb"
|
3
|
+
require "intersection_validator.rb"
|
4
|
+
require "validates_intersection_of_alias.rb"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveModelValidatesIntersectionOf
|
2
|
+
class Validator < ActiveModel::EachValidator
|
3
|
+
ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " \
|
4
|
+
"and must be supplied as the :in (or :within) option"
|
5
|
+
|
6
|
+
def check_validity!
|
7
|
+
unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
|
8
|
+
raise ArgumentError, ERROR_MESSAGE
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_each(record, attribute, value)
|
13
|
+
raise ArgumentError, "value must be an array" unless value.is_a?(Array)
|
14
|
+
|
15
|
+
if (value - members(record)).any?
|
16
|
+
record.errors.add(attribute, :inclusion, **options.except(:in, :within).merge!(value: value))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def members(record)
|
23
|
+
members = if delimiter.respond_to?(:call)
|
24
|
+
delimiter.call(record)
|
25
|
+
elsif delimiter.respond_to?(:to_sym)
|
26
|
+
record.send(delimiter)
|
27
|
+
else
|
28
|
+
delimiter
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def delimiter
|
33
|
+
@delimiter ||= options[:in] || options[:within]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
IntersectionValidator = Class.new(ActiveModelValidatesIntersectionOf::Validator)
|
File without changes
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require_relative "../shared.rb"
|
3
|
+
|
4
|
+
RSpec.describe IntersectionValidator do
|
5
|
+
class BadSetupValidator
|
6
|
+
include ActiveModel::Validations
|
7
|
+
attr_accessor :list
|
8
|
+
end
|
9
|
+
|
10
|
+
context "setup" do
|
11
|
+
describe "with neither :in nor :within configuration" do
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetupValidator.class_eval("validates :list, intersection: true")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "validation" do
|
21
|
+
context "with :in option" do
|
22
|
+
class ListAliasValidator
|
23
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
24
|
+
include ActiveModel::Validations
|
25
|
+
attr_accessor :list
|
26
|
+
validates :list, intersection: { in: VALID_ARRAY, message: "not valid" }
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:valid_partial_array) { ListAliasValidator::VALID_ARRAY.sample(2) }
|
30
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
31
|
+
subject { ListAliasValidator.new }
|
32
|
+
|
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
|
+
|
42
|
+
context "with :within option" do
|
43
|
+
class WithinOptionListAliasValidator
|
44
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
45
|
+
include ActiveModel::Validations
|
46
|
+
attr_accessor :list
|
47
|
+
validates :list, intersection: { within: VALID_ARRAY, message: "not valid" }
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:valid_partial_array) { WithinOptionListAliasValidator::VALID_ARRAY.sample(2) }
|
51
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
52
|
+
|
53
|
+
subject { WithinOptionListAliasValidator.new }
|
54
|
+
|
55
|
+
context "validation with :in option" do
|
56
|
+
describe "with a valid list" do
|
57
|
+
it_behaves_like "valid object"
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "with an invalid list" do
|
61
|
+
it_behaves_like "invalid object"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "validate with lambda" do
|
67
|
+
class SomeList
|
68
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
69
|
+
include ActiveModel::Validations
|
70
|
+
attr_accessor :list
|
71
|
+
validates_intersection_of :list, in: lambda {|a| VALID_ARRAY }, message: "not valid"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "is valid" do
|
75
|
+
list = SomeList.new
|
76
|
+
list.list = ["z"]
|
77
|
+
expect(list.valid?).to eq true
|
78
|
+
end
|
79
|
+
|
80
|
+
it "is invalid" do
|
81
|
+
list = SomeList.new
|
82
|
+
list.list = ["G"]
|
83
|
+
expect(list.valid?).to eq false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "validate with proc" do
|
88
|
+
class ProcList
|
89
|
+
VALID_ARRAY = ["z", "q", 5 , 3]
|
90
|
+
include ActiveModel::Validations
|
91
|
+
attr_accessor :list
|
92
|
+
validates_intersection_of :list, in: proc { VALID_ARRAY }, message: "not valid"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "is valid" do
|
96
|
+
list = ProcList.new
|
97
|
+
list.list = ["q"]
|
98
|
+
expect(list.valid?).to eq true
|
99
|
+
end
|
100
|
+
|
101
|
+
it "is invalid" do
|
102
|
+
list = ProcList.new
|
103
|
+
list.list = [10]
|
104
|
+
expect(list.valid?).to eq false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "validate with symbol" do
|
109
|
+
class SymList
|
110
|
+
VALID_ARRAY = ["z", "d", 5 , 6]
|
111
|
+
include ActiveModel::Validations
|
112
|
+
attr_accessor :list
|
113
|
+
validates_intersection_of :list, in: :valid_options, message: "not valid"
|
114
|
+
|
115
|
+
def valid_options
|
116
|
+
VALID_ARRAY
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it "is valid" do
|
121
|
+
list = SymList.new
|
122
|
+
list.list = ["d"]
|
123
|
+
expect(list.valid?).to eq true
|
124
|
+
end
|
125
|
+
|
126
|
+
it "is invalid" do
|
127
|
+
list = SymList.new
|
128
|
+
list.list = [8]
|
129
|
+
expect(list.valid?).to eq false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "validate with value as string" do
|
134
|
+
class SomeInvalidList
|
135
|
+
include ActiveModel::Validations
|
136
|
+
attr_accessor :list
|
137
|
+
validates_intersection_of :list, in: proc { ['test'] }, message: "not valid"
|
138
|
+
end
|
139
|
+
|
140
|
+
it "causes an exception" do
|
141
|
+
list = SomeInvalidList.new
|
142
|
+
list.list = "d"
|
143
|
+
expect {
|
144
|
+
list.valid?
|
145
|
+
}.to raise_error(ArgumentError, "value must be an array")
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -5,37 +5,65 @@ RSpec.describe ActiveModel::Validations::HelperMethods do
|
|
5
5
|
class BadSetup
|
6
6
|
include ActiveModel::Validations
|
7
7
|
attr_accessor :list
|
8
|
-
validates_intersection_of :list
|
9
8
|
end
|
10
9
|
|
11
|
-
let(:badsetup) { BadSetup.new }
|
12
|
-
|
13
10
|
context "setup" do
|
14
|
-
describe "
|
15
|
-
|
11
|
+
describe "with neither :in nor :within configuration" do
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetup.class_eval("validates_intersection_of :list")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
context "validation" do
|
21
|
+
context "with :in option" do
|
22
|
+
class List
|
23
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
24
|
+
include ActiveModel::Validations
|
25
|
+
attr_accessor :list
|
26
|
+
validates_intersection_of :list, in: VALID_ARRAY, message: "not valid"
|
27
|
+
end
|
20
28
|
|
21
|
-
|
22
|
-
|
29
|
+
let(:valid_partial_array) { List::VALID_ARRAY.sample(2) }
|
30
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
23
31
|
|
24
|
-
|
25
|
-
include ActiveModel::Validations
|
26
|
-
attr_accessor :list
|
27
|
-
validates_intersection_of :list, in: VALID_ARRAY, message: "not valid"
|
28
|
-
end
|
32
|
+
subject { List.new }
|
29
33
|
|
30
|
-
|
34
|
+
context "validation" do
|
35
|
+
describe "with a valid list" do
|
36
|
+
it_behaves_like "valid object"
|
37
|
+
end
|
31
38
|
|
32
|
-
|
33
|
-
|
34
|
-
|
39
|
+
describe "with an invalid list" do
|
40
|
+
it_behaves_like "invalid object"
|
41
|
+
end
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
|
-
|
38
|
-
|
45
|
+
context "with :within option" do
|
46
|
+
class ListWithinOption
|
47
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
48
|
+
include ActiveModel::Validations
|
49
|
+
attr_accessor :list
|
50
|
+
validates_intersection_of :list, within: VALID_ARRAY, message: "not valid"
|
51
|
+
end
|
52
|
+
|
53
|
+
let(:valid_partial_array) { ListWithinOption::VALID_ARRAY.sample(2) }
|
54
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
55
|
+
|
56
|
+
subject { ListWithinOption.new }
|
57
|
+
|
58
|
+
context "validation" do
|
59
|
+
describe "with a valid list" do
|
60
|
+
it_behaves_like "valid object"
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "with an invalid list" do
|
64
|
+
it_behaves_like "invalid object"
|
65
|
+
end
|
66
|
+
end
|
39
67
|
end
|
40
68
|
end
|
41
69
|
end
|
@@ -0,0 +1,66 @@
|
|
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
|
+
end
|
9
|
+
|
10
|
+
context "setup" do
|
11
|
+
describe "with neither :in nor :within configuration" do
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetupValidator.class_eval("validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list]")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "validation" do
|
21
|
+
context "with :in option" do
|
22
|
+
class ListValidator
|
23
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
24
|
+
include ActiveModel::Validations
|
25
|
+
attr_accessor :list
|
26
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list], in: VALID_ARRAY, message: "not valid"
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:valid_partial_array) { ListValidator::VALID_ARRAY.sample(2) }
|
30
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
31
|
+
subject { ListValidator.new }
|
32
|
+
|
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
|
+
|
42
|
+
context "with :within option" do
|
43
|
+
class WithinOptionListValidator
|
44
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
45
|
+
include ActiveModel::Validations
|
46
|
+
attr_accessor :list
|
47
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list], within: VALID_ARRAY, message: "not valid"
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:valid_partial_array) { WithinOptionListValidator::VALID_ARRAY.sample(2) }
|
51
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
52
|
+
|
53
|
+
subject { WithinOptionListValidator.new }
|
54
|
+
|
55
|
+
context "validation with :in option" do
|
56
|
+
describe "with a valid list" do
|
57
|
+
it_behaves_like "valid object"
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "with an invalid list" do
|
61
|
+
it_behaves_like "invalid object"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/shared.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
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
1
|
RSpec.shared_examples "valid object" do
|
8
2
|
it "the subject should be valid" do
|
9
3
|
subject.list = valid_partial_array
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require "coveralls"
|
5
|
+
Coveralls.wear!
|
6
|
+
|
1
7
|
require "bundler/setup"
|
2
8
|
require "active_model"
|
3
9
|
require "active_model_validates_intersection_of"
|
4
|
-
require 'coveralls'
|
5
|
-
Coveralls.wear!
|
6
10
|
|
7
11
|
RSpec.configure do |config|
|
8
12
|
# Enable flags like --only-failures and --next-failure
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_validates_intersection_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Biriba
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,42 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1
|
33
|
+
version: '2.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1
|
40
|
+
version: '2.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: A custom validation for your Active Model that check if an array is included
|
84
98
|
in another one
|
85
99
|
email:
|
@@ -91,16 +105,19 @@ files:
|
|
91
105
|
- ".gitignore"
|
92
106
|
- ".rspec"
|
93
107
|
- ".travis.yml"
|
108
|
+
- CHANGELOG.md
|
94
109
|
- Gemfile
|
95
110
|
- README.md
|
96
111
|
- Rakefile
|
97
112
|
- active_model_validates_intersection_of.gemspec
|
98
113
|
- lib/active_model_validates_intersection_of.rb
|
99
|
-
- lib/active_model_validates_intersection_of/
|
100
|
-
- lib/active_model_validates_intersection_of/validates_intersection_of_alias.rb
|
114
|
+
- lib/active_model_validates_intersection_of/validator.rb
|
101
115
|
- lib/active_model_validates_intersection_of/version.rb
|
102
|
-
-
|
116
|
+
- lib/intersection_validator.rb
|
117
|
+
- lib/validates_intersection_of_alias.rb
|
118
|
+
- spec/lib/intersection_validator_spec.rb
|
103
119
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
120
|
+
- spec/lib/validator_spec.rb
|
104
121
|
- spec/shared.rb
|
105
122
|
- spec/spec_helper.rb
|
106
123
|
homepage: https://github.com/rafaelbiriba/active_model_validates_intersection_of
|
@@ -122,14 +139,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
139
|
- !ruby/object:Gem::Version
|
123
140
|
version: '0'
|
124
141
|
requirements: []
|
125
|
-
|
126
|
-
rubygems_version: 2.6.12
|
142
|
+
rubygems_version: 3.0.3
|
127
143
|
signing_key:
|
128
144
|
specification_version: 4
|
129
145
|
summary: A custom validation for your Active Model that check if an array is included
|
130
146
|
in another one
|
131
147
|
test_files:
|
132
|
-
- spec/lib/
|
148
|
+
- spec/lib/intersection_validator_spec.rb
|
133
149
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
150
|
+
- spec/lib/validator_spec.rb
|
134
151
|
- spec/shared.rb
|
135
152
|
- spec/spec_helper.rb
|
@@ -1,11 +0,0 @@
|
|
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
|
@@ -1,41 +0,0 @@
|
|
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
|