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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 977b8c39503dc3cabfad86bc011240a877dfb84c49d12d4f9fe7f643426bbbc0
4
- data.tar.gz: 5a6ab82e347f48f6bb7ff5c0308bbfeec6b9f20414967036a0149d2e56592224
3
+ metadata.gz: 70a54053cb481280797981eb0f5cb1746dc446611de1ef7ed3ee112db984b08b
4
+ data.tar.gz: 328ee9c6a2638a317018c69689ca2775f247e8d82fc0038669d5f1d4caa91c57
5
5
  SHA512:
6
- metadata.gz: 06345f91aaa7aa5a8d3b797a80bb70c55e58450aad7e1abf38c136373b3c7db3ed184bcc91ca7ba43f88611628e468700d6c909f30f2b9b06a5aa99749d84c85
7
- data.tar.gz: e2ff2c3ed55bb9aa6af514f1ccdb19dc80eb0b098de3c2a141d00cd85cdb3ff01717f6986038619d0048c31b9519a348aff090f3785e725dcc61dc4f7350bd1e
6
+ metadata.gz: c60e7b895927d42d5534cad96bc2918e633f4bd59b77614cbe8712c46539976a4db2d842004027e757f372ddf77f40f7f1e493f395988788aeee82df178a3a85
7
+ data.tar.gz: ab1bd36fc0440980e39aa6b2ce18e7311ab38cc2e0a8192aa72818db91be65a5b6bfa47eb0282749a7362f987b63bbe23a13859c5a2c88e0f89d4af85e2e5505
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ generated_schema_validations-*.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- generated_schema_validations (0.1.0)
4
+ generated_schema_validations (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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/mode/application_record.rb`:
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 rubocp and simplecov
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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'generated_schema_validations'
5
- spec.version = '0.1.0'
5
+ spec.version = '0.2.1'
6
6
  spec.authors = ['Georg Limbach']
7
7
  spec.email = ['georg.limbach@lichtbit.com']
8
8
 
@@ -11,10 +11,10 @@ class GeneratedSchemaValidations::Dumper
11
11
  raise 'The scheme is not well-formed.' if schema_content.include?('ActiveRecord')
12
12
 
13
13
  file.write(schema_content)
14
+ file.close
14
15
 
15
16
  load file.path
16
17
  ensure
17
- file.close
18
18
  file.unlink
19
19
  end
20
20
  end
@@ -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.0
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-01-07 00:00:00.000000000 Z
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.1.4
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