Module: CachedAt::HasOneAssociation

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

Instance Method Summary collapse

Instance Method Details

#delete(method = ) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cached_at/associations/has_one_association.rb', line 31

def delete(method = options[:dependent])
  if load_target
    case method
    when :delete
      target.delete
    when :destroy
      target.destroy
    when :nullify
      updates = {reflection.foreign_key => nil}
      if reflection.options[:cached_at]
        cache_column = "#{reflection.inverse_of.name}_cached_at"
        updates[cache_column] = Time.now
      end
      target.update_columns(updates) if target.persisted?
    end
  end
end

#touch_cached_at(timestamp, method) ⇒ 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
# File 'lib/cached_at/associations/has_one_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}_was")].compact.uniq
  query = klass.where({ reflection.foreign_key => ids })
  
  case options[:dependent]
  when nil
    query.update_all({ cache_column => timestamp })
    traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
  when :destroy
    # don't need to worry about :destroy, that will touch the other caches
  when :delete, :nullify
    traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
  end
  
  if loaded? && target
    target.raw_write_attribute(cache_column, timestamp)
  end
end