From 9e368a5030728f054589efc3a0acc0f68265851b Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 29 Jun 2014 15:09:11 -0600 Subject: [PATCH] Simplify creation of default attributes on AR instance `AttributeSet#dup` has all the behavior we need. --- activerecord/lib/active_record/attributes.rb | 3 +-- activerecord/lib/active_record/base.rb | 1 + activerecord/lib/active_record/core.rb | 7 +------ .../lib/active_record/model_schema.rb | 20 +++++++------------ 4 files changed, 10 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/base.rb b/activerecord/lib/active_record/base.rb index 662c99269e..32e7c50588 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/class/delegating_attributes' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/slice' +require 'active_support/core_ext/hash/transform_values' require 'active_support/core_ext/string/behavior' require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/introspection' 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