enum_attributes_validation 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +23 -1
- data/README.md +60 -6
- data/lib/enum_attributes_validation.rb +13 -7
- data/lib/enum_attributes_validation/version.rb +1 -1
- metadata +101 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 355752203802ce0a6de195b9263267011473a55a
|
4
|
+
data.tar.gz: da2be498bfe9b5616bb2af20b5618b09ecad7c86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b01b8c222a306fe8dd916c6752dead5db0e3bcb34010e1a84f3c52dcff7eb8d48aee491f2e549f00d13e2441bd4148d125c568c566183bafd6b6e310ac82b5b6
|
7
|
+
data.tar.gz: 2e67752331b376feb444d4ea28b6036aba4020aa0d29b551a87b032aba186d6eb4146541e015e588e97d85ba55e22078eef1abd09937e65609b9666c9fcd215e
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enum_attributes_validation (0.1.
|
4
|
+
enum_attributes_validation (0.1.7)
|
5
|
+
activerecord
|
6
|
+
activesupport
|
5
7
|
|
6
8
|
GEM
|
7
9
|
remote: https://rubygems.org/
|
@@ -18,11 +20,27 @@ GEM
|
|
18
20
|
minitest (~> 5.1)
|
19
21
|
tzinfo (~> 1.1)
|
20
22
|
arel (8.0.0)
|
23
|
+
byebug (10.0.0)
|
21
24
|
concurrent-ruby (1.0.5)
|
25
|
+
diff-lcs (1.3)
|
22
26
|
i18n (0.9.1)
|
23
27
|
concurrent-ruby (~> 1.0)
|
24
28
|
minitest (5.10.3)
|
25
29
|
rake (10.5.0)
|
30
|
+
rspec (3.7.0)
|
31
|
+
rspec-core (~> 3.7.0)
|
32
|
+
rspec-expectations (~> 3.7.0)
|
33
|
+
rspec-mocks (~> 3.7.0)
|
34
|
+
rspec-core (3.7.1)
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-expectations (3.7.0)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.7.0)
|
39
|
+
rspec-mocks (3.7.0)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.7.0)
|
42
|
+
rspec-support (3.7.1)
|
43
|
+
sqlite3 (1.3.13)
|
26
44
|
thread_safe (0.3.6)
|
27
45
|
tzinfo (1.2.4)
|
28
46
|
thread_safe (~> 0.1)
|
@@ -34,8 +52,12 @@ DEPENDENCIES
|
|
34
52
|
activerecord
|
35
53
|
activesupport
|
36
54
|
bundler (~> 1.16)
|
55
|
+
byebug
|
37
56
|
enum_attributes_validation!
|
57
|
+
i18n
|
38
58
|
rake (~> 10.0)
|
59
|
+
rspec (~> 3.4)
|
60
|
+
sqlite3
|
39
61
|
|
40
62
|
BUNDLED WITH
|
41
63
|
1.16.0
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
|
1
2
|
# EnumAttributesValidation
|
2
3
|
|
3
|
-
Enum Attributes Validation provides validation functionality for enum attributes in models.
|
4
|
+
Enum Attributes Validation provides validation functionality for enum attributes in models. It validates **inclusion** for the specified enum keys. If values are **nil** the record is saved.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -20,19 +21,72 @@ Or install it yourself as:
|
|
20
21
|
|
21
22
|
## Usage
|
22
23
|
|
23
|
-
Use built in method to specify which enum attributes you want to validate
|
24
|
+
Use built in method to specify which enum attributes you want to validate.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Dog < ApplicationRecord
|
28
|
+
enum gender: { male: 0, female: 1 }
|
29
|
+
enum hair_color: { brown: 0, black: 1, blonde: 2 }
|
30
|
+
|
31
|
+
validate_enum_attributes :gender, :hair_color
|
32
|
+
end
|
33
|
+
|
34
|
+
dog = Dog.new(hair_color: :brown, gender: :female)
|
35
|
+
dog.save
|
36
|
+
# => <Dog gender: "female", hair_color: "brown" >
|
37
|
+
|
38
|
+
dog = Dog.new
|
39
|
+
dog.save
|
40
|
+
# => <Dog gender: nil, hair_color: nil >
|
41
|
+
|
42
|
+
dog = Dog.new(hair_color: nil, gender: nil)
|
43
|
+
dog.save
|
44
|
+
# => <Dog gender: nil, hair_color: nil >
|
45
|
+
```
|
46
|
+
|
47
|
+
Or use the singular method.
|
24
48
|
|
25
49
|
```ruby
|
26
|
-
class
|
27
|
-
enum gender { male: 0, female: 1 }
|
50
|
+
class Dog < ApplicationRecord
|
51
|
+
enum gender: { male: 0, female: 1 }
|
52
|
+
enum hair_color: { brown: 0, black: 1, blonde: 2 }
|
28
53
|
|
29
|
-
|
54
|
+
validate_enum_attribute :gender
|
30
55
|
end
|
31
56
|
```
|
32
57
|
|
58
|
+
|
59
|
+
### Custom error messages
|
60
|
+
|
61
|
+
To show a custom error message:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Dog < ApplicationRecord
|
65
|
+
enum gender: { male: 0, female: 1 }
|
66
|
+
|
67
|
+
validate_enum_attribute :gender, message: "Some string msg..."
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Translations
|
72
|
+
|
73
|
+
To use translations you need to update your locale file as follows
|
74
|
+
|
75
|
+
```yml
|
76
|
+
en:
|
77
|
+
activerecord:
|
78
|
+
errors:
|
79
|
+
models:
|
80
|
+
#your_model_name#:
|
81
|
+
attributes:
|
82
|
+
#your_attribute_name#::
|
83
|
+
invalid_enum: "can't be %{value}. Valid values are: %{valid_values}"
|
84
|
+
|
85
|
+
```
|
86
|
+
|
33
87
|
## Contributing
|
34
88
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/CristiRazvi/enum_attributes_validation.
|
36
90
|
|
37
91
|
## License
|
38
92
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'enum_attributes_validation/version'
|
2
2
|
require 'active_support'
|
3
3
|
require 'active_record'
|
4
4
|
|
@@ -16,22 +16,28 @@ module EnumAttributesValidation
|
|
16
16
|
|
17
17
|
def check_enum_invalid_attributes
|
18
18
|
if enum_invalid_attributes.present?
|
19
|
-
enum_invalid_attributes.each do |key,
|
20
|
-
|
19
|
+
enum_invalid_attributes.each do |key, opts|
|
20
|
+
if opts[:message]
|
21
|
+
self.errors.add(:base, opts[:message])
|
22
|
+
else
|
23
|
+
self.errors.add(key, :invalid_enum, value: opts[:value], valid_values: self.class.send(key.to_s.pluralize).keys.sort.join(', '), default: "value provided (#{opts[:value]}) is invalid")
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
26
30
|
class_methods do
|
27
|
-
def validate_enum_attributes(*attributes)
|
31
|
+
def validate_enum_attributes(*attributes, **opts)
|
28
32
|
attributes.each do |attribute|
|
29
33
|
string_attribute = attribute.to_s
|
30
34
|
|
31
35
|
define_method (string_attribute+"=").to_sym do |argument|
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
unless argument.nil?
|
37
|
+
string_argument = argument.to_s
|
38
|
+
self[string_attribute] = string_argument if self.class.send(string_attribute.pluralize).keys.include?(string_argument)
|
39
|
+
self.enum_invalid_attributes[attribute] = opts.merge(value: string_argument) unless self.class.send(string_attribute.pluralize).keys.include?(string_argument)
|
40
|
+
end
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_attributes_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cristi Stefan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: bundler
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +66,76 @@ dependencies:
|
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
68
|
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: i18n
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
41
139
|
description: Enum Attributes Validation provides validation functionality for enum
|
42
140
|
attributes in models.
|
43
141
|
email:
|
@@ -75,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
173
|
version: '0'
|
76
174
|
requirements: []
|
77
175
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.6.
|
176
|
+
rubygems_version: 2.6.14
|
79
177
|
signing_key:
|
80
178
|
specification_version: 4
|
81
179
|
summary: Validate model enum attributes
|