generated_schema_validations 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +20 -2
- data/generated_schema_validations.gemspec +1 -1
- data/lib/generated_schema_validations/dumper.rb +1 -1
- data/lib/generated_schema_validations/table.rb +6 -0
- data/lib/generated_schema_validations/template.rb +21 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70a54053cb481280797981eb0f5cb1746dc446611de1ef7ed3ee112db984b08b
|
4
|
+
data.tar.gz: 328ee9c6a2638a317018c69689ca2775f247e8d82fc0038669d5f1d4caa91c57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c60e7b895927d42d5534cad96bc2918e633f4bd59b77614cbe8712c46539976a4db2d842004027e757f372ddf77f40f7f1e493f395988788aeee82df178a3a85
|
7
|
+
data.tar.gz: ab1bd36fc0440980e39aa6b2ce18e7311ab38cc2e0a8192aa72818db91be65a5b6bfa47eb0282749a7362f987b63bbe23a13859c5a2c88e0f89d4af85e2e5505
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,7 @@ To generate `app/models/concerns/schema_validations.rb`:
|
|
27
27
|
|
28
28
|
### Use validations
|
29
29
|
|
30
|
-
Add to `app/
|
30
|
+
Add to `app/models/application_record.rb`:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
class ApplicationRecord < ActiveRecord::Base
|
@@ -51,7 +51,7 @@ class User < ApplicationRecord
|
|
51
51
|
end
|
52
52
|
```
|
53
53
|
|
54
|
-
### Exclude from
|
54
|
+
### Exclude from rubocop and simplecov
|
55
55
|
|
56
56
|
Add to simplecov config file (e.g. `.simplecov`):
|
57
57
|
|
@@ -93,6 +93,24 @@ validate :stuff, numericality: true
|
|
93
93
|
|
94
94
|
You can watch changes on `schema_validations.rb` to understand the generated validations.
|
95
95
|
|
96
|
+
## Changelog
|
97
|
+
|
98
|
+
### 0.2.1
|
99
|
+
|
100
|
+
* Close tempfile before reading
|
101
|
+
|
102
|
+
### 0.2.0
|
103
|
+
|
104
|
+
* Add validations of date and datetime columns to be in database range
|
105
|
+
|
106
|
+
### 0.1.2
|
107
|
+
|
108
|
+
* Exclude columns in unique validations
|
109
|
+
|
110
|
+
### 0.1.1
|
111
|
+
|
112
|
+
* Add column type `binary`
|
113
|
+
|
96
114
|
## Contributing
|
97
115
|
|
98
116
|
Bug reports and pull requests are welcome on GitHub at https://github.com/Lichtbit/generated_schema_validations.
|
@@ -76,16 +76,22 @@ class GeneratedSchemaValidations::Table
|
|
76
76
|
|
77
77
|
def datetime(name, column_options = {})
|
78
78
|
null_validation(:datetime, name, column_options)
|
79
|
+
validates name, :date_time_in_db_range
|
79
80
|
end
|
80
81
|
|
81
82
|
def date(name, column_options = {})
|
82
83
|
null_validation(:date, name, column_options)
|
84
|
+
validates name, :date_in_db_range
|
83
85
|
end
|
84
86
|
|
85
87
|
def boolean(name, column_options = {})
|
86
88
|
null_validation(:boolean, name, column_options)
|
87
89
|
end
|
88
90
|
|
91
|
+
def binary(name, column_options = {})
|
92
|
+
null_validation(:binary, name, column_options)
|
93
|
+
end
|
94
|
+
|
89
95
|
def string(name, column_options = {})
|
90
96
|
text(name, column_options)
|
91
97
|
end
|
@@ -54,6 +54,7 @@ module SchemaValidations
|
|
54
54
|
def dbv_uniqueness_validations_for(unique_indexes, foreign_key:, column:)
|
55
55
|
unique_indexes.each do |names|
|
56
56
|
next unless foreign_key.in?(names)
|
57
|
+
next if column.to_sym.in?(schema_validations_excluded_columns)
|
57
58
|
|
58
59
|
scope = (names - [foreign_key]).map(&:to_sym)
|
59
60
|
options = { allow_nil: true }
|
@@ -70,4 +71,24 @@ module SchemaValidations
|
|
70
71
|
end
|
71
72
|
end
|
72
73
|
end
|
74
|
+
|
75
|
+
class DateTimeInDbRangeValidator < ActiveModel::EachValidator
|
76
|
+
def validate_each(record, attr_name, value)
|
77
|
+
return if value.nil?
|
78
|
+
return unless value.is_a?(DateTime) || value.is_a?(Time)
|
79
|
+
return if value.year.between?(-4711, 294275) # see https://www.postgresql.org/docs/9.3/datatype-datetime.html
|
80
|
+
|
81
|
+
record.errors.add(attr_name, :invalid, options)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class DateInDbRangeValidator < ActiveModel::EachValidator
|
86
|
+
def validate_each(record, attr_name, value)
|
87
|
+
return if value.nil?
|
88
|
+
return unless value.is_a?(Date)
|
89
|
+
return if value.year.between?(-4711, 5874896) # see https://www.postgresql.org/docs/9.3/datatype-datetime.html
|
90
|
+
|
91
|
+
record.errors.add(attr_name, :invalid, options)
|
92
|
+
end
|
93
|
+
end
|
73
94
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: generated_schema_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Limbach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: After each migration it generates a file with some validations. Each
|
14
14
|
active record should include this file and can uns generated validations.
|
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
56
|
+
rubygems_version: 3.0.8
|
57
57
|
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Generate rails validations from schema.rb file
|