Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only invoke identities that exist.

  • Loading branch information...
commit f755802cf35fb2028a702ffd230e51ba74378dca 1 parent df46c13
@dblock dblock authored
View
1  lib/garner/mixins/mongoid_document.rb
@@ -15,6 +15,7 @@ def invalidate_api_cache
self.all_embedding_documents.each { |doc| doc.invalidate_api_cache }
cache_class = self.class.api_cache_class || self.class
Garner::Cache::ObjectIdentity::IDENTITY_FIELDS.each do |identity_field|
+ next unless self.respond_to?(identity_field)
Garner::Cache::ObjectIdentity.invalidate(cache_class, { identity_field => self.send(identity_field) })
end
Garner::Cache::ObjectIdentity.invalidate(cache_class)
View
72 spec/garner/mixins/mongoid_document_spec.rb
@@ -2,16 +2,7 @@
describe Garner::Mixins::Mongoid::Document do
describe "all_embedding_documents" do
- before(:each) do
- class Foo
- include Mongoid::Document
- embeds_one :bars
- embedded_in :bar
- end
- class Bar
- include Mongoid::Document
- embedded_in :foo
- end
+ before :each do
@foo = Foo.new
@bar = Bar.new
@foo.bar = @bar
@@ -21,36 +12,47 @@ class Bar
end
end
describe "api_cache_class" do
- before(:each) do
- class TestModel
- def self.before_save(* args)
- end
- def self.before_destroy(* args)
- end
- attr_accessor :id
- include Garner::Mixins::Mongoid::Document
- end
- class OtherModel ; end
- end
it "is used by :invalidate_api_cache" do
- @test = TestModel.new
- @test.stub(:metadata) { nil }
- @test.id = 42
+ t = TestModel.new
+ t.stub(:metadata).and_return(nil)
Garner::Cache::ObjectIdentity.stub(:invalidate).as_null_object
- Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModel, { :id => 42 })
- @test.invalidate_api_cache
+ Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModel, { :id => t.id })
+ t.invalidate_api_cache
end
it "allows for an override" do
- class TestModel
- cache_as OtherModel
- end
- TestModel.api_cache_class.should == OtherModel
- @test = TestModel.new
- @test.stub(:metadata) { nil }
- @test.id = 42
+ TestModelChild.api_cache_class.should == TestModel
+ t = TestModelChild.new
+ t.stub(:metadata).and_return(nil)
Garner::Cache::ObjectIdentity.stub(:invalidate).as_null_object
- Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(OtherModel, { :id => 42 })
- @test.invalidate_api_cache
+ Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModel, { :id => t.id })
+ t.invalidate_api_cache
+ end
+ context "with mutliple identity fields" do
+ before :each do
+ silence_warnings do
+ Garner::Cache::ObjectIdentity::IDENTITY_FIELDS = [ :slug, :id ]
+ end
+ end
+ after :each do
+ silence_warnings do
+ Garner::Cache::ObjectIdentity::IDENTITY_FIELDS = [ :id ]
+ end
+ end
+ it "invalidates only identity fields that exist" do
+ t = TestModel.new
+ t.stub(:metadata).and_return(nil)
+ Garner::Cache::ObjectIdentity.stub(:invalidate).as_null_object
+ Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModel, { :id => t.id })
+ t.invalidate_api_cache
+ end
+ it "invalidates all identity fields" do
+ t = TestModelWithSlug.new({ :slug => "forty-two" })
+ t.stub(:metadata).and_return(nil)
+ Garner::Cache::ObjectIdentity.stub(:invalidate).as_null_object
+ Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModelWithSlug, { :id => t.id })
+ Garner::Cache::ObjectIdentity.should_receive(:invalidate).with(TestModelWithSlug, { :slug => "forty-two" })
+ t.invalidate_api_cache
+ end
end
end
end
View
5 spec/support/bar.rb
@@ -0,0 +1,5 @@
+class Bar
+ include Mongoid::Document
+ include Garner::Mixins::Mongoid::Document
+ embedded_in :foo
+end
View
6 spec/support/foo.rb
@@ -0,0 +1,6 @@
+class Foo
+ include Mongoid::Document
+ include Garner::Mixins::Mongoid::Document
+ embeds_one :bars
+ embedded_in :bar
+end
View
4 spec/support/test_model.rb
@@ -0,0 +1,4 @@
+class TestModel
+ include Mongoid::Document
+ include Garner::Mixins::Mongoid::Document
+end
View
5 spec/support/test_model_child.rb
@@ -0,0 +1,5 @@
+class TestModelChild
+ include Mongoid::Document
+ include Garner::Mixins::Mongoid::Document
+ cache_as TestModel
+end
View
6 spec/support/test_model_with_slug.rb
@@ -0,0 +1,6 @@
+class TestModelWithSlug
+ include Mongoid::Document
+ include Garner::Mixins::Mongoid::Document
+
+ field :slug, :type => String
+end
Please sign in to comment.
Something went wrong with that request. Please try again.