validates_by_schema 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +2 -2
- data/lib/validates_by_schema.rb +25 -12
- data/lib/validates_by_schema/validation_option.rb +4 -1
- data/lib/validates_by_schema/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 95a3e699ef49bdc2b00988cce281ee448bfc05020b5ce9388889a928ae74fe57
|
4
|
+
data.tar.gz: 544048310b55c334b2e287feb99e48f17258d3e9082137dfa9d5b633c8b4afa6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 024cbf1790625db028dc7d351ff8d20797e762f31c2d91651da61ee6b533e73999d67aa871a3649db94140dc9ae86ceee3d09b311cf9a324c5cd075ce19a38ac
|
7
|
+
data.tar.gz: 8f4c69c1a0b59d300c7553f92f6056db6d6ffa50fd1b46f6bd65b0a7cd8e0b8baaaac0bc58fcdb0f3498613b87c0544397963f6107310e2aa5665af2505fd77a
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Validates By Schema (validates_by_schema)
|
2
2
|
[](https://rubygems.org/gems/validates_by_schema)
|
3
3
|
[](https://travis-ci.org/joshwlewis/validates_by_schema)
|
4
|
-
[](https://gemnasium.com/joshwlewis/validates_by_schema)
|
5
4
|
[](https://coveralls.io/r/joshwlewis/validates_by_schema)
|
6
|
-
[](https://codeclimate.com/github/joshwlewis/validates_by_schema)
|
6
|
+
|
7
7
|
|
8
8
|
Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
|
9
9
|
|
data/lib/validates_by_schema.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/lazy_load_hooks'
|
3
|
+
|
1
4
|
module ValidatesBySchema
|
2
5
|
autoload :ValidationOption, 'validates_by_schema/validation_option'
|
3
6
|
|
@@ -6,32 +9,42 @@ module ValidatesBySchema
|
|
6
9
|
module ClassMethods
|
7
10
|
|
8
11
|
def validates_by_schema(options = {})
|
9
|
-
|
12
|
+
@validates_by_schema_options = options
|
13
|
+
define_schema_validations if schema_loaded?
|
14
|
+
end
|
10
15
|
|
11
|
-
|
16
|
+
def load_schema!
|
17
|
+
super
|
18
|
+
# define schema validations lazy to avoid accessing the database
|
19
|
+
# at class load time.
|
20
|
+
define_schema_validations
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def define_schema_validations
|
26
|
+
return unless @validates_by_schema_options
|
27
|
+
|
28
|
+
customized_schema_validatable_columns.each do |c|
|
12
29
|
ValidationOption.new(self, c).define!
|
13
30
|
end
|
14
31
|
|
15
|
-
|
16
|
-
# Since `db:create` and other tasks from Rails 5.2.0 might load models,
|
17
|
-
# we need to swallow this error to execute `db:create` properly.
|
32
|
+
@validates_by_schema_options = nil
|
18
33
|
end
|
19
34
|
|
20
|
-
|
21
|
-
|
22
|
-
def customized_columns(options)
|
35
|
+
def customized_schema_validatable_columns
|
23
36
|
# Allow user to specify :only or :except options
|
24
|
-
|
37
|
+
schema_validatable_columns.tap do |columns|
|
25
38
|
{ only: :select!, except: :reject! }.each do |k, v|
|
26
|
-
if
|
27
|
-
attrs = Array(
|
39
|
+
if @validates_by_schema_options[k]
|
40
|
+
attrs = Array(@validates_by_schema_options[k]).collect(&:to_s)
|
28
41
|
columns.send(v) { |c| attrs.include?(c.name) }
|
29
42
|
end
|
30
43
|
end
|
31
44
|
end
|
32
45
|
end
|
33
46
|
|
34
|
-
def
|
47
|
+
def schema_validatable_columns
|
35
48
|
columns.reject do |c|
|
36
49
|
ignored_columns_for_validates_by_schema.include?(c.name)
|
37
50
|
end
|
@@ -10,7 +10,9 @@ class ValidatesBySchema::ValidationOption
|
|
10
10
|
|
11
11
|
def define!
|
12
12
|
if association
|
13
|
-
|
13
|
+
if !ActiveRecord::Base.belongs_to_required_by_default && !column.null
|
14
|
+
klass.validates association.name, presence: true
|
15
|
+
end
|
14
16
|
else
|
15
17
|
options = to_hash
|
16
18
|
klass.validates column.name, options if options.present?
|
@@ -94,4 +96,5 @@ class ValidatesBySchema::ValidationOption
|
|
94
96
|
def to_s
|
95
97
|
to_hash.inspect
|
96
98
|
end
|
99
|
+
|
97
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_by_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-10-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 5.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 5.0.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
requirements: []
|
146
146
|
rubyforge_project:
|
147
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.7.8
|
148
148
|
signing_key:
|
149
149
|
specification_version: 4
|
150
150
|
summary: Automatic validation based on your database schema column types and limits.
|