validates_schema 1.0.0 → 1.1.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.
- data/lib/validates_schema/validates_schema.rb +50 -5
- metadata +3 -3
@@ -4,7 +4,46 @@ class ActiveRecord::Base
|
|
4
4
|
|
5
5
|
def inherited(klass)
|
6
6
|
super(klass)
|
7
|
+
begin
|
8
|
+
add_schema_based_validations(klass)
|
9
|
+
return # it worked, so just return
|
10
|
+
rescue ActiveRecord::StatementInvalid => e
|
11
|
+
# puts "e.message: #{e.message}"
|
12
|
+
|
13
|
+
begin
|
14
|
+
# The ActiveRecord::StatementInvalid could be a result
|
15
|
+
# of an abstract superclass, so let's reset the table name
|
16
|
+
# and try again:
|
17
|
+
klass.reset_table_name
|
18
|
+
add_schema_based_validations(klass)
|
19
|
+
return # it worked, so just return
|
20
|
+
rescue ActiveRecord::StatementInvalid => e
|
21
|
+
# puts "e.message: #{e.message}"
|
22
|
+
end
|
23
|
+
end # rescue ActiveRecord::StatementInvalid
|
24
|
+
|
25
|
+
# Ok, so it failed, let's add a hook around setting
|
26
|
+
# the table name and try that. Hopefully all should be well then.
|
27
|
+
unless klass.respond_to?(:set_table_name_with_schema_validations)
|
28
|
+
klass.class_eval do
|
29
|
+
class << self
|
30
|
+
def set_table_name_with_schema_validations(*args)
|
31
|
+
set_table_name_without_schema_validations(*args)
|
32
|
+
return if abstract_class
|
33
|
+
add_schema_based_validations(self)
|
34
|
+
end
|
35
|
+
alias_method_chain :set_table_name, :schema_validations
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end # inherited
|
41
|
+
|
42
|
+
def add_schema_based_validations(klass = self)
|
43
|
+
klass.reset_column_information
|
7
44
|
# puts klass
|
45
|
+
# puts "klass.arel_table: #{klass.arel_table.inspect}"
|
46
|
+
validations = []
|
8
47
|
klass.arel_table.columns.each do |c|
|
9
48
|
detail = c.column
|
10
49
|
next if detail.name == 'id'
|
@@ -19,15 +58,21 @@ class ActiveRecord::Base
|
|
19
58
|
options[:presence] = true unless detail.null
|
20
59
|
|
21
60
|
unless options.empty?
|
22
|
-
|
61
|
+
validations << "validates :#{detail.name.to_sym}, #{options.inspect}"
|
23
62
|
klass.class_eval do
|
24
63
|
validates detail.name.to_sym, options || {}
|
25
64
|
end
|
26
65
|
end
|
27
|
-
|
28
66
|
end
|
29
|
-
|
67
|
+
# uncomment to see a list of generated validations:
|
68
|
+
# unless validations.empty?
|
69
|
+
# puts "#{klass}:"
|
70
|
+
# validations.each do |line|
|
71
|
+
# puts " #{line}"
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
end # add_schema_based_validations
|
30
75
|
|
31
|
-
end
|
76
|
+
end # class << self
|
32
77
|
|
33
|
-
end
|
78
|
+
end # ActiveRecord::Base
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 1.0.0
|
9
|
+
version: 1.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- markbates
|
@@ -60,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
60
|
requirements:
|
61
61
|
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
hash:
|
63
|
+
hash: 372854992706096450
|
64
64
|
segments:
|
65
65
|
- 0
|
66
66
|
version: "0"
|