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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 69129acef7a43c707e98663657e97dc1b7222ce1
4
- data.tar.gz: 4368f63d8423b026079cea4fd215baf7c27791a7
2
+ SHA256:
3
+ metadata.gz: 95a3e699ef49bdc2b00988cce281ee448bfc05020b5ce9388889a928ae74fe57
4
+ data.tar.gz: 544048310b55c334b2e287feb99e48f17258d3e9082137dfa9d5b633c8b4afa6
5
5
  SHA512:
6
- metadata.gz: e1df6f34e85a82ca55287847596878ce45a3d3d8e076cfe7b150919769090f26ad94d68e92d9aaf0dfd9eced591d0bda74f602a5706b05041dc24a907be4e4a2
7
- data.tar.gz: a391c996e1f204c10d3d622f8c7f9d15a8ecf1640e44078a09a8f07d8c351338dcb80515bdc8a6417f1baa069703d5ad04f42fc93eb800fd64d81dcbd1119d27
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
  [![Gem Version](http://img.shields.io/gem/v/validates_by_schema.svg?style=flat)](https://rubygems.org/gems/validates_by_schema)
3
3
  [![Build Status](http://img.shields.io/travis/joshwlewis/validates_by_schema.svg?style=flat)](https://travis-ci.org/joshwlewis/validates_by_schema)
4
- [![Dependency Status](http://img.shields.io/gemnasium/joshwlewis/validates_by_schema.svg?style=flat)](https://gemnasium.com/joshwlewis/validates_by_schema)
5
4
  [![Coverage Status](http://img.shields.io/coveralls/joshwlewis/validates_by_schema.svg?style=flat)](https://coveralls.io/r/joshwlewis/validates_by_schema)
6
- [![Code Climate](http://img.shields.io/codeclimate/github/joshwlewis/validates_by_schema.svg?style=flat)](https://codeclimate.com/github/joshwlewis/validates_by_schema)
5
+ [![Code Climate](http://img.shields.io/codeclimate/maintainability/joshwlewis/validates_by_schema.svg?style=flat)](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
 
@@ -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
- return unless table_exists?
12
+ @validates_by_schema_options = options
13
+ define_schema_validations if schema_loaded?
14
+ end
10
15
 
11
- customized_columns(options).each do |c|
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
- rescue ::ActiveRecord::NoDatabaseError
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
- private
21
-
22
- def customized_columns(options)
35
+ def customized_schema_validatable_columns
23
36
  # Allow user to specify :only or :except options
24
- schema_validateable_columns.tap do |columns|
37
+ schema_validatable_columns.tap do |columns|
25
38
  { only: :select!, except: :reject! }.each do |k, v|
26
- if options[k]
27
- attrs = Array(options[k]).collect(&:to_s)
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 schema_validateable_columns
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
- klass.validates association.name, presence: true unless column.null
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
@@ -1,3 +1,3 @@
1
1
  module ValidatesBySchema
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  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.3.1
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: 2018-05-16 00:00:00.000000000 Z
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: 3.1.0
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: 3.1.0
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.4.8
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.