Skip to content

Commit

Permalink
feat: use with_connection where possible (#441)
Browse files Browse the repository at this point in the history
rails/rails#51349 - this allows rails 7.2 applications
to raise an error or emit a deprecation warning whenever
`ActiveRecord::Base.connection` is used.

this makes sure that in rails 7.1 and after we use the new, preferred method.
  • Loading branch information
flood4life authored Oct 13, 2024
1 parent 07f6c27 commit 608cca2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
6 changes: 5 additions & 1 deletion lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative './with_connection'

module ActiveRecord
module Acts #:nodoc:
module List #:nodoc:
Expand Down Expand Up @@ -459,7 +461,9 @@ def check_top_position

# When using raw column name it must be quoted otherwise it can raise syntax errors with SQL keywords (e.g. order)
def quoted_position_column
@_quoted_position_column ||= self.class.connection.quote_column_name(position_column)
@_quoted_position_column ||= ActiveRecord::Acts::List::WithConnection.new(self.class).call do |connection|
connection.quote_column_name(position_column)
end
end

# Used in order clauses
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative './with_connection'

module ActiveRecord::Acts::List::PositionColumnMethodDefiner #:nodoc:
def self.call(caller_class, position_column, touch_on_update)
define_class_methods(caller_class, position_column, touch_on_update)
Expand All @@ -15,7 +17,9 @@ def self.call(caller_class, position_column, touch_on_update)
def self.define_class_methods(caller_class, position_column, touch_on_update)
caller_class.class_eval do
define_singleton_method :quoted_position_column do
@_quoted_position_column ||= connection.quote_column_name(position_column)
@_quoted_position_column ||= ActiveRecord::Acts::List::WithConnection.new(self).call do |connection|
connection.quote_column_name(position_column)
end
end

define_singleton_method :quoted_position_column_with_table_name do
Expand Down Expand Up @@ -72,18 +76,22 @@ def self.define_instance_methods(caller_class, position_column)
cached_quoted_now = quoted_current_time_from_proper_timezone

timestamp_attributes_for_update_in_model.map do |attr|
", #{self.class.connection.quote_column_name(attr)} = #{cached_quoted_now}"
ActiveRecord::Acts::List::WithConnection.new(self.class).call do |connection|
", #{connection.quote_column_name(attr)} = #{cached_quoted_now}"
end
end.join
end

private

def quoted_current_time_from_proper_timezone
self.class.connection.quote(
self.class.connection.quoted_date(
current_time_from_proper_timezone
ActiveRecord::Acts::List::WithConnection.new(self.class).call do |connection|
connection.quote(
connection.quoted_date(
current_time_from_proper_timezone
)
)
)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

require_relative './with_connection'

module ActiveRecord::Acts::List::SequentialUpdatesMethodDefiner #:nodoc:
def self.call(caller_class, column, sequential_updates_option)
caller_class.class_eval do
define_method :sequential_updates? do
if !defined?(@sequential_updates)
if sequential_updates_option.nil?
table_exists =
if active_record_version_is?('>= 5')
caller_class.connection.data_source_exists?(caller_class.table_name)
else
caller_class.connection.table_exists?(caller_class.table_name)
end
index_exists = caller_class.connection.index_exists?(caller_class.table_name, column, unique: true)
@sequential_updates = table_exists && index_exists
else
@sequential_updates = sequential_updates_option
end
else
@sequential_updates
return @sequential_updates if defined?(@sequential_updates)

return @sequential_updates = sequential_updates_option unless sequential_updates_option.nil?

ActiveRecord::Acts::List::WithConnection.new(caller_class).call do |connection|
table_exists =
if active_record_version_is?('>= 5')
connection.data_source_exists?(caller_class.table_name)
else
connection.table_exists?(caller_class.table_name)
end
index_exists = connection.index_exists?(caller_class.table_name, column, unique: true)
@sequential_updates = table_exists && index_exists
end
end

Expand Down
25 changes: 25 additions & 0 deletions lib/acts_as_list/active_record/acts/with_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module ActiveRecord
module Acts
module List
class WithConnection
def initialize(recipient)
@recipient = recipient
end

attr_reader :recipient

def call
if recipient.respond_to?(:with_connection)
recipient.with_connection do |connection|
yield connection
end
else
yield recipient.connection
end
end
end
end
end
end

0 comments on commit 608cca2

Please sign in to comment.