提交 15970efb 编写于 作者: J Jon Leighton

Merge pull request #9371 from benmoss/access-connection-via-class

Access an instance's connection via its class, rather than via #connection
## Rails 4.0.0 (unreleased) ##
* `connection` is deprecated as an instance method.
This allows end-users to have a `connection` method on their models
without clashing with ActiveRecord internals.
*Ben Moss*
* When copying migrations, preserve their magic comments and content encoding.
*OZAWA Sakuro*
......
......@@ -26,7 +26,7 @@ def insert_record(record, validate = true, raise = false)
join_table[reflection.association_foreign_key] => record.id
)
owner.connection.insert stmt
owner.class.connection.insert stmt
end
record
......@@ -41,7 +41,7 @@ def count_records
def delete_records(records, method)
if sql = options[:delete_sql]
records = load_target if records == :all
records.each { |record| owner.connection.delete(interpolate(sql, record)) }
records.each { |record| owner.class.connection.delete(interpolate(sql, record)) }
else
relation = join_table
condition = relation[reflection.foreign_key].eq(owner.id)
......@@ -53,7 +53,7 @@ def delete_records(records, method)
)
end
owner.connection.delete(relation.where(condition).compile_delete)
owner.class.connection.delete(relation.where(condition).compile_delete)
end
end
......
......@@ -324,6 +324,7 @@ def readonly!
# also be used to "borrow" the connection to do database work that isn't
# easily done without going straight to SQL.
def connection
ActiveSupport::Deprecation.warn("#connection is deprecated in favour of accessing it via the class")
self.class.connection
end
......
......@@ -86,7 +86,7 @@ def update_record(attribute_names = @attributes.keys) #:nodoc:
)
).arel.compile_update(arel_attributes_with_values_for_update(attribute_names))
affected_rows = connection.update stmt
affected_rows = self.class.connection.update stmt
unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update")
......@@ -117,7 +117,7 @@ def relation_for_destroy
if locking_enabled?
column_name = self.class.locking_column
column = self.class.columns_hash[column_name]
substitute = connection.substitute_at(column, relation.bind_values.length)
substitute = self.class.connection.substitute_at(column, relation.bind_values.length)
relation = relation.where(self.class.arel_table[column_name].eq(substitute))
relation.bind_values << [column, self[column_name].to_i]
......
......@@ -410,7 +410,7 @@ def destroy_row
def relation_for_destroy
pk = self.class.primary_key
column = self.class.columns_hash[pk]
substitute = connection.substitute_at(column, 0)
substitute = self.class.connection.substitute_at(column, 0)
relation = self.class.unscoped.where(
self.class.arel_table[pk].eq(substitute))
......
......@@ -477,9 +477,8 @@ class CustomConnectionFixturesTest < ActiveRecord::TestCase
fixtures :courses
self.use_transactional_fixtures = false
def test_connection
assert_kind_of Course, courses(:ruby)
assert_equal Course.connection, courses(:ruby).connection
def test_connection_instance_method_deprecation
assert_deprecated { courses(:ruby).connection }
end
def test_leaky_destroy
......
......@@ -182,9 +182,9 @@ def test_only_call_after_rollback_on_create_after_transaction_rollsback_for_new_
end
def test_call_after_rollback_when_commit_fails
@first.connection.class.send(:alias_method, :real_method_commit_db_transaction, :commit_db_transaction)
@first.class.connection.class.send(:alias_method, :real_method_commit_db_transaction, :commit_db_transaction)
begin
@first.connection.class.class_eval do
@first.class.connection.class.class_eval do
def commit_db_transaction; raise "boom!"; end
end
......@@ -194,8 +194,8 @@ def commit_db_transaction; raise "boom!"; end
assert !@first.save rescue nil
assert_equal [:after_rollback], @first.history
ensure
@first.connection.class.send(:remove_method, :commit_db_transaction)
@first.connection.class.send(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction)
@first.class.connection.class.send(:remove_method, :commit_db_transaction)
@first.class.connection.class.send(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册