Skip to content

Commit

Permalink
Remove resource_id because its convered by active resources primary key
Browse files Browse the repository at this point in the history
Oops!

Signed-off-by: David Souza <david@souza.net>
  • Loading branch information
Morgan Brown authored and davidsouza committed Apr 5, 2012
1 parent 9e2fd04 commit 3336ac7
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def cache_collection_synchronize(object, *arguments)
return unless cached_resource.collection_synchronize

if arguments.length == 1 && arguments[0] == :all
object.each {|r| cache_write(cached_resource.get_resource_id(r), r)}
object.each {|r| cache_write(r.send(primary_key), r)}
elsif !arguments.include?(:all) && (collection = cache_read(:all))
collection.each_with_index {|member, i| collection[i] = object if cached_resource.get_resource_id(member) == cached_resource.get_resource_id(object)}
collection.each_with_index {|member, i| collection[i] = object if member.send(primary_key) == object.send(primary_key)}
cache_write(:all, collection)
end
end
Expand Down
9 changes: 0 additions & 9 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,19 @@ class Configuration < OpenStruct
# defaults. The following options exist for cached resource:
# :enabled, default: true
# :ttl, default: 604800
# :resource_id, default: id
# :collection_synchronize, default: false
# :cache, default: Rails.cache or ActiveSupport::Cache::MemoryStore.new,
# :logger, default: Rails.logger or ActiveSupport::BufferedLogger.new(NilIO.new)
def initialize(options={})
super({
:enabled => true,
:ttl => 604800,
:resource_id => :id,
:collection_synchronize => false,
:cache => defined?(Rails.cache) && Rails.cache || CACHE,
:logger => defined?(Rails.logger) && Rails.logger || LOGGER
}.merge(options))
end

# Get the resource id of the given object.
# This should be the url component that
# represents a specific resource.
def get_resource_id(object)
self.resource_id.to_proc.call(object)
end

# Enables caching.
def on!
self.enabled = true
Expand Down
18 changes: 1 addition & 17 deletions spec/cached_resource/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
configuration.ttl.should == 604800
end

it "should have an identifier of id" do
configuration.resource_id.should == :id
end

it "should disable collection synchronization" do
configuration.collection_synchronize.should == false
end
Expand Down Expand Up @@ -59,7 +55,7 @@
describe "when initialized through cached resource" do
before(:each) do
class Foo < ActiveResource::Base
cached_resource :ttl => 1, :cache => "cache", :logger => "logger", :enabled => false, :resource_id => :ugly, :collection_synchronize => true, :custom => "irrelevant"
cached_resource :ttl => 1, :cache => "cache", :logger => "logger", :enabled => false, :collection_synchronize => true, :custom => "irrelevant"
end
end

Expand All @@ -72,21 +68,9 @@ class Foo < ActiveResource::Base
Foo.cached_resource.cache.should == "cache"
Foo.cached_resource.logger.should == "logger"
Foo.cached_resource.enabled.should == false
Foo.cached_resource.resource_id.should == :ugly
Foo.cached_resource.collection_synchronize.should == true
Foo.cached_resource.custom.should == "irrelevant"
end

describe "when the id is callable" do
before(:each) do
Foo.cached_resource.resource_id = lambda {|obj| obj + "!"}
end

it "should return an id transformed by the call" do
Foo.cached_resource.get_resource_id("hello").should == "hello!"
end

end
end

describe "when multiple are initialized through cached resource" do
Expand Down

0 comments on commit 3336ac7

Please sign in to comment.