module CachedAt::Base

Public Instance Methods

cached_at(ignore: []) click to toggle source
# File lib/cached_at/base.rb, line 26
def cached_at(ignore: [])
  ignore = [ignore] if !ignore.is_a?(Array)
  self.cached_at_settings[:ignore].push(*ignore.map(&:to_s))
end
touch(*names, time: nil) click to toggle source
Calls superclass method
# File lib/cached_at/base.rb, line 32
def touch(*names, time: nil)
  names.push('cached_at')
  super(*names, time: time)
end

Private Instance Methods

set_cached_at() click to toggle source
# File lib/cached_at/base.rb, line 66
def set_cached_at
  return if !self.class.column_names.include?('cached_at')
  diff = changes.transform_values(&:first)
  return if diff.keys.all? { |k| cached_at_settings[:ignore].include?(k) }

  self.cached_at = current_time_from_proper_timezone
end
update_belongs_to_cached_at_keys() click to toggle source
# File lib/cached_at/base.rb, line 74
def update_belongs_to_cached_at_keys
  self.class.reflect_on_all_associations.each do |reflection|
    next unless reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
    cache_column = "#{reflection.name}_cached_at"

    if self.attribute_names.include?(cache_column)
      if self.changes_to_save.has_key?(reflection.foreign_key) && self.changes_to_save[reflection.foreign_key][1].nil?
        self.assign_attributes({ cache_column => current_time_from_proper_timezone })
      elsif (self.changes_to_save[reflection.foreign_key] || self.new_record? || (self.association(reflection.name).loaded? && self.send(reflection.name) && self.send(reflection.name).id.nil?)) && self.send(reflection.name).try(:cached_at)
        self.assign_attributes({ cache_column => self.send(reflection.name).cached_at })
      end
    end
    
  end
end
update_relations_cached_at(timestamp: nil, method: nil) click to toggle source
# File lib/cached_at/base.rb, line 46
def update_relations_cached_at(timestamp: nil, method: nil)
  method = @_new_record_before_last_commit ? :create : :update if method.nil?
  
  diff = saved_changes.transform_values(&:first)
  return if method == :create && diff.empty?
  return if method == :update && diff.empty?
  
  timestamp ||= current_time_from_proper_timezone

  self._reflections.each do |name, reflection|
    next unless reflection.options[:cached_at] || reflection&.parent_reflection&.class == ActiveRecord::Reflection::HasAndBelongsToManyReflection || !reflection.through_relationship_endpoints.empty?
    next if instance_variable_defined?(:@relationships_cached_at_touched) && (!@relationships_cached_at_touched.nil? && @relationships_cached_at_touched[reflection.name])
    next if reflection.is_a?(ActiveRecord::Reflection::HasManyReflection) && method == :create

    assoc = association(name.to_sym)
    assoc.touch_cached_at(timestamp, method)
    assoc.touch_through_reflections(timestamp)
  end
end
update_relations_cached_at_from_cached_at(method: nil) click to toggle source
# File lib/cached_at/base.rb, line 39
def update_relations_cached_at_from_cached_at(method: nil)
  update_relations_cached_at(
    timestamp: (self.class.column_names.include?('cached_at') ? cached_at : nil),
    method: method
  )
end