automatically add the column definition to the columns list if creating a new one

This commit is contained in:
Aaron Patterson 2011-12-07 16:01:53 -08:00
parent 1fc47a1a84
commit 483a3cf287

@ -225,17 +225,20 @@ def [](name)
# t.references :taggable, :polymorphic => { :default => 'Photo' } # t.references :taggable, :polymorphic => { :default => 'Photo' }
# end # end
def column(name, type, options = {}) def column(name, type, options = {})
column = self[name] || ColumnDefinition.new(@base, name.to_s, type) name = name.to_s
if options[:limit] type = type.to_sym
column.limit = options[:limit]
elsif native[type.to_sym].is_a?(Hash) column = self[name] || new_column_definition(@base, name, type)
column.limit = native[type.to_sym][:limit]
limit = options.fetch(:limit) do
native[type][:limit] if native[type].is_a?(Hash)
end end
column.limit = limit
column.precision = options[:precision] column.precision = options[:precision]
column.scale = options[:scale] column.scale = options[:scale]
column.default = options[:default] column.default = options[:default]
column.null = options[:null] column.null = options[:null]
@columns << column unless @columns.include? column
self self
end end
@ -276,9 +279,15 @@ def to_sql
end end
private private
def native def new_column_definition(base, name, type)
@base.native_database_types definition = ColumnDefinition.new base, name, type
end @columns << definition
definition
end
def native
@base.native_database_types
end
end end
# Represents an SQL table in an abstract way for updating a table. # Represents an SQL table in an abstract way for updating a table.