Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Oct 27, 2023
1 parent 6096ad7 commit 1091171
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lib/activerecord_slotted_counters/adapters/mysql_upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
module ActiveRecordSlottedCounters
module Adapters
class MysqlUpsert
attr_reader :klass, :current_adapter_name
attr_reader :klass

def initialize(klass, current_adapter_name)
def initialize(klass)
@klass = klass
@current_adapter_name = current_adapter_name
end

def apply?
def apply?(current_adapter_name)
return false unless defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)

current_adapter_name == ActiveRecord::ConnectionAdapters::Mysql2Adapter::ADAPTER_NAME
end

def bulk_insert(attributes, on_duplicate: nil, unique_by: nil)
def bulk_insert(attributes, on_duplicate: nil, **)
raise ArgumentError, "Values must not be empty" if attributes.empty?

keys = attributes.first.keys + klass.all_timestamp_attributes_in_model
Expand Down Expand Up @@ -58,6 +57,7 @@ def columns_for_attributes(attributes)
def quote_column_names(columns, table_name: false)
columns.map do |column|
column_name = klass.connection.quote_column_name(column.name)

if table_name
"#{klass.quoted_table_name}.#{column_name}"
else
Expand All @@ -71,6 +71,7 @@ def quote_record(columns, record_values)
type = klass.connection.lookup_cast_type_from_column(columns[i])
klass.connection.quote(type.serialize(value))
end.join(",")

"(#{values_str})"
end

Expand Down
7 changes: 3 additions & 4 deletions lib/activerecord_slotted_counters/adapters/pg_upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
module ActiveRecordSlottedCounters
module Adapters
class PgUpsert
attr_reader :klass, :current_adapter_name
attr_reader :klass

def initialize(klass, current_adapter_name)
def initialize(klass)
@klass = klass
@current_adapter_name = current_adapter_name
end

def apply?
def apply?(current_adapter_name)
return false if ActiveRecord::VERSION::MAJOR >= 7
return false unless defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)

Expand Down
7 changes: 3 additions & 4 deletions lib/activerecord_slotted_counters/adapters/rails_upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
module ActiveRecordSlottedCounters
module Adapters
class RailsUpsert
attr_reader :klass, :current_adapter_name
attr_reader :klass

def initialize(klass, current_adapter_name)
def initialize(klass)
@klass = klass
@current_adapter_name = current_adapter_name
end

def apply?
def apply?(_)
ActiveRecord::VERSION::MAJOR >= 7
end

Expand Down
6 changes: 3 additions & 3 deletions lib/activerecord_slotted_counters/has_slotted_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def set_slotted_counter_db_adapter
current_adapter_name = connection.adapter_name

adapter = available_adapters
.map { |adapter| adapter.new(self, current_adapter_name) }
.detect { |adapter| adapter.apply? }
.map { |adapter| adapter.new(self) }
.detect { |adapter| adapter.apply?(current_adapter_name) }

raise NotSupportedAdapter.new(current_adapter_name) if adapter.nil?

Expand Down Expand Up @@ -173,7 +173,7 @@ def registered_slotted_counter?(counter_name)
slotted_counters.include? counter_type
end

def update_slotted_counters(ids, registered_counters, touch, with_log = false)
def update_slotted_counters(ids, registered_counters, touch)
updated_counters_count = 0
ActiveRecord::Base.transaction do
updated_counters_count = insert_counters_records(ids, registered_counters)
Expand Down

0 comments on commit 1091171

Please sign in to comment.