4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/composite_primary_keys/counter_cache.rb', line 4
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
names = touch if touch != true
touch_updates = touch_attributes_with_time(*names)
updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
end
if id.is_a?(Relation) && self == id.klass
relation = id
elsif primary_key.is_a?(Array)
predicate = self.cpk_id_predicate(self.arel_table, self.primary_key, id)
relation = unscoped.where!(predicate)
else
relation = unscoped.where!(primary_key => id)
end
relation.update_all updates.join(", ")
end
|