active_model_validates_intersection_of 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -2
- data/README.md +22 -18
- data/lib/active_model_validates_intersection_of.rb +2 -1
- data/lib/active_model_validates_intersection_of/validator.rb +1 -1
- data/lib/active_model_validates_intersection_of/version.rb +1 -1
- data/lib/intersection_validator.rb +1 -0
- data/spec/lib/intersection_validator_spec.rb +65 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76242e3e9b01b1b5d32baf79a3e562c640b9a070
|
4
|
+
data.tar.gz: e284f822c443f3670ee1e4a41ab0a50d69a743ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e91fef8acba499072b1dc4822fd655a8675e69685b0fff02d7979acd022878a815365a23d92e938f8cbadffcb201dd769e5cdade3d5d80238c42f4b3f0dec7a
|
7
|
+
data.tar.gz: 844fcb44851f526de9e6fc0d9789ec81035b7c5ab1a6036790a0ce73ae4ef1a13a0ac82e3ab79e5ef2721f50076cb2240ef0dfe0067596972c6e9d1997fe12e5
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
## v1.1.0 (2017-06-06)
|
2
|
+
|
3
|
+
- Implement `validates :list, intersection: { in: ANOTHER_LIST }` see the README for more information
|
4
|
+
|
1
5
|
## v1.0.0 (2017-06-06)
|
2
6
|
|
3
|
-
-
|
4
|
-
-
|
7
|
+
- Release 1.0 version
|
8
|
+
- Add `:within` parameter support as an alias for `:in`
|
5
9
|
|
6
10
|
## v0.1.0 (2017-06-01)
|
7
11
|
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ user.valid? #false
|
|
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:
|
@@ -45,35 +45,39 @@ Or install it yourself as:
|
|
45
45
|
|
46
46
|
If your framework doesn't auto require gems, don't forget to do it after require of `active_model` gem.
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
```ruby
|
49
|
+
require "active_model"
|
50
|
+
require "active_model_validates_intersection_of"
|
51
|
+
```
|
50
52
|
|
51
53
|
## Usage
|
52
54
|
|
55
|
+
### Parameters
|
56
|
+
|
53
57
|
* `:in` - Parameter is required
|
54
|
-
* `:within` - A synonym(alias) for `:in`
|
55
|
-
* `:message` - Specifies a custom error message
|
58
|
+
* `:within` - *Optional:* A synonym(alias) for `:in`
|
59
|
+
* `:message` - *Optional:* Specifies a custom error message
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
validates_intersection_of :permission, in: DEFAULT_PERMISSION
|
61
|
-
end
|
62
|
-
```
|
61
|
+
### Validation
|
62
|
+
|
63
|
+
You can use the intersection validation in three differents ways: (Feel free to use what you liked more :))
|
63
64
|
|
64
65
|
```ruby
|
65
66
|
class User < ActiveRecord::Base
|
66
67
|
DEFAULT_PERMISSION = ["read", "write", "share"]
|
67
|
-
validates_intersection_of :permission, within: DEFAULT_PERMISSION, message: "invalid permission"
|
68
|
-
end
|
69
|
-
```
|
70
68
|
|
71
|
-
|
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
72
|
|
73
|
-
|
74
|
-
class User < ActiveRecord::Base
|
75
|
-
DEFAULT_PERMISSION = ["read", "write", "share"]
|
73
|
+
# Using the validator explicit:
|
76
74
|
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
|
75
|
+
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], within: DEFAULT_PERMISSION
|
76
|
+
|
77
|
+
#Using the validator implicit:
|
78
|
+
validates :permission, intersection: { in: DEFAULT_PERMISSION, message: "invalid permission" }
|
79
|
+
validates :permission, intersection: { within: DEFAULT_PERMISSION, message: "invalid permission" }
|
80
|
+
|
77
81
|
end
|
78
82
|
```
|
79
83
|
|
@@ -0,0 +1 @@
|
|
1
|
+
IntersectionValidator = Class.new(ActiveModelValidatesIntersectionOf::Validator)
|
@@ -0,0 +1,65 @@
|
|
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
|
+
validates :list, intersection: true
|
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 ListAliasValidator
|
22
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
23
|
+
include ActiveModel::Validations
|
24
|
+
attr_accessor :list
|
25
|
+
validates :list, intersection: { in: VALID_ARRAY, message: "not valid" }
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:valid_partial_array) { ListAliasValidator::VALID_ARRAY.sample(2) }
|
29
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
30
|
+
subject { ListAliasValidator.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 WithinOptionListAliasValidator
|
43
|
+
VALID_ARRAY = ["a", "b", 1 , 2]
|
44
|
+
include ActiveModel::Validations
|
45
|
+
attr_accessor :list
|
46
|
+
validates :list, intersection: { within: VALID_ARRAY, message: "not valid" }
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:valid_partial_array) { WithinOptionListAliasValidator::VALID_ARRAY.sample(2) }
|
50
|
+
let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
|
51
|
+
|
52
|
+
subject { WithinOptionListAliasValidator.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
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_validates_intersection_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Biriba
|
@@ -99,7 +99,9 @@ files:
|
|
99
99
|
- lib/active_model_validates_intersection_of.rb
|
100
100
|
- lib/active_model_validates_intersection_of/validator.rb
|
101
101
|
- lib/active_model_validates_intersection_of/version.rb
|
102
|
+
- lib/intersection_validator.rb
|
102
103
|
- lib/validates_intersection_of_alias.rb
|
104
|
+
- spec/lib/intersection_validator_spec.rb
|
103
105
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
104
106
|
- spec/lib/validator_spec.rb
|
105
107
|
- spec/shared.rb
|
@@ -130,6 +132,7 @@ specification_version: 4
|
|
130
132
|
summary: A custom validation for your Active Model that check if an array is included
|
131
133
|
in another one
|
132
134
|
test_files:
|
135
|
+
- spec/lib/intersection_validator_spec.rb
|
133
136
|
- spec/lib/validates_intersection_of_alias_spec.rb
|
134
137
|
- spec/lib/validator_spec.rb
|
135
138
|
- spec/shared.rb
|