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

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

Access an instance's connection via its class, rather than via #connection
This commit is contained in:
Jon Leighton 2013-03-09 12:36:53 -08:00
commit 15970efb2a
7 changed files with 19 additions and 13 deletions

View file

@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ## ## 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. * When copying migrations, preserve their magic comments and content encoding.
*OZAWA Sakuro* *OZAWA Sakuro*

View file

@ -26,7 +26,7 @@ module ActiveRecord
join_table[reflection.association_foreign_key] => record.id join_table[reflection.association_foreign_key] => record.id
) )
owner.connection.insert stmt owner.class.connection.insert stmt
end end
record record
@ -41,7 +41,7 @@ module ActiveRecord
def delete_records(records, method) def delete_records(records, method)
if sql = options[:delete_sql] if sql = options[:delete_sql]
records = load_target if records == :all 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 else
relation = join_table relation = join_table
condition = relation[reflection.foreign_key].eq(owner.id) condition = relation[reflection.foreign_key].eq(owner.id)
@ -53,7 +53,7 @@ module ActiveRecord
) )
end end
owner.connection.delete(relation.where(condition).compile_delete) owner.class.connection.delete(relation.where(condition).compile_delete)
end end
end end

View file

@ -324,6 +324,7 @@ module ActiveRecord
# also be used to "borrow" the connection to do database work that isn't # also be used to "borrow" the connection to do database work that isn't
# easily done without going straight to SQL. # easily done without going straight to SQL.
def connection def connection
ActiveSupport::Deprecation.warn("#connection is deprecated in favour of accessing it via the class")
self.class.connection self.class.connection
end end

View file

@ -86,7 +86,7 @@ module ActiveRecord
) )
).arel.compile_update(arel_attributes_with_values_for_update(attribute_names)) ).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 unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update") raise ActiveRecord::StaleObjectError.new(self, "update")
@ -117,7 +117,7 @@ module ActiveRecord
if locking_enabled? if locking_enabled?
column_name = self.class.locking_column column_name = self.class.locking_column
column = self.class.columns_hash[column_name] 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 = relation.where(self.class.arel_table[column_name].eq(substitute))
relation.bind_values << [column, self[column_name].to_i] relation.bind_values << [column, self[column_name].to_i]

View file

@ -410,7 +410,7 @@ module ActiveRecord
def relation_for_destroy def relation_for_destroy
pk = self.class.primary_key pk = self.class.primary_key
column = self.class.columns_hash[pk] 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( relation = self.class.unscoped.where(
self.class.arel_table[pk].eq(substitute)) self.class.arel_table[pk].eq(substitute))

View file

@ -477,9 +477,8 @@ class CustomConnectionFixturesTest < ActiveRecord::TestCase
fixtures :courses fixtures :courses
self.use_transactional_fixtures = false self.use_transactional_fixtures = false
def test_connection def test_connection_instance_method_deprecation
assert_kind_of Course, courses(:ruby) assert_deprecated { courses(:ruby).connection }
assert_equal Course.connection, courses(:ruby).connection
end end
def test_leaky_destroy def test_leaky_destroy

View file

@ -182,9 +182,9 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
end end
def test_call_after_rollback_when_commit_fails 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 begin
@first.connection.class.class_eval do @first.class.connection.class.class_eval do
def commit_db_transaction; raise "boom!"; end def commit_db_transaction; raise "boom!"; end
end end
@ -194,8 +194,8 @@ class TransactionCallbacksTest < ActiveRecord::TestCase
assert !@first.save rescue nil assert !@first.save rescue nil
assert_equal [:after_rollback], @first.history assert_equal [:after_rollback], @first.history
ensure ensure
@first.connection.class.send(:remove_method, :commit_db_transaction) @first.class.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(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction)
end end
end end