module AwesomeCounterCache::InstanceMethods

Public Instance Methods

awesome_counter_cache_after_commit_on_create(args, model, relation_name) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 2
def awesome_counter_cache_after_commit_on_create(args, model, relation_name)
  id = args.fetch(:id)
  @awesome_counter_cache_data.fetch(id)[:delta_current] = args.fetch(:delta_magnitude).call(model)
  model.create_awesome_counter_cache_for(relation_name, @awesome_counter_cache_data.fetch(id), args)
  @awesome_counter_cache_data[id][:delta_original] = @awesome_counter_cache_data[id].delete(:delta_current)
end
awesome_counter_cache_after_commit_on_destroy(args, model, relation_name) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 9
def awesome_counter_cache_after_commit_on_destroy(args, model, relation_name)
  id = args.fetch(:id)
  @awesome_counter_cache_data.fetch(id)[:delta_current] = args.fetch(:delta_magnitude).call(model)
  model.destroy_awesome_counter_cache_for(relation_name, @awesome_counter_cache_data.fetch(id), args)
  @awesome_counter_cache_data[id][:delta_original] = @awesome_counter_cache_data[id].delete(:delta_current)
end
awesome_counter_cache_after_commit_on_update(args, model, relation_name) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 16
def awesome_counter_cache_after_commit_on_update(args, model, relation_name)
  id = args.fetch(:id)
  @awesome_counter_cache_data.fetch(id)[:delta_current] = args.fetch(:delta_magnitude).call(model)
  model.update_awesome_counter_cache_for(relation_name, @awesome_counter_cache_data.fetch(id), args)
  @awesome_counter_cache_data[id][:delta_original] = @awesome_counter_cache_data[id].delete(:delta_current)
end
awesome_counter_cache_after_initialize(args, model) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 23
def awesome_counter_cache_after_initialize(args, model)
  id = args.fetch(:id)

  @awesome_counter_cache_data ||= {}
  @awesome_counter_cache_data[id] ||= {}

  primary_key_column = self.class.primary_key

  if read_attribute(primary_key_column).nil? # `new_record?` always returns false so check if primary key is nil instead - kaspernj
    @awesome_counter_cache_data.fetch(id)[:delta_original] ||= 0
  else
    @awesome_counter_cache_data.fetch(id)[:delta_original] ||= args.fetch(:delta_magnitude).call(model)
  end
end
awesome_counter_cache_reset_original_values() click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 56
def awesome_counter_cache_reset_original_values
  self.class.awesome_counter_caches.each do |id, args|
    @awesome_counter_cache_data.fetch(id)[:delta_original] = args.fetch(:delta_magnitude).call(self)
  end
end
create_awesome_counter_cache_for(relation_name, state_data, args) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 38
def create_awesome_counter_cache_for(relation_name, state_data, args)
  relation_model = __send__(relation_name)
  return if relation_model.blank?

  addition = state_data.fetch(:delta_current)

  if addition != 0
    primary_key_value = relation_model.read_attribute(relation_model.class.primary_key)
    relation_model.class.update_counters(primary_key_value, args.fetch(:column_name) => addition)
  end
end
destroy_awesome_counter_cache_for(relation_name, state_data, args) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 62
def destroy_awesome_counter_cache_for(relation_name, state_data, args)
  relation_model = __send__(relation_name)
  return if relation_model.blank?

  addition = -state_data.fetch(:delta_original)

  if addition != 0
    primary_key_value = relation_model.read_attribute(relation_model.class.primary_key)
    relation_model.class.update_counters(primary_key_value, args.fetch(:column_name) => addition)
  end
end
reload(*args, &blk) click to toggle source
Calls superclass method
# File lib/awesome_counter_cache/instance_methods.rb, line 50
def reload(*args, &blk)
  result = super
  awesome_counter_cache_reset_original_values
  result
end
update_awesome_counter_cache_for(relation_name, state_data, args) click to toggle source
# File lib/awesome_counter_cache/instance_methods.rb, line 74
def update_awesome_counter_cache_for(relation_name, state_data, args)
  relation_model = __send__(relation_name)
  return if relation_model.blank?

  addition = state_data.fetch(:delta_current) - state_data.fetch(:delta_original)

  if addition != 0
    primary_key_value = relation_model.read_attribute(relation_model.class.primary_key)
    relation_model.class.update_counters(primary_key_value, args.fetch(:column_name) => addition)
  end
end