validacity 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -14
- data/lib/generators/validacity/install_generator.rb +4 -4
- data/lib/generators/validacity/templates/application_validation.rb +4 -0
- data/lib/generators/validacity/templates/validation.rb +4 -0
- data/lib/generators/validacity/validation_generator.rb +13 -0
- data/lib/validacity.rb +11 -11
- data/lib/validacity/{base_validator.rb → base_validation.rb} +4 -4
- data/lib/validacity/validatable.rb +16 -16
- data/lib/validacity/version.rb +1 -1
- metadata +6 -6
- data/lib/generators/validacity/templates/application_validator.rb +0 -4
- data/lib/generators/validacity/templates/validator.rb +0 -4
- data/lib/generators/validacity/validator_generator.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66d3268535c64fc2c2771ef680fa9b58e999e74296ce83687309e7e63f9e0975
|
4
|
+
data.tar.gz: 65c93549bfb2dffc693f7dddddcf588e28595de5c10d8c56187492b10497a9a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7392af3b88ec32311a21f80932eb7ff4c798bc2cd5808b88a447fb06e9f2672b935a46bca7fce6080075ad4d3e1ff1ba87a62277ae0f336cde383c289b414fc7
|
7
|
+
data.tar.gz: 9451ed7db81476d1226be00706c6f1d5175e12e9b1f28a45c5da13444a6ffe630a65b3547b967f7d9d58096a27d818191cb8c45ace560b47fe06352a261cc390
|
data/README.md
CHANGED
@@ -1,16 +1,26 @@
|
|
1
1
|
# Validacity
|
2
2
|
|
3
|
-
Move
|
3
|
+
Move validation definitions out of you models and form objects.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
You can
|
7
|
+
You can apply multiple sets of validation rules on-fly,
|
8
|
+
remove any them if not needed and re-validate object against
|
9
|
+
new rules afterwards.
|
8
10
|
|
9
|
-
|
11
|
+
By the way, the validation errors will be putten to the object
|
12
|
+
so all your view helpers will be available to display the error list.
|
10
13
|
|
11
|
-
|
14
|
+
This approach will suitable if you need to validate different states
|
15
|
+
of the same object differently. Say you're using a state machine
|
16
|
+
and each state has it's own field set and the fields from the other
|
17
|
+
satates shouldn't be validated at this time.
|
12
18
|
|
13
|
-
|
19
|
+
Or perhaps you just have to many constructions like
|
20
|
+
`validate :blablabla, presence: true` in your model class and you want to
|
21
|
+
get rid of it so you can focus on buisiness logic.
|
22
|
+
|
23
|
+
You can add a concern to your model so it becomes **validatable**:
|
14
24
|
|
15
25
|
```ruby
|
16
26
|
|
@@ -23,52 +33,78 @@ end
|
|
23
33
|
|
24
34
|
```
|
25
35
|
|
26
|
-
|
36
|
+
Generate new validation:
|
37
|
+
|
38
|
+
```bash
|
39
|
+
|
40
|
+
$ bundle exec rails g validacity:validation UserPersonalData
|
41
|
+
|
42
|
+
```
|
27
43
|
|
28
44
|
```ruby
|
29
45
|
|
30
|
-
# app/
|
46
|
+
# app/validations/user_personal_data_validation.rb
|
31
47
|
|
32
|
-
class
|
48
|
+
class UserPersonalDataValidation
|
33
49
|
validate :name, presence_of: true
|
34
|
-
# ...a ton of different
|
50
|
+
# ...a ton of different validations
|
35
51
|
end
|
36
52
|
|
37
53
|
```
|
38
54
|
|
39
|
-
Now
|
55
|
+
Now let's try to validate your user object:
|
40
56
|
|
41
57
|
|
42
58
|
```ruby
|
43
59
|
|
44
|
-
user.
|
60
|
+
user = User.new
|
61
|
+
|
62
|
+
user.valid? # => false
|
63
|
+
|
64
|
+
user.name = "John"
|
65
|
+
|
66
|
+
uesr.valid? # => true
|
45
67
|
|
46
68
|
```
|
47
69
|
|
48
70
|
## Installation
|
71
|
+
|
49
72
|
Add this line to your application's Gemfile:
|
50
73
|
|
51
74
|
```ruby
|
75
|
+
|
52
76
|
gem 'validacity'
|
77
|
+
|
53
78
|
```
|
54
79
|
|
55
80
|
And then execute:
|
81
|
+
|
56
82
|
```bash
|
83
|
+
|
57
84
|
$ bundle
|
85
|
+
|
58
86
|
```
|
59
87
|
|
60
|
-
Now run the
|
88
|
+
Now run the validation installation command:
|
89
|
+
|
61
90
|
```bash
|
91
|
+
|
62
92
|
$ bundle exec rails g validacity:install
|
93
|
+
|
63
94
|
```
|
64
95
|
|
65
|
-
And the event
|
96
|
+
And the event validation:
|
97
|
+
|
66
98
|
```bash
|
67
|
-
|
99
|
+
|
100
|
+
$ bundle exec rails g validacity:validation Event
|
101
|
+
|
68
102
|
```
|
69
103
|
|
70
104
|
## Contributing
|
105
|
+
|
71
106
|
Contribution directions go here.
|
72
107
|
|
73
108
|
## License
|
109
|
+
|
74
110
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -17,16 +17,16 @@ module Validacity
|
|
17
17
|
require_dependency "validacity"
|
18
18
|
|
19
19
|
Validacity.configure do |config|
|
20
|
-
config.search_paths "#{__dir__}
|
20
|
+
config.search_paths "#{__dir__}/../../app/validations/**/*_validation.rb"
|
21
21
|
end
|
22
22
|
RUBY
|
23
23
|
create_file "config/initializers/validacity_initializer.rb", content
|
24
24
|
end
|
25
25
|
|
26
26
|
def copy_application_policy
|
27
|
-
|
28
|
-
"
|
29
|
-
template "
|
27
|
+
validation_file = File.join("app/validations", class_path,
|
28
|
+
"application_validation.rb")
|
29
|
+
template "application_validation.rb", validation_file
|
30
30
|
end
|
31
31
|
|
32
32
|
include Thor::Actions
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Validacity
|
2
|
+
module Generators
|
3
|
+
class ValidationGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path("templates", __dir__)
|
5
|
+
|
6
|
+
def create_validation
|
7
|
+
validation_file = File.join("app/validations", class_path,
|
8
|
+
"#{file_name}_validation.rb")
|
9
|
+
template "validation.rb", validation_file
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/validacity.rb
CHANGED
@@ -3,7 +3,7 @@ require "active_model"
|
|
3
3
|
|
4
4
|
require "validacity/version"
|
5
5
|
require "validacity/configuration"
|
6
|
-
require "validacity/
|
6
|
+
require "validacity/base_validation"
|
7
7
|
require "validacity/validatable"
|
8
8
|
|
9
9
|
module Validacity
|
@@ -16,23 +16,23 @@ module Validacity
|
|
16
16
|
yield configuration if block_given?
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
20
|
-
@
|
19
|
+
def validations
|
20
|
+
@_validations ||= {}
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
|
25
|
-
return
|
23
|
+
def find_validation(name)
|
24
|
+
validation = validations[name]
|
25
|
+
return validation if validation
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
reload_validations
|
28
|
+
validations[name]
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def
|
34
|
-
Dir.glob(configuration.search_paths) do |
|
35
|
-
send(require_method,
|
33
|
+
def reload_validations
|
34
|
+
Dir.glob(configuration.search_paths) do |validation|
|
35
|
+
send(require_method, validation)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module Validacity
|
2
|
-
class
|
2
|
+
class BaseValidation
|
3
3
|
include ::ActiveModel::Validations
|
4
4
|
|
5
5
|
class << self
|
6
6
|
def inherited(klass)
|
7
|
-
Validacity.
|
7
|
+
Validacity.validations[validation_name(klass)] = klass
|
8
8
|
end
|
9
9
|
|
10
10
|
private
|
11
11
|
|
12
|
-
def
|
13
|
-
klass.name.gsub(/
|
12
|
+
def validation_name(klass)
|
13
|
+
klass.name.gsub(/Validation$/, "").underscore.to_sym
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -4,44 +4,44 @@ module Validacity
|
|
4
4
|
|
5
5
|
class_methods do
|
6
6
|
def validations(*names)
|
7
|
-
names.each { |n|
|
7
|
+
names.each { |n| validacity_validations << n }
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
@
|
10
|
+
def validacity_validations
|
11
|
+
@_validacity_validations ||= Set.new
|
12
12
|
end
|
13
13
|
|
14
14
|
def new(resource)
|
15
15
|
instance = super
|
16
|
-
instance.
|
16
|
+
instance.validacity_validations.merge(validacity_validations.dup)
|
17
17
|
instance
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
@
|
21
|
+
def validacity_validations
|
22
|
+
@_validacity_validations ||= Set.new
|
23
23
|
end
|
24
24
|
|
25
25
|
def valid?(context = nil)
|
26
26
|
super
|
27
|
-
|
27
|
+
validacity_validations_run(context)
|
28
28
|
errors.empty?
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def
|
34
|
-
|
33
|
+
def validacity_validations_run(_context = nil)
|
34
|
+
each_validation_instance(&:validate)
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
38
|
-
@
|
39
|
-
|
40
|
-
unless (
|
41
|
-
|
42
|
-
@
|
37
|
+
def each_validation_instance
|
38
|
+
@_validacity_validations_instances ||= {}
|
39
|
+
validacity_validations.each do |name|
|
40
|
+
unless (validation = @_validacity_validations_instances[name])
|
41
|
+
validation = Validacity.find_validation(name).new(self)
|
42
|
+
@_validacity_validations_instances[name] = validation
|
43
43
|
end
|
44
|
-
yield(
|
44
|
+
yield(validation)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
data/lib/validacity/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validacity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Zinovyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -122,11 +122,11 @@ files:
|
|
122
122
|
- README.md
|
123
123
|
- Rakefile
|
124
124
|
- lib/generators/validacity/install_generator.rb
|
125
|
-
- lib/generators/validacity/templates/
|
126
|
-
- lib/generators/validacity/templates/
|
127
|
-
- lib/generators/validacity/
|
125
|
+
- lib/generators/validacity/templates/application_validation.rb
|
126
|
+
- lib/generators/validacity/templates/validation.rb
|
127
|
+
- lib/generators/validacity/validation_generator.rb
|
128
128
|
- lib/validacity.rb
|
129
|
-
- lib/validacity/
|
129
|
+
- lib/validacity/base_validation.rb
|
130
130
|
- lib/validacity/configuration.rb
|
131
131
|
- lib/validacity/railtie.rb
|
132
132
|
- lib/validacity/validatable.rb
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module Validacity
|
2
|
-
module Generators
|
3
|
-
class ValidatorGenerator < ::Rails::Generators::NamedBase
|
4
|
-
source_root File.expand_path("templates", __dir__)
|
5
|
-
|
6
|
-
def create_validator
|
7
|
-
validator_file = File.join("app/validators", class_path,
|
8
|
-
"#{file_name}_validator.rb")
|
9
|
-
template "validator.rb", validator_file
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|