Module: CachedAt::CollectionAssociation
- Defined in:
- lib/cached_at/associations/collection_association.rb
Instance Method Summary collapse
- #add_to_target(record, skip_callbacks = false, &block) ⇒ Object
- #delete_all(dependent = nil) ⇒ Object
- #replace_records(new_target, original_target) ⇒ Object
- #touch_cached_at(timestamp, method) ⇒ Object
- #touch_records_cached_at(records, timestamp) ⇒ Object
Instance Method Details
#add_to_target(record, skip_callbacks = false, &block) ⇒ Object
50 51 52 53 54 |
# File 'lib/cached_at/associations/collection_association.rb', line 50 def add_to_target(record, skip_callbacks = false, &block) value = super touch_records_cached_at([record], Time.now) if !(instance_variable_defined?(:@caching) && @caching) value end |
#delete_all(dependent = nil) ⇒ Object
66 67 68 69 |
# File 'lib/cached_at/associations/collection_association.rb', line 66 def delete_all(dependent = nil) touch_cached_at(Time.now, :destroy) super end |
#replace_records(new_target, original_target) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/cached_at/associations/collection_association.rb', line 56 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) ⇒ 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 |
# File 'lib/cached_at/associations/collection_association.rb', line 4 def touch_cached_at(, method) return unless [: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 }) if loaded? target.each { |r| r.raw_write_attribute(cache_column, ) } end if method != :destroy query.update_all({ cache_column => }) traverse_relationships(klass, [:cached_at], query, cache_column, ) else if [:dependent].nil? query.update_all({ cache_column => }) traverse_relationships(klass, [:cached_at], query, cache_column, ) else traverse_relationships(klass, [:cached_at], query, cache_column, ) end end end |
#touch_records_cached_at(records, timestamp) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cached_at/associations/collection_association.rb', line 33 def touch_records_cached_at(records, ) return unless [: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.raw_write_attribute(cache_column, ) unless r.destroyed? } query = klass.where({ klass.primary_key => records.map(&:id) }) query.update_all({ cache_column => }) traverse_relationships(klass, [:cached_at], query, cache_column, ) end |