module CachedAt::CollectionAssociation

Public Instance Methods

add_to_target(record, skip_callbacks: false, replace: false, &block) click to toggle source
Calls superclass method
# File lib/cached_at/associations/collection_association.rb, line 52
def add_to_target(record, skip_callbacks: false, replace: false, &block)
  value = super
  touch_records_cached_at([record], Time.now) if !(instance_variable_defined?(:@caching) && @caching)
  value
end
delete_all(dependent = nil) click to toggle source
Calls superclass method
# File lib/cached_at/associations/collection_association.rb, line 68
def delete_all(dependent = nil)
  touch_cached_at(Time.now, :destroy)
  super
end
replace_records(new_target, original_target) click to toggle source
Calls superclass method
# File lib/cached_at/associations/collection_association.rb, line 58
def replace_records(new_target, original_target)
  @caching = true
  changed_records = (target - new_target) | (new_target - target)
  value = super
  touch_records_cached_at(changed_records, Time.now) unless owner.new_record?
  value
ensure
  @caching = false
end
touch_cached_at(timestamp, method) click to toggle source
# File lib/cached_at/associations/collection_association.rb, line 4
def touch_cached_at(timestamp, method)
  return unless options[:cached_at]

  if reflection.inverse_of.nil?
    puts "WARNING: cannot updated cached at for relationship: #{owner.class.name}.#{name}, inverse_of not set"
    return
  end

  cache_column = "#{reflection.inverse_of.name}_cached_at"
  ids = [owner.send(reflection.association_primary_key), owner.send("#{reflection.association_primary_key}_before_last_save")].compact.uniq
  query = klass.where({ reflection.foreign_key => ids })
  
  if loaded?
    target.each do |record|
      record.send(:write_attribute_without_type_cast, cache_column, timestamp)
    end
  end
  
  if method != :destroy
    query.update_all({ cache_column => timestamp })
    traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
  else
    if options[:dependent].nil?
      query.update_all({ cache_column => timestamp })
      traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
    else
      traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
    end
  end
end
touch_records_cached_at(records, timestamp) click to toggle source
# File lib/cached_at/associations/collection_association.rb, line 35
def touch_records_cached_at(records, timestamp)
  return unless options[:cached_at]

  if reflection.inverse_of.nil?
    puts "WARNING: cannot updated cached at for relationship: #{owner.class.name}.#{name}, inverse_of not set"
    return
  end

  cache_column = "#{reflection.inverse_of.name}_cached_at"

  records.each { |r| r.send(:write_attribute_without_type_cast, cache_column, timestamp) unless r.destroyed? }

  query = klass.where({ klass.primary_key => records.map(&:id) })
  query.update_all({ cache_column => timestamp })
  traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
end