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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6b8262630e0adf42ea5e3e1695ac77c6155b0f1
4
- data.tar.gz: a272709e84de27dd6d85981f28621d19586d7b7f
3
+ metadata.gz: 76242e3e9b01b1b5d32baf79a3e562c640b9a070
4
+ data.tar.gz: e284f822c443f3670ee1e4a41ab0a50d69a743ea
5
5
  SHA512:
6
- metadata.gz: ba9de8512b66fd75d3a90f9cb75823cf050a7dbd7e51b68e41ca3f30e79b15493e17f209111dcd7638b0aa94b77cc372b34235a05d3dea62af18843c81efc07d
7
- data.tar.gz: 354b6a7bbafd6c3d48afbbf943d049a4afd1038a6a82d3c9a382a0580f90365df48311e66af09487c6cef7a82dedb3914b5cff726a21f538e45b2d5f5d8c8c34
6
+ metadata.gz: 4e91fef8acba499072b1dc4822fd655a8675e69685b0fff02d7979acd022878a815365a23d92e938f8cbadffcb201dd769e5cdade3d5d80238c42f4b3f0dec7a
7
+ data.tar.gz: 844fcb44851f526de9e6fc0d9789ec81035b7c5ab1a6036790a0ce73ae4ef1a13a0ac82e3ab79e5ef2721f50076cb2240ef0dfe0067596972c6e9d1997fe12e5
@@ -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
- - Releasing 1.0 version
4
- - Adding `:within` parameter support as an alias for `:in`
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 'active_model_validates_intersection_of'
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
- require "active_model"
49
- require "active_model_validates_intersection_of"
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 (optional)
58
+ * `:within` - *Optional:* A synonym(alias) for `:in`
59
+ * `:message` - *Optional:* Specifies a custom error message
56
60
 
57
- ```ruby
58
- class User < ActiveRecord::Base
59
- DEFAULT_PERMISSION = ["read", "write", "share"]
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
- You can also use the custom validation directly:
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
- ```ruby
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
 
@@ -1,3 +1,4 @@
1
1
  require "active_model_validates_intersection_of/version.rb"
2
2
  require "active_model_validates_intersection_of/validator.rb"
3
- require "validates_intersection_of_alias.rb"
3
+ require "intersection_validator.rb"
4
+ require "validates_intersection_of_alias.rb"
@@ -9,4 +9,4 @@ module ActiveModelValidatesIntersectionOf
9
9
  end
10
10
  end
11
11
  end
12
- end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveModelValidatesIntersectionOf
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0".freeze
3
3
  end
@@ -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.0.0
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