aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2013-11-02 16:05:19 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2013-11-02 16:05:19 -0700
commite94e97ca796c0759d8fcb8f946a3bbc60252d329 (patch)
tree770bd9523e2f18e2e6bb698956f03ac2ca225889 /activerecord/lib
parent6d30219c2734f2678d74a54b791a311748b3e64f (diff)
downloadrails-e94e97ca796c0759d8fcb8f946a3bbc60252d329.tar.gz
rails-e94e97ca796c0759d8fcb8f946a3bbc60252d329.tar.bz2
rails-e94e97ca796c0759d8fcb8f946a3bbc60252d329.zip
Extend ActiveRecord::Base#cache_key to take an optional list of timestamp attributes of which the highest will be used.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/integration.rb10
1 files changed, 9 insertions, 1 deletions
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}"