Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

When _type field is altered attempt to correctly instantiate the clas…

…s. Closes #1390.
  • Loading branch information...
commit 33507e5ad5112032f5699e7ca608b328537f3f6c 1 parent 947bdbc
@durran durran authored
View
2  CHANGELOG.md
@@ -23,6 +23,8 @@ For instructions on upgrading to newer versions, visit
* \#1392 Return 0 on aggregation functions where field is nonexistant.
+* \#1390 When _type field is lower case class camelize before constantizing.
+
## 2.3.3
### Resolved Issues
View
6 lib/mongoid/factory.rb
@@ -36,7 +36,11 @@ def build(klass, attributes = {}, options = {})
# @return [ Document ] The instantiated document.
def from_db(klass, attributes = {})
type = attributes["_type"]
- type.blank? ? klass.instantiate(attributes) : type.constantize.instantiate(attributes)
+ if type.blank?
+ klass.instantiate(attributes)
+ else
+ type.camelize.constantize.instantiate(attributes)
+ end
end
end
end
View
104 spec/unit/mongoid/factory_spec.rb
@@ -4,49 +4,88 @@
describe ".build" do
- context "when the _type attribute is present" do
+ context "when the type attribute is present" do
- before do
- @attributes = { "_type" => "Person", "title" => "Sir" }
+ let(:attributes) do
+ { "_type" => "Person", "title" => "Sir" }
end
- it "instantiates based on the type" do
- person = Mongoid::Factory.build(Person, @attributes)
- person.title.should == "Sir"
+ context "when the type is a class" do
+
+ let(:person) do
+ described_class.build(Person, attributes)
+ end
+
+ it "instantiates based on the type" do
+ person.title.should eq("Sir")
+ end
end
- it "does not instantiate classes other than the given or its subclasses" do
- person = Mongoid::Factory.build(Person, { "_type" => "Canvas" })
- person.class.should == Person
+ context "when the type is a not a subclass" do
+
+ let(:person) do
+ described_class.build(Person, { "_type" => "Canvas" })
+ end
+
+ it "instantiates the provided class" do
+ person.class.should eq(Person)
+ end
end
- it "does instantiate subclasses of the given class" do
- person = Mongoid::Factory.build(Person, { "_type" => "Doctor" })
- person.class.should == Doctor
+ context "when the type is a subclass of the provided" do
+
+ let(:person) do
+ described_class.build(Person, { "_type" => "Doctor" })
+ end
+
+ it "instantiates the subclass" do
+ person.class.should eq(Doctor)
+ end
end
- end
- context "when _type is not preset" do
+ context "when type is an empty string" do
+
+ let(:attributes) do
+ { "title" => "Sir", "_type" => "" }
+ end
- before do
- @attributes = { "title" => "Sir" }
+ let(:person) do
+ described_class.build(Person, attributes)
+ end
+
+ it "instantiates based on the type" do
+ person.title.should eq("Sir")
+ end
end
- it "instantiates based on the type" do
- person = Mongoid::Factory.build(Person, @attributes)
- person.title.should == "Sir"
+ context "when type is the lower case class name" do
+
+ let(:attributes) do
+ { "title" => "Sir", "_type" => "person" }
+ end
+
+ let(:person) do
+ described_class.build(Person, attributes)
+ end
+
+ it "instantiates based on the type" do
+ person.title.should eq("Sir")
+ end
end
end
- context "when _type is an empty string" do
+ context "when type is not preset" do
+
+ let(:attributes) do
+ { "title" => "Sir" }
+ end
- before do
- @attributes = { "title" => "Sir", "_type" => "" }
+ let(:person) do
+ described_class.build(Person, attributes)
end
- it "instantiates based on the type" do
- person = Mongoid::Factory.build(Person, @attributes)
- person.title.should == "Sir"
+ it "instantiates based on the provided class" do
+ person.title.should eq("Sir")
end
end
end
@@ -92,6 +131,21 @@
document.title.should == "Sir"
end
end
+
+ context "when type is the lower case class name" do
+
+ let(:attributes) do
+ { "title" => "Sir", "_type" => "person" }
+ end
+
+ let(:person) do
+ described_class.from_db(Person, attributes)
+ end
+
+ it "instantiates based on the type" do
+ person.title.should eq("Sir")
+ end
+ end
end
context "when a type is not in the attributes" do
Please sign in to comment.
Something went wrong with that request. Please try again.