aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-10-31 16:06:14 -0600
committerSean Griffin <sean@thoughtbot.com>2014-10-31 16:06:14 -0600
commit4010a9ddc6d91c40cc87a30c03e8dfd9ae214dbb (patch)
treeb8a7aef2c520d536f84065031a43655115f43b3e /activerecord/lib
parent61bacd6ef9c302238f3b8d9e6b432ee2090f6b78 (diff)
downloadrails-4010a9ddc6d91c40cc87a30c03e8dfd9ae214dbb.tar.gz
rails-4010a9ddc6d91c40cc87a30c03e8dfd9ae214dbb.tar.bz2
rails-4010a9ddc6d91c40cc87a30c03e8dfd9ae214dbb.zip
Don't modify the columns hash to set defaults from the attributes API
Nothing is directly using the columns for the default values anymore. This step helps us get closer not not mutating the columns hash.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/attributes.rb23
-rw-r--r--activerecord/lib/active_record/model_schema.rb6
2 files changed, 25 insertions, 4 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
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 31ff08a89d..a444aac23c 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -252,7 +252,7 @@ module ActiveRecord
def _default_attributes # :nodoc:
@default_attributes ||= attributes_builder.build_from_database(
- columns_hash.transform_values(&:default))
+ raw_default_values)
end
# Returns an array of column names as strings.
@@ -331,6 +331,10 @@ module ActiveRecord
base.table_name
end
end
+
+ def raw_default_values
+ columns_hash.transform_values(&:default)
+ end
end
end
end