aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-29 22:50:12 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-29 22:50:12 -0300
commit1f190b7ceb7dabbfd6b7b14fe48ed79bcc3a83fd (patch)
tree3a8ee885db4d2cf8f9584817fc44406cf9de39ab /activerecord
parent74cec956e80611df621c9d831696e3b59c65e482 (diff)
parent9e368a5030728f054589efc3a0acc0f68265851b (diff)
downloadrails-1f190b7ceb7dabbfd6b7b14fe48ed79bcc3a83fd.tar.gz
rails-1f190b7ceb7dabbfd6b7b14fe48ed79bcc3a83fd.tar.bz2
rails-1f190b7ceb7dabbfd6b7b14fe48ed79bcc3a83fd.zip
Merge pull request #15981 from sgrif/sg-simplify-defaults
Simplify creation of default attributes on AR instance
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attributes.rb3
-rw-r--r--activerecord/lib/active_record/core.rb7
-rw-r--r--activerecord/lib/active_record/model_schema.rb20
3 files changed, 9 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 492d8f3560..890a1314d9 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -110,13 +110,12 @@ module ActiveRecord
def clear_caches_calculated_from_columns
@attributes_builder = nil
- @column_defaults = nil
@column_names = nil
@column_types = nil
@columns = nil
@columns_hash = nil
@content_columns = nil
- @raw_column_defaults = nil
+ @default_attributes = nil
end
end
end
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 3321e268d5..224112b559 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -248,12 +248,7 @@ module ActiveRecord
# # Instantiates a single new object
# User.new(first_name: 'Jamie')
def initialize(attributes = nil, options = {})
- defaults = {}
- self.class.raw_column_defaults.each do |k, v|
- defaults[k] = v.duplicable? ? v.dup : v
- end
-
- @attributes = self.class.attributes_builder.build_from_database(defaults)
+ @attributes = self.class.default_attributes.dup
init_internals
initialize_internals_callback
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 099042cab2..092c3b4fb7 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -224,8 +224,8 @@ module ActiveRecord
end
def column_types # :nodoc:
- @column_types ||= Hash.new(Type::Value.new).tap do |column_types|
- columns.each { |column| column_types[column.name] = column.cast_type }
+ @column_types ||= columns_hash.transform_values(&:cast_type).tap do |h|
+ h.default = Type::Value.new
end
end
@@ -236,17 +236,12 @@ module ActiveRecord
# Returns a hash where the keys are column names and the values are
# default values when instantiating the AR object for this table.
def column_defaults
- @column_defaults ||= Hash[raw_column_defaults.map { |name, default|
- [name, type_for_attribute(name).type_cast_from_database(default)]
- }]
+ default_attributes.to_hash
end
- # Returns a hash where the keys are the column names and the values
- # are the default values suitable for use in `@raw_attriubtes`
- def raw_column_defaults # :nodoc:
- @raw_column_defaults ||= Hash[columns_hash.map { |name, column|
- [name, column.default]
- }]
+ def default_attributes # :nodoc:
+ @default_attributes ||= attributes_builder.build_from_database(
+ columns_hash.transform_values(&:default))
end
# Returns an array of column names as strings.
@@ -292,11 +287,10 @@ module ActiveRecord
connection.schema_cache.clear_table_cache!(table_name) if table_exists?
@arel_engine = nil
- @column_defaults = nil
- @raw_column_defaults = nil
@column_names = nil
@column_types = nil
@content_columns = nil
+ @default_attributes = nil
@dynamic_methods_hash = nil
@inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
@relation = nil