Permalink
Browse files

Deprecate #connection in favour of accessing it via the class

This allows end-users to have a `connection` method on their models
without clashing with ActiveRecord internals.
  • Loading branch information...
benmoss committed Mar 9, 2013
1 parent ca35454 commit 992d87db02507363a4ad4ab40a3e4c4b32cf0408
@@ -1,5 +1,11 @@
## 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

0 comments on commit 992d87d

Please sign in to comment.