Created
October 17, 2019 11:42
-
-
Save rebelweb/8e10ad612186fbe202170e3da14c6162 to your computer and use it in GitHub Desktop.
Update Counter Cache Job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UpdateCounterCacheJob < ApplicationJob attr_accessor :cache_columns | |
def perform(key = 'all') | |
self.cache_columns = counter_cache_hash | |
if key == 'all' | |
cache_columns.keys.each { |obj| update_cache_columns(obj) } | |
else | |
update_cache_columns(key) | |
end | |
end | |
private | |
def counter_cache_hash | |
file_name = format('%s/config/counter_cache_columns.json', Rails.root) | |
file = File.read(file_name) | |
JSON.parse(file) | |
end | |
def update_cache_columns(key) | |
model = key.camelize.constantize.base_class | |
cache_columns[key].each do |col| | |
model.find_each { |m| model.reset_counters(m.id, col) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment