aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2011-12-01 19:47:14 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2011-12-01 19:47:14 +0100
commit535853e83b9092078035a5abb2aa242fba815c05 (patch)
treede6348b219aaf625b2c0c651e4b5ff822c883a6e
parent218c2729384be487b7b743a58ac39753cb5a8856 (diff)
downloadrails-535853e83b9092078035a5abb2aa242fba815c05.tar.gz
rails-535853e83b9092078035a5abb2aa242fba815c05.tar.bz2
rails-535853e83b9092078035a5abb2aa242fba815c05.zip
Added ActiveRecord::Base#last_modified to work with the new fresh_when/stale? conditional get methods from Action Pack
-rw-r--r--actionpack/lib/action_controller/metal/conditional_get.rb4
-rw-r--r--actionpack/test/controller/render_test.rb4
-rw-r--r--activerecord/CHANGELOG.md1
-rw-r--r--activerecord/lib/active_record/base.rb7
4 files changed, 12 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/metal/conditional_get.rb b/actionpack/lib/action_controller/metal/conditional_get.rb
index 1645400693..a6747fca7c 100644
--- a/actionpack/lib/action_controller/metal/conditional_get.rb
+++ b/actionpack/lib/action_controller/metal/conditional_get.rb
@@ -23,7 +23,7 @@ module ActionController
# This will render the show template if the request isn't sending a matching etag or
# If-Modified-Since header and just a <tt>304 Not Modified</tt> response if there's a match.
#
- # You can also just pass a record where last_modified will be set by calling updated_at and the etag by passing the object itself. Example:
+ # You can also just pass a record where last_modified will be set by calling last_modified and the etag by passing the object itself. Example:
#
# def show
# @article = Article.find(params[:id])
@@ -42,7 +42,7 @@ module ActionController
options.assert_valid_keys(:etag, :last_modified, :public)
else
record = record_or_options
- options = { :etag => record, :last_modified => record.try(:updated_at) }.merge(additional_options)
+ options = { :etag => record, :last_modified => record.try(:last_modified) }.merge(additional_options)
end
response.etag = options[:etag] if options[:etag]
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index 243bad8749..e014d08ec9 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -51,7 +51,7 @@ class TestController < ActionController::Base
end
def conditional_hello_with_record
- record = Struct.new(:updated_at, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
+ record = Struct.new(:last_modified, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
if stale?(record)
render :action => 'hello_world'
@@ -65,7 +65,7 @@ class TestController < ActionController::Base
end
def conditional_hello_with_public_header_with_record
- record = Struct.new(:updated_at, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
+ record = Struct.new(:last_modified, :cache_key).new(Time.now.utc.beginning_of_day, "foo/123")
if stale?(record, :public => true)
render :action => 'hello_world'
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f798b03ea1..d39418cd87 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,6 @@
## Rails 3.2.0 (unreleased) ##
+* Added ActiveRecord::Base#last_modified to work with the new fresh_when/stale? conditional get methods from Action Pack *DHH*
* Implemented ActiveRecord::Relation#pluck method
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 76aa121ade..bae2dc738e 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1702,6 +1702,13 @@ MSG
end
end
+ # By default returns the value of the updated_at attribute, but can be overwritten to
+ # provide another indicator of when this record was last updated. This is used by
+ # ActionControllers conditional get fresh_when/stale? methods.
+ def last_modified
+ self[:updated_at]
+ end
+
def quoted_id #:nodoc:
quote_value(id, column_for_attribute(self.class.primary_key))
end