mirror of https://github.com/rails/rails
Duplicate column_defaults properly (closes #6115)
This commit is contained in:
parent
2ec6ef5dda
commit
c5176023a0
|
@ -1,5 +1,6 @@
|
|||
require 'active_support/concern'
|
||||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
require 'active_support/core_ext/object/duplicable'
|
||||
require 'thread'
|
||||
|
||||
module ActiveRecord
|
||||
|
@ -165,7 +166,9 @@ module ActiveRecord
|
|||
# # Instantiates a single new object bypassing mass-assignment security
|
||||
# User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
|
||||
def initialize(attributes = nil, options = {})
|
||||
@attributes = self.class.initialize_attributes(self.class.column_defaults.dup)
|
||||
# TODO: use deep_dup after fixing it to also dup values
|
||||
defaults = Hash[self.class.column_defaults.map { |k, v| [k, v.duplicable? ? v.dup : v] }]
|
||||
@attributes = self.class.initialize_attributes(defaults)
|
||||
@columns_hash = self.class.column_types.dup
|
||||
|
||||
init_internals
|
||||
|
|
|
@ -1909,7 +1909,7 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_attribute_names
|
||||
assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id"],
|
||||
assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id", "description"],
|
||||
Company.attribute_names
|
||||
end
|
||||
|
||||
|
@ -2001,6 +2001,12 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert_nil hash['firm_name']
|
||||
end
|
||||
|
||||
def test_default_values_are_deeply_dupped
|
||||
company = Company.new
|
||||
company.description << "foo"
|
||||
assert_equal "", Company.new.description
|
||||
end
|
||||
|
||||
["find_by", "find_by!"].each do |meth|
|
||||
test "#{meth} delegates to scoped" do
|
||||
record = stub
|
||||
|
|
|
@ -173,6 +173,7 @@ ActiveRecord::Schema.define do
|
|||
t.integer :client_of
|
||||
t.integer :rating, :default => 1
|
||||
t.integer :account_id
|
||||
t.string :description, :null => false, :default => ""
|
||||
end
|
||||
|
||||
add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"
|
||||
|
|
Loading…
Reference in New Issue