提交 9798a11c 编写于 作者: A Aaron Patterson

extract methods out of the cache update method

上级 6772d5e7
......@@ -42,20 +42,28 @@ def update_counters(record)
return unless counter_cache_name && owner.persisted?
diff_target = if record
different_target?(record)
else
owner[reflection.foreign_key]
end
if diff_target
if record
record.class.increment_counter(counter_cache_name, record.id)
end
if foreign_key_present?
klass.decrement_counter(counter_cache_name, target_id)
end
if record
update_with_record record, counter_cache_name
else
update_without_record counter_cache_name
end
end
def update_with_record record, counter_cache_name
return unless different_target? record
record.class.increment_counter(counter_cache_name, record.id)
decrement_counter counter_cache_name
end
def update_without_record counter_cache_name
decrement_counter counter_cache_name
end
def decrement_counter counter_cache_name
if foreign_key_present?
klass.decrement_counter(counter_cache_name, target_id)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册