rails-schema-validations 0.8 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README +13 -2
- data/lib/rails-schema-validations.rb +7 -3
- data/test/validations_from_schema_test.rb +6 -1
- metadata +4 -4
data/README
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
Summary
|
2
|
+
=======
|
1
3
|
Add validations to your models automatically based on the schema constraints.
|
2
4
|
Place this one line in your model:
|
3
5
|
|
4
6
|
validations_from_schema
|
5
7
|
|
6
8
|
|
9
|
+
Generated Validations
|
10
|
+
=====================
|
11
|
+
|
7
12
|
| schema type | rails validation | extra |
|
8
13
|
|-------------|---------------------------|---------------------------|
|
9
14
|
| non-null | validates_presence_of | |
|
@@ -11,7 +16,13 @@ validations_from_schema
|
|
11
16
|
| integer | validates_numericality_of | :only_integer, :less_than |
|
12
17
|
| float | validates_numericality_of | |
|
13
18
|
|
19
|
+
for boolean non-null it uses validates_inclusion_of :in => [true,false]
|
20
|
+
|
21
|
+
Options
|
22
|
+
=======
|
23
|
+
To exclude a column use the `:except => ['excluded']` option.
|
14
24
|
|
25
|
+
Warning
|
26
|
+
=======
|
15
27
|
Tested on MySQL.
|
16
|
-
|
17
|
-
This gem may have problems on other databases, particularly Sqlite.
|
28
|
+
Assume it doesn't work on your database unless you modify the test suite to run against your db and all tests pass
|
@@ -1,8 +1,11 @@
|
|
1
1
|
# this will put it in the scope of a model
|
2
2
|
module ActiveRecord::Validations::ClassMethods
|
3
|
-
def validations_from_schema
|
3
|
+
def validations_from_schema options = {}
|
4
|
+
except = [*options.delete(:except)].map {|c| c.to_s}
|
5
|
+
fail "unexpected arguments" if options.size != 0
|
6
|
+
|
4
7
|
self.columns.map do |col|
|
5
|
-
next if col.primary
|
8
|
+
next if col.primary or except.include? col.name
|
6
9
|
|
7
10
|
case col.type
|
8
11
|
when :integer
|
@@ -21,7 +24,8 @@ module ActiveRecord::Validations::ClassMethods
|
|
21
24
|
validates_inclusion_of col.name, :in => [true, false], :allow_nil => col.null
|
22
25
|
end
|
23
26
|
end.compact + content_columns.map do |col|
|
24
|
-
|
27
|
+
next if col.null or col.type == :boolean or except.include? col.name
|
28
|
+
validates_presence_of col.name
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
@@ -12,6 +12,7 @@ def load_schema
|
|
12
12
|
%w(string integer float datetime date boolean).each do |kind|
|
13
13
|
t.send(kind, kind)
|
14
14
|
t.send(kind, "#{kind}_notnull", :null => false)
|
15
|
+
t.send(kind, "#{kind}_excepted", :null => false)
|
15
16
|
end
|
16
17
|
%w(string integer).each do |kind|
|
17
18
|
t.send(kind, "#{kind}_limit", :limit => 4)
|
@@ -30,7 +31,8 @@ end
|
|
30
31
|
load_schema
|
31
32
|
|
32
33
|
class Model < ActiveRecord::Base
|
33
|
-
validations_from_schema
|
34
|
+
validations_from_schema :except => (
|
35
|
+
%w(string integer float date datetime boolean). map {|s| "#{s}_excepted"})
|
34
36
|
end
|
35
37
|
|
36
38
|
context Model do
|
@@ -56,6 +58,8 @@ context Model do
|
|
56
58
|
%w(string integer float date datetime boolean).each do |name|
|
57
59
|
asserts_no_error_on name, nil
|
58
60
|
asserts_error_on "#{name}_notnull", nil
|
61
|
+
# ignore
|
62
|
+
asserts_no_error_on "#{name}_excepted", nil
|
59
63
|
end
|
60
64
|
%w(string integer).each do |name|
|
61
65
|
asserts_no_error_on "#{name}_limit", nil
|
@@ -80,5 +84,6 @@ context Model do
|
|
80
84
|
asserts_no_error_on :boolean_notnull, false
|
81
85
|
asserts_error_on :boolean_notnull, nil
|
82
86
|
|
87
|
+
# ignore
|
83
88
|
asserts_no_error_on :id, nil
|
84
89
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-schema-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 9
|
9
|
+
version: "0.9"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Greg Weber
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-25 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|