Skip to content

Commit 6972c3a

Browse files
authored
Use lease_connection on Rails 7.2+ in ActiveRecord5Adapter (#894)
Rails 7.2 soft-deprecated ActiveRecord::Base.connection in favour of lease_connection, which better describes that the caller is acquiring a connection from the pool rather than holding a permanent one (see rails/rails#51230). Prefer lease_connection when available, and fall back to connection on Rails < 7.2 where it is not defined. Refs #893
1 parent 1100093 commit 6972c3a

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

lib/cancan/model_adapters/active_record_5_adapter.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,22 @@ def sanitize_sql_activerecord5(conditions)
4949
def visit_nodes(node)
5050
# Rails 5.2 adds a BindParam node that prevents the visitor method from properly compiling the SQL query
5151
if self.class.version_greater_or_equal?('5.2.0')
52-
connection = @model_class.send(:connection)
52+
connection = model_connection
5353
collector = Arel::Collectors::SubstituteBinds.new(connection, Arel::Collectors::SQLString.new)
5454
connection.visitor.accept(node, collector).value
5555
else
56-
@model_class.send(:connection).visitor.compile(node)
56+
model_connection.visitor.compile(node)
57+
end
58+
end
59+
60+
# Rails 7.2 soft-deprecated ActiveRecord::Base.connection in favour of
61+
# lease_connection, which better describes its lifecycle. Fall back to
62+
# connection for Rails < 7.2 where lease_connection is not defined.
63+
def model_connection
64+
if @model_class.respond_to?(:lease_connection)
65+
@model_class.lease_connection
66+
else
67+
@model_class.send(:connection)
5768
end
5869
end
5970
end

0 commit comments

Comments
 (0)