Module: CachedAt::Association

Defined in:
lib/cached_at/associations/association.rb

Instance Method Summary collapse

Instance Method Details

#touch_through_reflections(timestamp) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cached_at/associations/association.rb', line 38

def touch_through_reflections(timestamp)
  reflection.through_relationship_endpoints.each do |r|
    cache_column = "#{r.inverse_of.name}_cached_at"
    
    source_assoc = owner.association(r.source_reflection_name.to_sym)
    if source_assoc.loaded?
      source_assoc.target.raw_write_attribute(cache_column, timestamp)
    end
    query = r.klass.where(r.association_primary_key => owner.send(r.foreign_key))
    query.update_all({ cache_column => timestamp })
    traverse_relationships(r.klass, r.options[:cached_at], query, cache_column, timestamp)
  end
end

#traverse_relationships(klass, relationships, query, cache_column, timestamp) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cached_at/associations/association.rb', line 4

def traverse_relationships(klass, relationships, query, cache_column, timestamp)
  if relationships.is_a?(Symbol)
    reflection = klass.reflect_on_association(relationships)
    case reflection
    when ActiveRecord::Reflection::BelongsToReflection
      cache_column = "#{reflection.inverse_of.name}_#{cache_column}"
      reflection.klass.joins(reflection.inverse_of.name).merge(query).update_all({
        cache_column => timestamp
      })
    when ActiveRecord::Reflection::HasManyReflection
      cache_column = "#{reflection.inverse_of.name}_#{cache_column}"
      reflection.klass.joins(reflection.inverse_of.name).merge(query).update_all({
        cache_column => timestamp
      })
    when ActiveRecord::Reflection::HasAndBelongsToManyReflection
      query = reflection.klass.joins(reflection.inverse_of.name).merge(query)
      puts '!!!!!!!!!!!!!!!!!'
    when ActiveRecord::Reflection::ThroughReflection
      cache_column = "#{reflection.inverse_of.name}_#{cache_column}"
      reflection.klass.joins(reflection.inverse_of.name).merge(query).update_all({
        cache_column => timestamp
      })
    end
  elsif relationships.is_a?(Hash)
    relationships.each do |key, value|
      traverse_relationships(klass, key, query, cache_column, timestamp)
    end
  elsif relationships.is_a?(Array)
    relationships.each do |value|
      traverse_relationships(klass, value, query, cache_column, timestamp)
    end
  end
end