boolean_validator 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: cea3c2b44f571c7f1387a1d154e27e70e7dea6358e7fc9c9728221743dc4ad22
4
- data.tar.gz: 6c882fcd039e8bc676353e84134a9fe8a833f78c15fb950cafa83a85909b8d39
3
+ metadata.gz: 211c6e902816bcf08e1ca63ef7b7c6b4749147a324b5c6269b5ce1957082d440
4
+ data.tar.gz: d25fd380c3863e8eaadc1c0ee00b42635d482a82a475e5bc06093f57519fd8ed
5
5
  SHA512:
6
- metadata.gz: 5ac50b6791a526d6ddc29838e0aa7990a7ed01d0dd0f2a879799799eeb2b619dcb2faa19f928de39cd9ea833d2f3bc7f8605a2a610fd5b10c1ad200311fc7c65
7
- data.tar.gz: 16c521e13d67bbf99b8801a159206391931b65c872f0dc1d3c31cab2e0e06ed0963916b6bfc4d9e4f2e472f82f4126b2d17c9842b0fb2651d8f9127791bb7ce8
6
+ metadata.gz: fce0aab9b8ad438fce116f9ea1365d42322ee20e6332f044c566c6df9a48340f11b26078fa4d9570ada51b43d52628e345b36e238af8ac25efc2008e5b09b3fa
7
+ data.tar.gz: 72253cfd635c94da5ebea3956b2ca093b8fa72593b2740f108003329947d1519a5068ce43167d41146aa3c9e596f701b74bce8e8e6397d8458e4546c2652f38d
data/README.md CHANGED
@@ -18,10 +18,28 @@ And then execute:
18
18
 
19
19
  ## Usage
20
20
 
21
- In your model:
21
+ You would have implemented a `Post` model like this:
22
+
23
+ ```ruby
24
+ class Post < ActiveRecord::Base
25
+ validates :is_public_before_type_cast, inclusion: { in: [true, false] }
26
+ end
27
+ ```
28
+
29
+ or this (though this is wrong because this *validation* actually *validates nothing*):
30
+
31
+ ```ruby
32
+ class Post < ActiveRecord::Base
33
+ validates :is_public, inclusion: { in: [true, false] }
34
+ end
35
+ ```
36
+
37
+
38
+ To validate boolean value with this gem, try:
39
+
22
40
  ```ruby
23
41
  class Post < ActiveRecord::Base
24
- validates :is_public, boolean: true
42
+ validates :is_public_before_type_cast, boolean: true
25
43
  end
26
44
  ```
27
45
 
@@ -29,7 +47,7 @@ or you can pass an option like:
29
47
 
30
48
  ```ruby
31
49
  class Post < ActiveRecord::Base
32
- validate :is_public, boolean: { message: 'Customize your error message' }
50
+ validate :is_public_before_type_cast, boolean: { message: 'Customize your error message' }
33
51
  end
34
52
  ```
35
53
 
@@ -1,8 +1,11 @@
1
1
  require 'active_model/validator'
2
+ require 'extensions/active_model/type/boolean'
2
3
 
3
4
  class BooleanValidator < ActiveModel::EachValidator
5
+ BOOLEAN_VALUES = ActiveModel::Type::Boolean::TRUE_VALUES + ActiveModel::Type::Boolean::FALSE_VALUES
6
+
4
7
  def validate_each(record, attribute, value)
5
- return if [true, false].include?(value)
8
+ return if BOOLEAN_VALUES.include?(value)
6
9
 
7
10
  record.errors.add(attribute, :invalid, options.slice(:message).merge(value: value))
8
11
  end
@@ -0,0 +1,12 @@
1
+ require 'active_model/type'
2
+
3
+ module ActiveModelExtension
4
+ module Boolean
5
+ # FALSE_VALUES are defined in:
6
+ # https://github.com/rails/rails/blob/406d3a926cfcd3724f8002f70346aad95eed4a8c/activemodel/lib/active_model/type/boolean.rb
7
+
8
+ TRUE_VALUES = Set.new([true, 1, "1", "t", "T", "true", "TRUE", "on", "ON"])
9
+ end
10
+ end
11
+
12
+ ActiveModel::Type::Boolean.send(:include, ActiveModelExtension::Boolean)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boolean_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ayumitamai97
@@ -34,7 +34,8 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - lib/boolean_validator.rb
37
- homepage: https://github.com/ayumitamai97
37
+ - lib/extensions/active_model/type/boolean.rb
38
+ homepage: https://github.com/ayumitamai97/boolean_validator
38
39
  licenses:
39
40
  - MIT
40
41
  metadata: {}