1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Simplify creation of default attributes on AR instance

`AttributeSet#dup` has all the behavior we need.
This commit is contained in:
Sean Griffin 2014-06-29 15:09:11 -06:00
parent f123da507a
commit 9e368a5030
4 changed files with 10 additions and 21 deletions

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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