legacy_data 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
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
- <%= "validates_numericality_of #{constraints[:numericality_of].map {|cols| cols.downcase.to_sym.inspect}.join(', ')}" unless constraints[:numericality_of].blank? %>
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{legacy_data}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Rothenberg"]
@@ -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] = integer_column_names
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 integer_column_names
155
- columns.select {|column| column.type == :integer }.map &:name
161
+ def integer_columns
162
+ columns.select {|column| column.type == :integer }
156
163
  end
157
164
 
158
165
  def columns
@@ -8,7 +8,5 @@ class Post < ActiveRecord::Base
8
8
  # Constraints
9
9
  validates_uniqueness_of :title
10
10
  validates_presence_of :body
11
-
12
-
13
11
  end
14
12
 
@@ -7,7 +7,7 @@ class Comment < ActiveRecord::Base
7
7
  # Constraints
8
8
 
9
9
 
10
- validates_numericality_of :id, :post_id
11
-
10
+ validates_numericality_of :post_id, {:allow_nil=>true}
11
+ validates_numericality_of :id
12
12
  end
13
13
 
@@ -8,6 +8,5 @@ class Post < ActiveRecord::Base
8
8
 
9
9
 
10
10
  validates_numericality_of :id
11
-
12
11
  end
13
12
 
@@ -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 ).and_return([boolean_presence=[mock], presence_of =[mock]])
117
- @schema.stub!(:integer_column_names ).and_return(int_col_names =[mock])
118
- @schema.stub!(:custom_constraints ).and_return(custom =[mock])
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 =>int_col_names,
124
+ :numericality_of =>numericality,
125
125
  :custom =>custom }
126
126
  end
127
127
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legacy_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rothenberg