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 +4 -4
- data/README.md +21 -3
- data/lib/boolean_validator.rb +4 -1
- data/lib/extensions/active_model/type/boolean.rb +12 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 211c6e902816bcf08e1ca63ef7b7c6b4749147a324b5c6269b5ce1957082d440
|
4
|
+
data.tar.gz: d25fd380c3863e8eaadc1c0ee00b42635d482a82a475e5bc06093f57519fd8ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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 :
|
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 :
|
50
|
+
validate :is_public_before_type_cast, boolean: { message: 'Customize your error message' }
|
33
51
|
end
|
34
52
|
```
|
35
53
|
|
data/lib/boolean_validator.rb
CHANGED
@@ -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
|
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.
|
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
|
-
|
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: {}
|