Module: CachedAt::HasManyThroughAssociation
- Defined in:
- lib/cached_at/associations/has_many_through_association.rb
Instance Method Summary collapse
Instance Method Details
#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 32 33 34 35 36 37 38 39 40 |
# File 'lib/cached_at/associations/has_many_through_association.rb', line 4 def touch_cached_at(, method) using_reflection = reflection.parent_reflection || reflection if using_reflection.[:cached_at] return if method == :create && !using_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection) if using_reflection.inverse_of.nil? puts "WARNING: cannot updated cached at for relationship: #{owner.class.name}.#{using_reflection.name}, inverse_of not set" return end cache_column = "#{using_reflection.inverse_of.name}_cached_at" query = nil if loaded? target.each { |r| r.raw_write_attribute(cache_column, ) } query = klass.where(klass.primary_key => target.map(&:id)) else ids = [owner.send(using_reflection.association_primary_key), owner.send("#{using_reflection.association_primary_key}_was")].compact.uniq arel_table = klass._reflections[using_reflection.inverse_of.[:through].to_s].klass.arel_table query = klass.joins(using_reflection.inverse_of.[:through]) query = if using_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection) query.where(arel_table[using_reflection.foreign_key].in(ids)) else query.where(arel_table[using_reflection.inverse_of.foreign_key].in(ids)) end end query.update_all({ cache_column => }) traverse_relationships(klass, [:cached_at], query, cache_column, ) end if using_reflection.inverse_of && using_reflection.inverse_of.[:cached_at] cache_column = "#{using_reflection.name}_cached_at" owner.raw_write_attribute(cache_column, ) owner.update_columns(cache_column => ) end end |
#touch_records_cached_at(records, timestamp) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cached_at/associations/has_many_through_association.rb', line 42 def touch_records_cached_at(records, ) using_reflection = reflection.parent_reflection || reflection if using_reflection.[:cached_at] if using_reflection.inverse_of.nil? puts "WARNING: cannot updated cached at for relationship: #{klass.name}.#{name}, inverse_of not set" return end cache_column = "#{using_reflection.inverse_of.name}_cached_at" records.each { |r| r.raw_write_attribute(cache_column, ) } query = klass.where(klass.primary_key => records.map(&:id)) query.update_all({ cache_column => }) traverse_relationships(klass, using_reflection.[:cached_at], query, cache_column, ) end if using_reflection.inverse_of&..try(:[], :cached_at) || using_reflection.inverse_of&.parent_reflection&..try(:[], :cached_at) cache_column = "#{using_reflection.name}_cached_at" owner.update_column(cache_column, ) unless owner.new_record? end end |