active_model_validates_intersection_of 0.1.0 → 1.0.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: 0ef8977cca2302df90c919bd3212ee38c004a503
4
- data.tar.gz: 21dab974646c8a4cfed5742a7ad6627316ea350d
3
+ metadata.gz: d6b8262630e0adf42ea5e3e1695ac77c6155b0f1
4
+ data.tar.gz: a272709e84de27dd6d85981f28621d19586d7b7f
5
5
  SHA512:
6
- metadata.gz: 6452a2c54d3754d6a1947b5e30784bea2a899e0e4e47662dd51dcea9487c34ea801d80b264d1127aed41378ec114b912135c95065143bbaf2c1352b520d4d431
7
- data.tar.gz: ebdfcd71070d2e4132dfaed773684a9b8374917461ff6e6bd862242b62c5e46c034c18ab6c742c20adfd4e8788b5fe03f62646501d1442db3a57e0b6795f21e7
6
+ metadata.gz: ba9de8512b66fd75d3a90f9cb75823cf050a7dbd7e51b68e41ca3f30e79b15493e17f209111dcd7638b0aa94b77cc372b34235a05d3dea62af18843c81efc07d
7
+ data.tar.gz: 354b6a7bbafd6c3d48afbbf943d049a4afd1038a6a82d3c9a382a0580f90365df48311e66af09487c6cef7a82dedb3914b5cff726a21f538e45b2d5f5d8c8c34
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## v1.0.0 (2017-06-06)
2
+
3
+ - Releasing 1.0 version
4
+ - Adding `:within` parameter support as an alias for `:in`
5
+
6
+ ## v0.1.0 (2017-06-01)
7
+
8
+ - Initial release
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: DEFAULT_ROLES
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
- user = User.new(permission: ["read", "admin"])
25
- user.valid? #false
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
- * `in:` parameter is required
49
- * `message:` is optional
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: DEFAULT_ROLES
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, in: DEFAULT_ROLES, message: "invalid 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: VALID_ARRAY
76
+ validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
71
77
  end
72
78
  ```
73
79
 
@@ -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
  module ActiveModelValidatesIntersectionOf
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  require "active_model_validates_intersection_of/version.rb"
2
- require "active_model_validates_intersection_of/inclusion_intersection_validator.rb"
3
- require "active_model_validates_intersection_of/validates_intersection_of_alias.rb"
2
+ require "active_model_validates_intersection_of/validator.rb"
3
+ require "validates_intersection_of_alias.rb"
@@ -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 "without an :in configuration" do
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
- VALID_ARRAY = ["z", "x", 5 , 6]
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
- let(:valid_partial_array) { VALID_ARRAY.sample(2) }
22
- let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
28
+ let(:valid_partial_array) { List::VALID_ARRAY.sample(2) }
29
+ let(:invalid_partial_array) { valid_partial_array + ["invalid"] }
23
30
 
24
- class List
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
- subject { List.new }
33
+ context "validation" do
34
+ describe "with a valid list" do
35
+ it_behaves_like "valid object"
36
+ end
31
37
 
32
- context "validation" do
33
- describe "with a valid list" do
34
- it_behaves_like "valid object"
38
+ describe "with an invalid list" do
39
+ it_behaves_like "invalid object"
40
+ end
41
+ end
35
42
  end
36
43
 
37
- describe "with an invalid list" do
38
- it_behaves_like "invalid object"
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
@@ -1,7 +1,7 @@
1
1
  require "bundler/setup"
2
2
  require "active_model"
3
3
  require "active_model_validates_intersection_of"
4
- require 'coveralls'
4
+ require "coveralls"
5
5
  Coveralls.wear!
6
6
 
7
7
  RSpec.configure do |config|
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.1.0
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-01 00:00:00.000000000 Z
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/inclusion_intersection_validator.rb
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
- - spec/lib/inclusion_intersection_validator_spec.rb
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