Permalink
Browse files

automatically add the column definition to the columns list if creati…

…ng a new one
  • Loading branch information...
1 parent 1fc47a1 commit 483a3cf2872a6a5d544f2c15cb60d0bf2bce036a @tenderlove tenderlove committed Dec 8, 2011
Showing with 21 additions and 12 deletions.
  1. +21 −12 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -225,17 +225,20 @@ def [](name)
# t.references :taggable, :polymorphic => { :default => 'Photo' }
# end
def column(name, type, options = {})
- column = self[name] || ColumnDefinition.new(@base, name.to_s, type)
- if options[:limit]
- column.limit = options[:limit]
- elsif native[type.to_sym].is_a?(Hash)
- column.limit = native[type.to_sym][:limit]
+ name = name.to_s
+ type = type.to_sym
+
+ column = self[name] || new_column_definition(@base, name, type)
+
+ limit = options.fetch(:limit) do
+ native[type][:limit] if native[type].is_a?(Hash)
end
+
+ column.limit = limit
column.precision = options[:precision]
- column.scale = options[:scale]
- column.default = options[:default]
- column.null = options[:null]
- @columns << column unless @columns.include? column
+ column.scale = options[:scale]
+ column.default = options[:default]
+ column.null = options[:null]
self
end
@@ -276,9 +279,15 @@ def to_sql
end
private
- def native
- @base.native_database_types
- end
+ def new_column_definition(base, name, type)
+ definition = ColumnDefinition.new base, name, type
+ @columns << definition
+ definition
+ end
+
+ def native
+ @base.native_database_types
+ end
end
# Represents an SQL table in an abstract way for updating a table.

0 comments on commit 483a3cf

Please sign in to comment.