提交 4d3f0217 编写于 作者: E Eileen M. Uchitelle 提交者: eileencodes

Merge pull request #36930 from eugeneius/active_record_base_connection_specification_name

Fix setting connection_specification_name on ActiveRecord::Base
上级 d5205571
...@@ -204,7 +204,7 @@ def connection ...@@ -204,7 +204,7 @@ def connection
# Return the specification name from the current class or its parent. # Return the specification name from the current class or its parent.
def connection_specification_name def connection_specification_name
if !defined?(@connection_specification_name) || @connection_specification_name.nil? if !defined?(@connection_specification_name) || @connection_specification_name.nil?
return primary_class? ? "primary" : superclass.connection_specification_name return self == Base ? "primary" : superclass.connection_specification_name
end end
@connection_specification_name @connection_specification_name
end end
......
...@@ -375,6 +375,8 @@ class MyClass < ApplicationRecord ...@@ -375,6 +375,8 @@ class MyClass < ApplicationRecord
end end
def test_connection_specification_name_should_fallback_to_parent def test_connection_specification_name_should_fallback_to_parent
Object.send :const_set, :ApplicationRecord, ApplicationRecord
klassA = Class.new(Base) klassA = Class.new(Base)
klassB = Class.new(klassA) klassB = Class.new(klassA)
klassC = Class.new(MyClass) klassC = Class.new(MyClass)
...@@ -387,6 +389,12 @@ def test_connection_specification_name_should_fallback_to_parent ...@@ -387,6 +389,12 @@ def test_connection_specification_name_should_fallback_to_parent
klassA.connection_specification_name = "readonly" klassA.connection_specification_name = "readonly"
assert_equal "readonly", klassB.connection_specification_name assert_equal "readonly", klassB.connection_specification_name
ActiveRecord::Base.connection_specification_name = "readonly"
assert_equal "readonly", klassC.connection_specification_name
ensure
Object.send :remove_const, :ApplicationRecord
ActiveRecord::Base.connection_specification_name = "primary"
end end
def test_remove_connection_should_not_remove_parent def test_remove_connection_should_not_remove_parent
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册