Skip to content

Fix for caching referenced documents belonging to a polymorphic collection #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
1.x
---

* 2012/03/21: Fixed a bug in caching/invalidating referenced polymorphic documents, [Frank Macreery](http://github.com/macreery)
* 2012/02/29: Added support for versioning, [Daniel Doubrovkine](http://github.com/dblock)

1.0
3 changes: 2 additions & 1 deletion lib/mongoid-cached-json/cached_json.rb
Original file line number Diff line number Diff line change
@@ -93,7 +93,8 @@ def materialize_json(options, object_def)

# Cache key.
def cached_json_key(options, cached_class, cached_id)
"as_json/#{options[:version]}/#{cached_class}/#{cached_id}/#{options[:properties]}/#{!!options[:is_top_level_json]}"
base_class_name = cached_class.collection_name.singularize.camelize
"as_json/#{options[:version]}/#{base_class_name}/#{cached_id}/#{options[:properties]}/#{!!options[:is_top_level_json]}"
end

# If the reference is a symbol, we may be lucky and be able to figure out the as_json
31 changes: 31 additions & 0 deletions spec/cached_json_spec.rb
Original file line number Diff line number Diff line change
@@ -320,5 +320,36 @@
person.as_json({ :version => :v3 }).should == { :first => "John", :middle => "F.", :last => "Kennedy", :name => "John F. Kennedy", :born => "May 29, 1917" }
end
end
context "polymorphic objects" do
before(:each) do
@json_embedded_foobar = JsonEmbeddedFoobar.new(:foo => "embedded")
@json_referenced_foobar = JsonReferencedFoobar.new(:foo => "referenced")
@json_parent_foobar = JsonParentFoobar.create({
:json_polymorphic_embedded_foobar => @json_embedded_foobar,
:json_polymorphic_referenced_foobar => @json_referenced_foobar
})
@json_referenced_foobar.json_parent_foobar = @json_parent_foobar
@json_referenced_foobar.save!

# Cache...
[:all, :short, :public].each do |prop|
@json_parent_foobar.as_json(:properties => prop)
end
end
it "returns correct JSON when a child (embedded) polymorphic document is changed" do
@json_parent_foobar.as_json(:properties => :all)[:json_polymorphic_embedded_foobar][:foo].should == "embedded"
@json_embedded_foobar.as_json(:properties => :all)[:foo].should == "embedded"
@json_embedded_foobar.update_attributes!(:foo => "EMBEDDED")
@json_embedded_foobar.as_json(:properties => :all)[:foo].should == "EMBEDDED"
@json_parent_foobar.as_json(:properties => :all)[:json_polymorphic_embedded_foobar][:foo].should == "EMBEDDED"
end
it "returns correct JSON when a child (referenced) polymorphic document is changed" do
@json_parent_foobar.as_json(:properties => :all)[:json_polymorphic_referenced_foobar][:foo].should == "referenced"
@json_referenced_foobar.as_json(:properties => :all)[:foo].should == "referenced"
@json_referenced_foobar.update_attributes!(:foo => "REFERENCED")
@json_referenced_foobar.as_json(:properties => :all)[:foo].should == "REFERENCED"
@json_parent_foobar.as_json(:properties => :all)[:json_polymorphic_referenced_foobar][:foo].should == "REFERENCED"
end
end
end

4 changes: 4 additions & 0 deletions spec/support/json_embedded_foobar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require File.join(File.dirname(__FILE__), "json_polymorphic_embedded_foobar")

class JsonEmbeddedFoobar < JsonPolymorphicEmbeddedFoobar
end
12 changes: 12 additions & 0 deletions spec/support/json_parent_foobar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class JsonParentFoobar
include Mongoid::Document
include Mongoid::CachedJson

belongs_to :json_polymorphic_referenced_foobar
embeds_one :json_polymorphic_embedded_foobar

json_fields \
:json_polymorphic_referenced_foobar => { :type => :reference },
:json_polymorphic_embedded_foobar => { :type => :reference }

end
10 changes: 10 additions & 0 deletions spec/support/json_polymorphic_embedded_foobar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class JsonPolymorphicEmbeddedFoobar
include Mongoid::Document
include Mongoid::CachedJson

embedded_in :json_parent_foobar

json_fields \
:foo => { :properties => :short }

end
10 changes: 10 additions & 0 deletions spec/support/json_polymorphic_referenced_foobar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class JsonPolymorphicReferencedFoobar
include Mongoid::Document
include Mongoid::CachedJson

has_one :json_parent_foobar

json_fields \
:foo => { :properties => :short }

end
4 changes: 4 additions & 0 deletions spec/support/json_referenced_foobar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require File.join(File.dirname(__FILE__), "json_polymorphic_referenced_foobar")

class JsonReferencedFoobar < JsonPolymorphicReferencedFoobar
end