schema_validations 2.0.0 → 2.0.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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06905897e2968a424dd329db0dd07bfdc32625aa
|
4
|
+
data.tar.gz: 351f0900d4f03138c76a3f19a4e83c4a02cf522d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84c4bae6e9d3931f74a817c05c272a82be472c41a147a579c3ea5bd80b53b76f7287afe2f8a7eff909d1d59c667c3fd38fb147c3031ec8c74c1ab9f460dcfdd5
|
7
|
+
data.tar.gz: 3d39c05c232dd621b8ea04c80c018c108844cd5817725088b9ee300682863444ed7eb8b1c1fb7231116c62b6bbcaf32c487a537e14fec877c8bacec5b21ae0ab
|
data/README.md
CHANGED
@@ -17,24 +17,30 @@ columns, keeping your model class definitions simple and DRY. That's great
|
|
17
17
|
for simple data columns, but where it falls down is when your table contains
|
18
18
|
constraints.
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
```ruby
|
21
|
+
create_table :users do |t|
|
22
|
+
t.string :email, null: false, limit: 30
|
23
|
+
t.boolean :confirmed, null: false
|
24
|
+
end
|
25
|
+
```
|
24
26
|
|
25
|
-
In that case the constraints
|
27
|
+
In that case the constraints `null: false`, `limit: 30` and `:boolean` must be validated on the model level, to avoid ugly database exceptions:
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
```ruby
|
30
|
+
class User < ActiveRecord::Base
|
31
|
+
validates :email, presence: true, length: { maximum: 30 }
|
32
|
+
validates :confirmed, presence: true, inclusion: { in: [true, false] }
|
33
|
+
end
|
34
|
+
```
|
31
35
|
|
32
36
|
...which isn't the most DRY approach.
|
33
37
|
|
34
38
|
SchemaValidations aims to DRY up your models, doing that boring work for you. It inspects the database and automatically creates validations based on the schema. After installing it your model is as simple as it can be.
|
35
39
|
|
36
|
-
|
37
|
-
|
40
|
+
```ruby
|
41
|
+
class User < ActiveRecord::Base
|
42
|
+
end
|
43
|
+
```
|
38
44
|
|
39
45
|
Validations are there but they are created by schema_validations under the
|
40
46
|
hood.
|
@@ -43,26 +49,28 @@ hood.
|
|
43
49
|
|
44
50
|
Simply add schema_validations to your Gemfile.
|
45
51
|
|
46
|
-
|
52
|
+
```ruby
|
53
|
+
gem "schema_validations"
|
54
|
+
```
|
47
55
|
|
48
56
|
## Which validations are covered?
|
49
57
|
|
50
58
|
Constraints:
|
51
59
|
|
52
|
-
| Constraint | Validation
|
53
|
-
|
54
|
-
|
|
55
|
-
| :
|
56
|
-
| :
|
57
|
-
| :
|
60
|
+
| Constraint | Validation |
|
61
|
+
|---------------------|---------------------------------------------------|
|
62
|
+
| `null: false` | `validates ... presence: true` |
|
63
|
+
| `limit: 100` | `validates ... length: { maximum: 100 }` |
|
64
|
+
| `unique: true` | `validates ... uniqueness: true` |
|
65
|
+
| `unique: true, case_sensitive: false` <br>(If [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) is also in use) | `validates ... uniqueness: { case_sensitive: false }` |
|
58
66
|
|
59
67
|
Data types:
|
60
68
|
|
61
|
-
| Type | Validation
|
62
|
-
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
| Type | Validation |
|
70
|
+
|--------------------|------------------------------------------------------------------------------------------------------|
|
71
|
+
| `:boolean` | `:validates ... inclusion: { in: [true, false] }` |
|
72
|
+
| `:float` | `:validates ... numericality: true` |
|
73
|
+
| `:integer` | `:validates ... numericality: { only_integer: true, greater_than_or_equal_to: ..., less_than: ... }` |
|
66
74
|
|
67
75
|
|
68
76
|
## What if I want something special?
|
@@ -117,25 +125,25 @@ You can override the global configuration per-model, using the `schema_validatio
|
|
117
125
|
|
118
126
|
##### Disable per model:
|
119
127
|
```ruby
|
120
|
-
|
121
|
-
|
122
|
-
|
128
|
+
class User < ActiveRecord::Base
|
129
|
+
schema_validations auto_create: false
|
130
|
+
end
|
123
131
|
```
|
124
132
|
|
125
133
|
##### Use a custom validation rather than schema_validations automatic default:
|
126
134
|
```ruby
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
135
|
+
class User < ActiveRecord::Base
|
136
|
+
schema_validations except: :email # don't create default validation for email
|
137
|
+
validates :email, presence: true, length: { in: 5..30 }
|
138
|
+
end
|
131
139
|
```
|
132
140
|
|
133
141
|
##### Include validations every field, without a whitelist:
|
134
142
|
|
135
143
|
```ruby
|
136
|
-
|
137
|
-
|
138
|
-
|
144
|
+
class User < ActiveRecord::Base
|
145
|
+
schema_validations whitelist: nil
|
146
|
+
end
|
139
147
|
```
|
140
148
|
|
141
149
|
|
@@ -145,7 +153,9 @@ If you're curious (or dubious) about what validations SchemaValidations
|
|
145
153
|
defines, you can check the log file. For every assocation that
|
146
154
|
SchemaValidations defines, it generates a debug entry in the log such as
|
147
155
|
|
148
|
-
|
156
|
+
```
|
157
|
+
[schema_validations] Article.validates_length_of :title, :allow_nil=>true, :maximum=>50
|
158
|
+
```
|
149
159
|
|
150
160
|
which shows the exact validation definition call.
|
151
161
|
|
@@ -175,6 +185,10 @@ Earlier versions of SchemaValidations supported:
|
|
175
185
|
|
176
186
|
## Release Notes
|
177
187
|
|
188
|
+
### 2.0.1
|
189
|
+
|
190
|
+
* Bug fix: Don't crash when optimistic locking is in use (#8)
|
191
|
+
|
178
192
|
### 2.0.0
|
179
193
|
|
180
194
|
This major version is backwards compatible for most uses. Only those who specified a per-model `:except` clause would be affected.
|
data/lib/schema_validations.rb
CHANGED
@@ -3,6 +3,7 @@ require 'valuable'
|
|
3
3
|
require 'schema_plus_columns'
|
4
4
|
require 'schema_validations/version'
|
5
5
|
require 'schema_validations/active_record/validations'
|
6
|
+
require 'schema_validations/active_record/type'
|
6
7
|
require 'schema_validations/railtie' if defined?(Rails::Railtie)
|
7
8
|
|
8
9
|
module SchemaValidations
|
@@ -112,6 +113,7 @@ module SchemaValidations
|
|
112
113
|
return if @inserted
|
113
114
|
@inserted = true
|
114
115
|
::ActiveRecord::Base.extend SchemaValidations::ActiveRecord::Validations
|
116
|
+
::ActiveRecord::Type::Integer.prepend SchemaValidations::ActiveRecord::Type::Integer
|
115
117
|
end
|
116
118
|
|
117
119
|
end
|
@@ -130,12 +130,13 @@ module SchemaValidations
|
|
130
130
|
def load_integer_column_validations(name, column) # :nodoc:
|
131
131
|
options = { :allow_nil => true, :only_integer => true }
|
132
132
|
|
133
|
-
range = column.cast_type.
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
133
|
+
if range = column.cast_type.try(:range)
|
134
|
+
options[:greater_than_or_equal_to] = range.begin
|
135
|
+
if range.exclude_end?
|
136
|
+
options[:less_than] = range.end
|
137
|
+
else
|
138
|
+
options[:less_than_or_equal_to] = range.end
|
139
|
+
end
|
139
140
|
end
|
140
141
|
|
141
142
|
validate_logged :validates_numericality_of, name, options
|
data/spec/validations_spec.rb
CHANGED
@@ -391,6 +391,22 @@ describe "Validations" do
|
|
391
391
|
end
|
392
392
|
end
|
393
393
|
|
394
|
+
context 'with optimistic locking' do
|
395
|
+
before do
|
396
|
+
ActiveRecord::Schema.define do
|
397
|
+
create_table :optimistics, :force => true do |t|
|
398
|
+
t.integer :lock_version
|
399
|
+
end
|
400
|
+
end
|
401
|
+
with_auto_validations do
|
402
|
+
class Optimistic < ActiveRecord::Base; end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
it 'should not crash' do
|
406
|
+
expect(Optimistic.new).to be_valid
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
394
410
|
protected
|
395
411
|
def with_auto_validations(value = true)
|
396
412
|
old_value = SchemaValidations.config.auto_create
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronen Barzel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-01-
|
12
|
+
date: 2016-01-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: schema_plus_columns
|
@@ -181,6 +181,7 @@ files:
|
|
181
181
|
- gemfiles/activerecord-4.2/Gemfile.sqlite3
|
182
182
|
- init.rb
|
183
183
|
- lib/schema_validations.rb
|
184
|
+
- lib/schema_validations/active_record/type.rb
|
184
185
|
- lib/schema_validations/active_record/validations.rb
|
185
186
|
- lib/schema_validations/railtie.rb
|
186
187
|
- lib/schema_validations/version.rb
|