Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use with_connection where possible #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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