From 608cca2d4172594b4ec392422220165533b9f12f Mon Sep 17 00:00:00 2001 From: Murad Iusufov Date: Sun, 13 Oct 2024 23:14:28 +0100 Subject: [PATCH] feat: use `with_connection` where possible (#441) https://github.com/rails/rails/pull/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. --- lib/acts_as_list/active_record/acts/list.rb | 6 +++- .../acts/position_column_method_definer.rb | 20 +++++++++---- .../acts/sequential_updates_method_definer.rb | 30 +++++++++---------- .../active_record/acts/with_connection.rb | 25 ++++++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 lib/acts_as_list/active_record/acts/with_connection.rb diff --git a/lib/acts_as_list/active_record/acts/list.rb b/lib/acts_as_list/active_record/acts/list.rb index e2a17a87..7745bc6f 100644 --- a/lib/acts_as_list/active_record/acts/list.rb +++ b/lib/acts_as_list/active_record/acts/list.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative './with_connection' + module ActiveRecord module Acts #:nodoc: module List #:nodoc: @@ -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 diff --git a/lib/acts_as_list/active_record/acts/position_column_method_definer.rb b/lib/acts_as_list/active_record/acts/position_column_method_definer.rb index 26c85d9b..ed8fdb7d 100644 --- a/lib/acts_as_list/active_record/acts/position_column_method_definer.rb +++ b/lib/acts_as_list/active_record/acts/position_column_method_definer.rb @@ -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) @@ -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 @@ -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 diff --git a/lib/acts_as_list/active_record/acts/sequential_updates_method_definer.rb b/lib/acts_as_list/active_record/acts/sequential_updates_method_definer.rb index 2419d16b..8dbf5558 100644 --- a/lib/acts_as_list/active_record/acts/sequential_updates_method_definer.rb +++ b/lib/acts_as_list/active_record/acts/sequential_updates_method_definer.rb @@ -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 diff --git a/lib/acts_as_list/active_record/acts/with_connection.rb b/lib/acts_as_list/active_record/acts/with_connection.rb new file mode 100644 index 00000000..6e86e39d --- /dev/null +++ b/lib/acts_as_list/active_record/acts/with_connection.rb @@ -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