active_model_validates_intersection_of 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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +14 -8
- data/lib/active_model_validates_intersection_of/{inclusion_intersection_validator.rb → validator.rb} +3 -2
- data/lib/active_model_validates_intersection_of/version.rb +1 -1
- data/lib/active_model_validates_intersection_of.rb +2 -2
- data/lib/{active_model_validates_intersection_of/validates_intersection_of_alias.rb → validates_intersection_of_alias.rb} +0 -0
- data/spec/lib/validates_intersection_of_alias_spec.rb +42 -15
- data/spec/lib/validator_spec.rb +65 -0
- data/spec/spec_helper.rb +1 -1
- metadata +7 -6
- data/spec/lib/inclusion_intersection_validator_spec.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6b8262630e0adf42ea5e3e1695ac77c6155b0f1
|
4
|
+
data.tar.gz: a272709e84de27dd6d85981f28621d19586d7b7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba9de8512b66fd75d3a90f9cb75823cf050a7dbd7e51b68e41ca3f30e79b15493e17f209111dcd7638b0aa94b77cc372b34235a05d3dea62af18843c81efc07d
|
7
|
+
data.tar.gz: 354b6a7bbafd6c3d48afbbf943d049a4afd1038a6a82d3c9a382a0580f90365df48311e66af09487c6cef7a82dedb3914b5cff726a21f538e45b2d5f5d8c8c34
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -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**!
|
@@ -43,22 +43,28 @@ Or install it yourself as:
|
|
43
43
|
|
44
44
|
$ gem install active_model_validates_intersection_of
|
45
45
|
|
46
|
+
If your framework doesn't auto require gems, don't forget to do it after require of `active_model` gem.
|
47
|
+
|
48
|
+
require "active_model"
|
49
|
+
require "active_model_validates_intersection_of"
|
50
|
+
|
46
51
|
## Usage
|
47
52
|
|
48
|
-
* `
|
49
|
-
* `
|
53
|
+
* `:in` - Parameter is required
|
54
|
+
* `:within` - A synonym(alias) for `:in`
|
55
|
+
* `:message` - Specifies a custom error message (optional)
|
50
56
|
|
51
57
|
```ruby
|
52
58
|
class User < ActiveRecord::Base
|
53
59
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
54
|
-
validates_intersection_of :permission, in:
|
60
|
+
validates_intersection_of :permission, in: DEFAULT_PERMISSION
|
55
61
|
end
|
56
62
|
```
|
57
63
|
|
58
64
|
```ruby
|
59
65
|
class User < ActiveRecord::Base
|
60
66
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
61
|
-
validates_intersection_of :permission,
|
67
|
+
validates_intersection_of :permission, within: DEFAULT_PERMISSION, message: "invalid permission"
|
62
68
|
end
|
63
69
|
```
|
64
70
|
|
@@ -67,7 +73,7 @@ You can also use the custom validation directly:
|
|
67
73
|
```ruby
|
68
74
|
class User < ActiveRecord::Base
|
69
75
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
70
|
-
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in:
|
76
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
|
71
77
|
end
|
72
78
|
```
|
73
79
|
|
data/lib/active_model_validates_intersection_of/{inclusion_intersection_validator.rb → validator.rb}
RENAMED
@@ -1,10 +1,11 @@
|
|
1
1
|
module ActiveModelValidatesIntersectionOf
|
2
2
|
class Validator < ActiveModel::EachValidator
|
3
3
|
def validate_each(record, attribute, value)
|
4
|
-
delimiter = options[:in]
|
4
|
+
delimiter = options[:in] || options[:within]
|
5
5
|
raise(ArgumentError, "An array must be supplied as the :in option of the configuration hash") unless delimiter.kind_of?(Array)
|
6
|
+
|
6
7
|
if (value - delimiter).any?
|
7
|
-
record.errors.add(attribute, :inclusion, options.except(:in).merge!(value: value))
|
8
|
+
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(value: value))
|
8
9
|
end
|
9
10
|
end
|
10
11
|
end
|
@@ -1,3 +1,3 @@
|
|
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 "validates_intersection_of_alias.rb"
|
File without changes
|
@@ -11,31 +11,58 @@ RSpec.describe ActiveModel::Validations::HelperMethods do
|
|
11
11
|
let(:badsetup) { BadSetup.new }
|
12
12
|
|
13
13
|
context "setup" do
|
14
|
-
describe "
|
14
|
+
describe "with neither :in nor :within configuration" do
|
15
15
|
it_behaves_like "invalid configuration"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
context "validation" do
|
20
|
+
context "with :in option" do
|
21
|
+
class List
|
22
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
23
|
+
include ActiveModel::Validations
|
24
|
+
attr_accessor :list
|
25
|
+
validates_intersection_of :list, in: VALID_ARRAY, message: "not valid"
|
26
|
+
end
|
20
27
|
|
21
|
-
|
22
|
-
|
28
|
+
let(:valid_partial_array) { List::VALID_ARRAY.sample(2) }
|
29
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
23
30
|
|
24
|
-
|
25
|
-
include ActiveModel::Validations
|
26
|
-
attr_accessor :list
|
27
|
-
validates_intersection_of :list, in: VALID_ARRAY, message: "not valid"
|
28
|
-
end
|
31
|
+
subject { List.new }
|
29
32
|
|
30
|
-
|
33
|
+
context "validation" do
|
34
|
+
describe "with a valid list" do
|
35
|
+
it_behaves_like "valid object"
|
36
|
+
end
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
38
|
+
describe "with an invalid list" do
|
39
|
+
it_behaves_like "invalid object"
|
40
|
+
end
|
41
|
+
end
|
35
42
|
end
|
36
43
|
|
37
|
-
|
38
|
-
|
44
|
+
context "with :within option" do
|
45
|
+
class ListWithinOption
|
46
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
47
|
+
include ActiveModel::Validations
|
48
|
+
attr_accessor :list
|
49
|
+
validates_intersection_of :list, within: VALID_ARRAY, message: "not valid"
|
50
|
+
end
|
51
|
+
|
52
|
+
let(:valid_partial_array) { ListWithinOption::VALID_ARRAY.sample(2) }
|
53
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
54
|
+
|
55
|
+
subject { ListWithinOption.new }
|
56
|
+
|
57
|
+
context "validation" do
|
58
|
+
describe "with a valid list" do
|
59
|
+
it_behaves_like "valid object"
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "with an invalid list" do
|
63
|
+
it_behaves_like "invalid object"
|
64
|
+
end
|
65
|
+
end
|
39
66
|
end
|
40
67
|
end
|
41
68
|
end
|
@@ -0,0 +1,65 @@
|
|
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 "with neither :in nor :within configuration" do
|
15
|
+
it_behaves_like "invalid configuration"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "validation" do
|
20
|
+
context "with :in option" do
|
21
|
+
class ListValidator
|
22
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
23
|
+
include ActiveModel::Validations
|
24
|
+
attr_accessor :list
|
25
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list], in: VALID_ARRAY, message: "not valid"
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:valid_partial_array) { ListValidator::VALID_ARRAY.sample(2) }
|
29
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
30
|
+
subject { ListValidator.new }
|
31
|
+
|
32
|
+
describe "with a valid list" do
|
33
|
+
it_behaves_like "valid object"
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "with an invalid list" do
|
37
|
+
it_behaves_like "invalid object"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with :within option" do
|
42
|
+
class WithinOptionListValidator
|
43
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
44
|
+
include ActiveModel::Validations
|
45
|
+
attr_accessor :list
|
46
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list], within: VALID_ARRAY, message: "not valid"
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:valid_partial_array) { WithinOptionListValidator::VALID_ARRAY.sample(2) }
|
50
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
51
|
+
|
52
|
+
subject { WithinOptionListValidator.new }
|
53
|
+
|
54
|
+
context "validation with :in option" do
|
55
|
+
describe "with a valid list" do
|
56
|
+
it_behaves_like "valid object"
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "with an invalid list" do
|
60
|
+
it_behaves_like "invalid object"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
version: 1.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: 2017-06-
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -91,16 +91,17 @@ files:
|
|
91
91
|
- ".gitignore"
|
92
92
|
- ".rspec"
|
93
93
|
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
94
95
|
- Gemfile
|
95
96
|
- README.md
|
96
97
|
- Rakefile
|
97
98
|
- active_model_validates_intersection_of.gemspec
|
98
99
|
- 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
|
100
|
+
- lib/active_model_validates_intersection_of/validator.rb
|
101
101
|
- lib/active_model_validates_intersection_of/version.rb
|
102
|
-
-
|
102
|
+
- lib/validates_intersection_of_alias.rb
|
103
103
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
104
|
+
- spec/lib/validator_spec.rb
|
104
105
|
- spec/shared.rb
|
105
106
|
- spec/spec_helper.rb
|
106
107
|
homepage: https://github.com/rafaelbiriba/active_model_validates_intersection_of
|
@@ -129,7 +130,7 @@ specification_version: 4
|
|
129
130
|
summary: A custom validation for your Active Model that check if an array is included
|
130
131
|
in another one
|
131
132
|
test_files:
|
132
|
-
- spec/lib/inclusion_intersection_validator_spec.rb
|
133
133
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
134
|
+
- spec/lib/validator_spec.rb
|
134
135
|
- spec/shared.rb
|
135
136
|
- spec/spec_helper.rb
|
@@ -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
|