From e94e97ca796c0759d8fcb8f946a3bbc60252d329 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 2 Nov 2013 16:05:19 -0700 Subject: Extend ActiveRecord::Base#cache_key to take an optional list of timestamp attributes of which the highest will be used. --- activerecord/lib/active_record/integration.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/integration.rb') diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index 2589b2f3da..6449789fe4 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -45,10 +45,18 @@ module ActiveRecord # Product.new.cache_key # => "products/new" # Product.find(5).cache_key # => "products/5" (updated_at not available) # Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available) - def cache_key + # + # You can also pass a list of named timestamps, and the newest in the list will be + # used to generate the key: + # + # Person.find(5).cache_key(:updated_at, :last_reviewed_at) + def cache_key(*timestamp_names) case when new_record? "#{self.class.model_name.cache_key}/new" + when timestamp_names.any? + timestamps = timestamp_names.collect { |method| send(method) }.compact + "#{self.class.model_name.cache_key}/#{id}-#{timestamps.max.utc.to_s(:number)}" when timestamp = max_updated_column_timestamp timestamp = timestamp.utc.to_s(cache_timestamp_format) "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" -- cgit v1.2.3