Permalink
Browse files

return the resource with the same persistence in cache

  • Loading branch information...
samsonw committed Apr 9, 2012
1 parent 11e743a commit 7f387657b80f566753e63b7f5014a13006c18dcc
Showing with 11 additions and 3 deletions.
  1. +4 −2 lib/cached_resource/caching.rb
  2. +7 −1 spec/cached_resource/caching_spec.rb
@@ -81,7 +81,9 @@ def is_collection?(*arguments)
# The key is processed to make sure it is valid.
def cache_read(key)
key = cache_key(Array(key)) unless key.is_a? String
- object = cached_resource.cache.read(key).try(:dup)
+ object = cached_resource.cache.read(key).try do |cache|
+ cache.dup.tap { |o| o.instance_variable_set(:@persisted, cache.persisted?) if cache.respond_to?(:persisted?)}
+ end
object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key}")
object
end
@@ -102,4 +104,4 @@ def cache_key(*arguments)
end
end
-end
+end
@@ -40,6 +40,12 @@ class Thing < ActiveResource::Base
Thing.cached_resource.cache.read("thing/1").should == result
end
+ it "should cache a response with the same persistence" do
+ result1 = Thing.find(1)
+ result2 = Thing.find(1)
+ result1.persisted?.should == result2.persisted?
+ end
+
it "should read a response when the request is made again" do
# make a request
Thing.find(1)
@@ -266,4 +272,4 @@ class Thing < ActiveResource::Base
new_result.name.should_not == old_result.name
end
end
-end
+end

0 comments on commit 7f38765

Please sign in to comment.