Module: CachedAt::CollectionAssociation
- Defined in:
- lib/cached_at/associations/collection_association.rb
Instance Method Summary collapse
- #concat_records(records, should_raise = false) ⇒ Object
- #delete_all(dependent = nil) ⇒ Object
- #remove_records(existing_records, records, method) ⇒ Object
- #touch_cached_at(timestamp, method) ⇒ Object
- #touch_records_added_cached_at(records, timestamp) ⇒ Object
- #touch_records_removed_cached_at(records, timestamp) ⇒ Object
Instance Method Details
#concat_records(records, should_raise = false) ⇒ Object
70 71 72 73 74 |
# File 'lib/cached_at/associations/collection_association.rb', line 70 def concat_records(records, should_raise = false) value = super touch_records_added_cached_at(records, Time.now) value end |
#delete_all(dependent = nil) ⇒ Object
81 82 83 84 |
# File 'lib/cached_at/associations/collection_association.rb', line 81 def delete_all(dependent = nil) touch_cached_at(Time.now, :destroy) super end |
#remove_records(existing_records, records, method) ⇒ Object
76 77 78 79 |
# File 'lib/cached_at/associations/collection_association.rb', line 76 def remove_records(existing_records, records, method) touch_records_removed_cached_at(existing_records, Time.now) super 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_added_cached_at(records, timestamp) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cached_at/associations/collection_association.rb', line 33 def touch_records_added_cached_at(records, ) return if owner.new_record? || records.empty? if reflection.[:cached_at] if reflection.inverse_of.nil? puts "WARNING: cannot updated cached at for relationship: #{klass.name}.#{name}, inverse_of not set" return end cache_column = "#{reflection.inverse_of.name}_cached_at" if loaded? target.each { |r| r.raw_write_attribute(cache_column, ) } end ids = records.inject([]) { |a, o| a += [o.send(klass.primary_key), o.send("#{klass.primary_key}_was")] }.compact.uniq query = klass.where(klass.primary_key => ids) query.update_all({ cache_column => }) traverse_relationships(klass, reflection.[:cached_at], query, cache_column, ) end end |
#touch_records_removed_cached_at(records, timestamp) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cached_at/associations/collection_association.rb', line 54 def touch_records_removed_cached_at(records, ) return if owner.new_record? || records.empty? return unless [:cached_at] if reflection.inverse_of.nil? puts "WARNING: cannot updated cached at for relationship: #{klass.name}.#{name}, inverse_of not set" return end cache_column = "#{reflection.inverse_of.name}_cached_at" ids = records.inject([]) { |a, o| a += [o.send(klass.primary_key), o.send("#{klass.primary_key}_was")] }.compact.uniq query = klass.where(klass.primary_key => ids) traverse_relationships(klass, reflection.[:cached_at], query, cache_column, ) end |