diff options
Diffstat (limited to 'activerecord/lib/active_record/attributes.rb')
-rw-r--r-- | activerecord/lib/active_record/attributes.rb | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 890a1314d9..3288108a6a 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -6,7 +6,9 @@ module ActiveRecord included do class_attribute :user_provided_columns, instance_accessor: false # :internal: + class_attribute :user_provided_defaults, instance_accessor: false # :internal: self.user_provided_columns = {} + self.user_provided_defaults = {} end module ClassMethods # :nodoc: @@ -77,7 +79,11 @@ module ActiveRecord name = name.to_s clear_caches_calculated_from_columns # Assign a new hash to ensure that subclasses do not share a hash - self.user_provided_columns = user_provided_columns.merge(name => connection.new_column(name, options[:default], cast_type)) + self.user_provided_columns = user_provided_columns.merge(name => cast_type) + + if options.key?(:default) + self.user_provided_defaults = user_provided_defaults.merge(name => options[:default]) + end end # Returns an array of column objects for the table associated with this class. @@ -99,11 +105,18 @@ module ActiveRecord def add_user_provided_columns(schema_columns) existing_columns = schema_columns.map do |column| - user_provided_columns[column.name] || column + new_type = user_provided_columns[column.name] + if new_type + column.with_type(new_type) + else + column + end end existing_column_names = existing_columns.map(&:name) - new_columns = user_provided_columns.except(*existing_column_names).values + new_columns = user_provided_columns.except(*existing_column_names).map do |(name, type)| + connection.new_column(name, nil, type) + end existing_columns + new_columns end @@ -117,6 +130,10 @@ module ActiveRecord @content_columns = nil @default_attributes = nil end + + def raw_default_values + super.merge(user_provided_defaults) + end end end end |