legacy_data 0.1.2 → 0.1.4
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/VERSION +1 -1
- data/generators/models_from_tables/templates/model.rb +6 -2
- data/legacy_data.gemspec +1 -1
- data/lib/legacy_data/schema.rb +10 -3
- data/spec/expected/post.rb +0 -2
- data/spec/functional/expected/comment.rb +2 -2
- data/spec/functional/expected/post.rb +0 -1
- data/spec/legacy_data/schema_spec.rb +5 -5
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
@@ -24,13 +24,17 @@ class <%= class_name -%> < ActiveRecord::Base
|
|
24
24
|
<%- constraints[:boolean_presence].each do |col|
|
25
25
|
-%> validates_inclusion_of <%= col %>, :in => %w(true false)
|
26
26
|
<%- end -%>
|
27
|
-
|
27
|
+
<%- [:allow_nil, :do_not_allow_nil].each do |nullable|
|
28
|
+
unless constraints[:numericality_of][nullable].blank?
|
29
|
+
-%> <%= "validates_numericality_of #{constraints[:numericality_of][nullable].map {|cols| cols.downcase.to_sym.inspect}.join(', ')}" %><%= ", {:allow_nil=>true}" if nullable == :allow_nil %>
|
30
|
+
<%- end
|
31
|
+
end unless constraints[:numericality_of].blank? -%>
|
28
32
|
<%- constraints[:custom].each do |name, sql_rule|
|
29
33
|
-%> validate <%= "validate_#{name}".to_sym.inspect %>
|
30
34
|
def <%= "validate_#{name}" %>
|
31
35
|
# TODO: validate this SQL constraint
|
32
36
|
"<%= sql_rule %>"
|
33
37
|
end
|
34
|
-
<%- end
|
38
|
+
<%- end -%>
|
35
39
|
end
|
36
40
|
|
data/legacy_data.gemspec
CHANGED
data/lib/legacy_data/schema.rb
CHANGED
@@ -122,7 +122,7 @@ module LegacyData
|
|
122
122
|
|
123
123
|
@constraints[:unique], @constraints[:multi_column_unique] = uniqueness_constraints
|
124
124
|
@constraints[:boolean_presence], @constraints[:presence_of] = presence_constraints
|
125
|
-
@constraints[:numericality_of] =
|
125
|
+
@constraints[:numericality_of] = numericality_constraints
|
126
126
|
@constraints[:custom] = custom_constraints
|
127
127
|
end
|
128
128
|
@constraints
|
@@ -134,6 +134,13 @@ module LegacyData
|
|
134
134
|
#
|
135
135
|
end
|
136
136
|
|
137
|
+
def numericality_constraints
|
138
|
+
allow_nil, do_not_allow_nil = integer_columns.partition do |column|
|
139
|
+
column.null
|
140
|
+
end
|
141
|
+
{:allow_nil=>allow_nil.map(&:name), :do_not_allow_nil=>do_not_allow_nil.map(&:name)}
|
142
|
+
end
|
143
|
+
|
137
144
|
def uniqueness_constraints
|
138
145
|
unique, multi_column_unique = unique_constraints.partition do |columns|
|
139
146
|
columns.size == 1
|
@@ -151,8 +158,8 @@ module LegacyData
|
|
151
158
|
columns.detect {|column| column.name == name }
|
152
159
|
end
|
153
160
|
|
154
|
-
def
|
155
|
-
columns.select {|column| column.type == :integer }
|
161
|
+
def integer_columns
|
162
|
+
columns.select {|column| column.type == :integer }
|
156
163
|
end
|
157
164
|
|
158
165
|
def columns
|
data/spec/expected/post.rb
CHANGED
@@ -112,16 +112,16 @@ describe LegacyData::Schema do
|
|
112
112
|
|
113
113
|
describe 'constraints' do
|
114
114
|
it 'should have the different types of constraints' do
|
115
|
-
@schema.stub!(:uniqueness_constraints).and_return([unique =[mock], multi_column_unique=[mock]])
|
116
|
-
@schema.stub!(:presence_constraints
|
117
|
-
@schema.stub!(:
|
118
|
-
@schema.stub!(:custom_constraints
|
115
|
+
@schema.stub!(:uniqueness_constraints ).and_return([unique =[mock], multi_column_unique=[mock]])
|
116
|
+
@schema.stub!(:presence_constraints ).and_return([boolean_presence=[mock], presence_of =[mock]])
|
117
|
+
@schema.stub!(:numericality_constraints).and_return(numericality={} )
|
118
|
+
@schema.stub!(:custom_constraints ).and_return(custom =[mock])
|
119
119
|
|
120
120
|
@schema.constraints.should == {:unique =>unique,
|
121
121
|
:multi_column_unique=>multi_column_unique,
|
122
122
|
:boolean_presence =>boolean_presence,
|
123
123
|
:presence_of =>presence_of,
|
124
|
-
:numericality_of =>
|
124
|
+
:numericality_of =>numericality,
|
125
125
|
:custom =>custom }
|
126
126
|
end
|
127
127
|
|