Sha256: 818d4eb212b9b16cf63b37ca5b463ee8eaf78cce58f62a08a5e7ad51761f63ad

Contents?: true

Size: 937 Bytes

Versions: 4

Compression:

Stored size: 937 Bytes

Contents

module ActiveRecord
  module CounterCache
    module ClassMethods
      def update_counters(id, counters)
        touch = counters.delete(:touch)

        updates = counters.map do |counter_name, value|
          operator = value < 0 ? "-" : "+"
          quoted_column = connection.quote_column_name(counter_name)
          "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
        end

        if touch
          touch_updates = touch_updates(touch)
          updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
        end

        # CPK
        if primary_key.is_a?(Array)
          predicate = self.cpk_id_predicate(self.arel_table, self.primary_key, id)
          unscoped.where(predicate).update_all updates.join(", ")
        else
          unscoped.where(primary_key => id).update_all updates.join(", ")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
composite_primary_keys-11.0.3 lib/composite_primary_keys/counter_cache.rb
composite_primary_keys-11.0.2 lib/composite_primary_keys/counter_cache.rb
composite_primary_keys-11.0.1 lib/composite_primary_keys/counter_cache.rb
composite_primary_keys-11.0.0 lib/composite_primary_keys/counter_cache.rb