Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Proper separation of mongoization/evolution w/ fks:

- This is split up now, so that empty strings and nils aren't filtered
  out of criteria params on these fields.
- Fixes #2089.
  • Loading branch information...
commit ba41bcf53b4f1909c2bc95900e3976521df64197 1 parent 2187289
@durran durran authored
View
10 CHANGELOG.md
@@ -529,6 +529,13 @@ For instructions on upgrading to newer versions, visit
### Major Changes (Backwards Incompatible)
+* Polymorphic relations can not have ids other than object ids. This is
+ because Mongoid cannot properly figure out in an optimized way what the
+ various classes on the other side of the relation store their ids as, as
+ they could potentially all be different.
+
+ This was not allowed before, but nowhere was it explicitly stated.
+
* \#2039 Validating presence of a relation now checks both the relation and
the foreign key value.
@@ -771,6 +778,9 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#2089 Allow proper separation of mongoization and evolving with respect to
+ foreign keys.
+
* \#2085 Allow demongoization of floats and ints to big decimals.
* \#2084 Don't cascade if metadata does not exist.
View
1  lib/mongoid.rb
@@ -46,6 +46,7 @@
require "mongoid/copyable"
require "mongoid/criteria"
require "mongoid/dirty"
+require "mongoid/evolvable"
require "mongoid/factory"
require "mongoid/fields"
require "mongoid/finders"
View
1  lib/mongoid/components.rb
@@ -21,6 +21,7 @@ module Components
include Mongoid::Atomic
include Mongoid::Dirty
include Mongoid::Attributes
+ include Mongoid::Evolvable
include Mongoid::Fields
include Mongoid::Hierarchy
include Mongoid::Indexes
View
19 lib/mongoid/evolvable.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+module Mongoid
+
+ # Contains behaviour specific to evolving for origin queries.
+ module Evolvable
+
+ # Evolve the document into an object id.
+ #
+ # @example Evolve the document.
+ # document.__evolve_object_id__
+ #
+ # @return [ Object ] The document's id.
+ #
+ # @since 3.0.0
+ def __evolve_object_id__
+ id
+ end
+ end
+end
View
15 lib/mongoid/extensions/array.rb
@@ -12,7 +12,20 @@ module Array
#
# @since 3.0.0
def __evolve_object_id__
- map!(&:__evolve_object_id__).compact!
+ map!(&:__evolve_object_id__)
+ self
+ end
+
+ # Mongoize the array into an array of object ids.
+ #
+ # @example Evolve the array to object ids.
+ # [ id ].__mongoize_object_id__
+ #
+ # @return [ Array<Moped::BSON::ObjectId> ] The converted array.
+ #
+ # @since 3.0.0
+ def __mongoize_object_id__
+ map!(&:__mongoize_object_id__).compact!
self
end
View
12 lib/mongoid/extensions/hash.rb
@@ -15,6 +15,18 @@ def __evolve_object_id__
update_values(&:__evolve_object_id__)
end
+ # Mongoizes each value in the hash to an object id if it is convertable.
+ #
+ # @example Convert the hash values.
+ # { field: id }.__mongoize_object_id__
+ #
+ # @return [ Hash ] The converted hash.
+ #
+ # @since 3.0.0
+ def __mongoize_object_id__
+ update_values(&:__mongoize_object_id__)
+ end
+
# Get the id attribute from this hash, whether it's prefixed with an
# underscore or is a symbol.
#
View
1  lib/mongoid/extensions/object.rb
@@ -14,6 +14,7 @@ module Object
def __evolve_object_id__
self
end
+ alias :__mongoize_object_id__ :__evolve_object_id__
# Mongoize a plain object into a time.
#
View
3  lib/mongoid/extensions/object_id.rb
@@ -14,6 +14,7 @@ module ObjectId
def __evolve_object_id__
self
end
+ alias :__mongoize_object_id__ :__evolve_object_id__
module ClassMethods
@@ -42,7 +43,7 @@ def evolve(object)
#
# @since 3.0.0
def mongoize(object)
- evolve(object)
+ object.__mongoize_object_id__
end
end
end
View
34 lib/mongoid/extensions/string.rb
@@ -11,13 +11,23 @@ module String
# @example Evolve the string.
# "test".__evolve_object_id__
#
- # @return [ String, Moped::BSON::ObjectId, nil ] The evolved string.
+ # @return [ String, Moped::BSON::ObjectId ] The evolved string.
#
# @since 3.0.0
def __evolve_object_id__
- unless blank?
- BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
- end
+ convert_to_object_id
+ end
+
+ # Mongoize the string into an object id if possible.
+ #
+ # @example Evolve the string.
+ # "test".__mongoize_object_id__
+ #
+ # @return [ String, Moped::BSON::ObjectId, nil ] The mongoized string.
+ #
+ # @since 3.0.0
+ def __mongoize_object_id__
+ convert_to_object_id unless blank?
end
# Mongoize the string for storage.
@@ -116,6 +126,22 @@ def unconvertable_to_bson?
@unconvertable_to_bson ||= false
end
+ private
+
+ # If the string is a legal object id, convert it.
+ #
+ # @api private
+ #
+ # @example Convert to the object id.
+ # string.convert_to_object_id
+ #
+ # @return [ String, BSON::ObjectId ] The string or the id.
+ #
+ # @since 3.0.0
+ def convert_to_object_id
+ BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
+ end
+
module ClassMethods
# Convert the object from it's mongo friendly ruby type to this type.
View
14 lib/mongoid/fields/foreign_key.rb
@@ -62,9 +62,11 @@ def foreign_key?
#
# @since 3.0.0
def evolve(object)
- return object.id if object.is_a?(Document)
- evolved = mongoize(object)
- type.resizable? ? evolved.first : evolved
+ if object_id_field?
+ object.__evolve_object_id__
+ else
+ related_id_field.evolve(object)
+ end
end
# Mongoize the object into the Mongo friendly value.
@@ -81,7 +83,7 @@ def mongoize(object)
if type.resizable? || object_id_field?
type.__mongoize_fk__(constraint, object)
else
- metadata.klass.fields["_id"].mongoize(object)
+ related_id_field.mongoize(object)
end
end
@@ -115,6 +117,10 @@ def evaluate_default_proc(doc)
serialize_default(default_val[])
end
+ def related_id_field
+ @related_id_field ||= metadata.klass.fields["_id"]
+ end
+
# This is used when default values need to be serialized. Most of the
# time just return the object.
#
View
4 spec/mongoid/atomic/paths/root_spec.rb
@@ -20,14 +20,14 @@
describe "#path" do
it "returns an empty string" do
- root.path.should eq("")
+ root.path.should be_empty
end
end
describe "#position" do
it "returns an empty string" do
- root.position.should eq("")
+ root.position.should be_empty
end
end
View
85 spec/mongoid/criteria_spec.rb
@@ -1711,20 +1711,85 @@
describe "\##{method}" do
- let!(:match) do
- Band.create(genres: [ "electro", "dub" ])
- end
+ context "when querying on a normal field" do
- let!(:non_match) do
- Band.create(genres: [ "house" ])
- end
+ let!(:match) do
+ Band.create(genres: [ "electro", "dub" ])
+ end
- let(:criteria) do
- Band.send(method, genres: [ "dub" ])
+ let!(:non_match) do
+ Band.create(genres: [ "house" ])
+ end
+
+ let(:criteria) do
+ Band.send(method, genres: [ "dub" ])
+ end
+
+ it "returns the matching documents" do
+ criteria.should eq([ match ])
+ end
end
- it "returns the matching documents" do
- criteria.should eq([ match ])
+ context "when querying on a foreign key" do
+
+ let(:id) do
+ BSON::ObjectId.new
+ end
+
+ let!(:match_one) do
+ Person.create(preference_ids: [ id ])
+ end
+
+ context "when providing valid ids" do
+
+ let(:criteria) do
+ Person.send(method, preference_ids: [ id ])
+ end
+
+ it "returns the matching documents" do
+ criteria.should eq([ match_one ])
+ end
+ end
+
+ context "when providing empty strings" do
+
+ let(:criteria) do
+ Person.send(method, preference_ids: [ id, "" ])
+ end
+
+ it "returns the matching documents" do
+ criteria.should eq([ match_one ])
+ end
+ end
+
+ context "when providing nils" do
+
+ context "when the relation is a many to many" do
+
+ let(:criteria) do
+ Person.send(method, preference_ids: [ id, nil ])
+ end
+
+ it "returns the matching documents" do
+ criteria.should eq([ match_one ])
+ end
+ end
+
+ context "when the relation is a one to one" do
+
+ let!(:game) do
+ Game.create
+ end
+
+ let(:criteria) do
+ Game.send(method, person_id: [ nil ])
+ end
+
+ it "returns the matching documents" do
+ criteria.should eq([ game ])
+ end
+ end
+ end
end
end
end
View
107 spec/mongoid/extensions/array_spec.rb
@@ -68,8 +68,8 @@
array.__evolve_object_id__
end
- it "returns the array without the nils" do
- evolved.should eq([ object_id ])
+ it "returns the array with the nils" do
+ evolved.should eq([ object_id, nil ])
end
it "returns the same instance" do
@@ -91,8 +91,8 @@
array.__evolve_object_id__
end
- it "returns the array without the empty strings" do
- evolved.should eq([ object_id ])
+ it "returns the array with the empty strings" do
+ evolved.should eq([ object_id, "" ])
end
it "returns the same instance" do
@@ -101,6 +101,105 @@
end
end
+ describe "#__mongoize_object_id__" do
+
+ context "when provided an array of strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:other) do
+ "blah"
+ end
+
+ let(:array) do
+ [ object_id.to_s, other ]
+ end
+
+ let(:mongoized) do
+ array.__mongoize_object_id__
+ end
+
+ it "converts the convertible ones to object ids" do
+ mongoized.should eq([ object_id, other ])
+ end
+
+ it "returns the same instance" do
+ mongoized.should equal(array)
+ end
+ end
+
+ context "when provided an array of object ids" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id ]
+ end
+
+ let(:mongoized) do
+ array.__mongoize_object_id__
+ end
+
+ it "returns the array" do
+ mongoized.should eq(array)
+ end
+
+ it "returns the same instance" do
+ mongoized.should equal(array)
+ end
+ end
+
+ context "when some values are nil" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id, nil ]
+ end
+
+ let(:mongoized) do
+ array.__mongoize_object_id__
+ end
+
+ it "returns the array without the nils" do
+ mongoized.should eq([ object_id ])
+ end
+
+ it "returns the same instance" do
+ mongoized.should equal(array)
+ end
+ end
+
+ context "when some values are empty strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id, "" ]
+ end
+
+ let(:mongoized) do
+ array.__mongoize_object_id__
+ end
+
+ it "returns the array without the empty strings" do
+ mongoized.should eq([ object_id ])
+ end
+
+ it "returns the same instance" do
+ mongoized.should equal(array)
+ end
+ end
+ end
+
describe ".__mongoize_fk__" do
context "when the related model uses object ids" do
View
155 spec/mongoid/extensions/hash_spec.rb
@@ -4,20 +4,159 @@
describe "#__evolve_object_id__" do
- let(:object_id) do
- BSON::ObjectId.new
+ context "when values have object id strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: object_id.to_s }
+ end
+
+ let(:evolved) do
+ hash.__evolve_object_id__
+ end
+
+ it "converts each value in the hash" do
+ evolved[:field].should eq(object_id)
+ end
end
- let(:hash) do
- { field: object_id.to_s }
+ context "when values have object ids" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: object_id }
+ end
+
+ let(:evolved) do
+ hash.__evolve_object_id__
+ end
+
+ it "converts each value in the hash" do
+ evolved[:field].should eq(object_id)
+ end
+ end
+
+ context "when values have empty strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: "" }
+ end
+
+ let(:evolved) do
+ hash.__evolve_object_id__
+ end
+
+ it "retains the empty string values" do
+ evolved[:field].should be_empty
+ end
end
- let(:evolved) do
- hash.__evolve_object_id__
+ context "when values have nils" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: nil }
+ end
+
+ let(:evolved) do
+ hash.__evolve_object_id__
+ end
+
+ it "retains the nil values" do
+ evolved[:field].should be_nil
+ end
end
+ end
+
+ describe "#__mongoize_object_id__" do
+
+ context "when values have object id strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
- it "converts each value in the hash" do
- evolved[:field].should eq(object_id)
+ let(:hash) do
+ { field: object_id.to_s }
+ end
+
+ let(:mongoized) do
+ hash.__mongoize_object_id__
+ end
+
+ it "converts each value in the hash" do
+ mongoized[:field].should eq(object_id)
+ end
+ end
+
+ context "when values have object ids" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: object_id }
+ end
+
+ let(:mongoized) do
+ hash.__mongoize_object_id__
+ end
+
+ it "converts each value in the hash" do
+ mongoized[:field].should eq(object_id)
+ end
+ end
+
+ context "when values have empty strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: "" }
+ end
+
+ let(:mongoized) do
+ hash.__mongoize_object_id__
+ end
+
+ it "converts the empty strings to nil" do
+ mongoized[:field].should be_nil
+ end
+ end
+
+ context "when values have nils" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { field: nil }
+ end
+
+ let(:mongoized) do
+ hash.__mongoize_object_id__
+ end
+
+ it "retains the nil values" do
+ mongoized[:field].should be_nil
+ end
end
end
View
2  spec/mongoid/extensions/nil_class_spec.rb
@@ -5,7 +5,7 @@
describe "#collectionize" do
it "returns ''" do
- nil.collectionize.should eq("")
+ nil.collectionize.should be_empty
end
end
end
View
327 spec/mongoid/extensions/object_id_spec.rb
@@ -79,8 +79,8 @@
BSON::ObjectId.evolve("")
end
- it "evolves to nil" do
- evolved.should be_nil
+ it "evolves to an empty string" do
+ evolved.should be_empty
end
end
end
@@ -93,8 +93,8 @@
BSON::ObjectId.evolve([ nil, nil ])
end
- it "returns an empty array" do
- evolved.should be_empty
+ it "returns the array with nils" do
+ evolved.should eq([ nil, nil ])
end
end
@@ -104,8 +104,8 @@
BSON::ObjectId.evolve([ "", "" ])
end
- it "returns an empty array" do
- evolved.should be_empty
+ it "returns the array with empty strings" do
+ evolved.should eq([ "", "" ])
end
end
@@ -628,4 +628,319 @@
end
end
end
+
+ describe ".mongoize" do
+
+ context "when the class is using object ids" do
+
+ context "when provided a single object id" do
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(object_id)
+ end
+
+ it "returns the object id" do
+ mongoized.should eq(object_id)
+ end
+ end
+
+ context "when provided an array of object ids" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize([ object_id, other_id ])
+ end
+
+ it "returns the array of object ids" do
+ mongoized.should eq([ object_id, other_id ])
+ end
+ end
+
+ context "when provided a single string" do
+
+ context "when the string is a valid object id" do
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(object_id.to_s)
+ end
+
+ it "mongoizes to an object id" do
+ mongoized.should eq(object_id)
+ end
+ end
+
+ context "when the string is not a valid object id" do
+
+ it "returns the key" do
+ BSON::ObjectId.mongoize(composite_key).should eq(
+ composite_key
+ )
+ end
+ end
+
+ context "when the string is empty" do
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize("")
+ end
+
+ it "mongoizes to nil" do
+ mongoized.should be_nil
+ end
+ end
+ end
+
+ context "when provided an array" do
+
+ context "when array key of nils" do
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize([ nil, nil ])
+ end
+
+ it "returns an empty array" do
+ mongoized.should be_empty
+ end
+ end
+
+ context "when the array key is empty strings" do
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize([ "", "" ])
+ end
+
+ it "returns an empty array" do
+ mongoized.should be_empty
+ end
+ end
+
+ context "when the array key is full of strings" do
+
+ context "when the strings are valid object ids" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize([ object_id.to_s, other_id.to_s ])
+ end
+
+ it "mongoizes to an array of object ids" do
+ mongoized.should eq([ object_id, other_id ])
+ end
+ end
+
+ context "when the strings are not valid object ids" do
+
+ let(:other_key) do
+ "hawaii-five-o"
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize([ composite_key, other_key ])
+ end
+
+ it "returns the key" do
+ BSON::ObjectId.mongoize(composite_key).should eq(
+ composite_key
+ )
+ end
+ end
+ end
+ end
+
+ context "when provided a hash" do
+
+ context "when the hash key is _id" do
+
+ context "when the value is an object id" do
+
+ let(:hash) do
+ { _id: object_id }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+
+ context "when the value is an array of object ids" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { _id: [ object_id, other_id ] }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+
+ context "when the value is a string" do
+
+ let(:hash) do
+ { _id: object_id.to_s }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash with mongoized value" do
+ mongoized.should eq({ _id: object_id })
+ end
+ end
+
+ context "when the value is an array of strings" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { _id: [ object_id.to_s, other_id.to_s ] }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash with mongoized values" do
+ mongoized.should eq({ _id: [ object_id, other_id ] })
+ end
+ end
+ end
+
+ context "when the hash key is id" do
+
+ context "when the value is an object id" do
+
+ let(:hash) do
+ { id: object_id }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+
+ context "when the value is an array of object ids" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { id: [ object_id, other_id ] }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+
+ context "when the value is a string" do
+
+ let(:hash) do
+ { id: object_id.to_s }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash with mongoized value" do
+ mongoized.should eq({ id: object_id })
+ end
+ end
+
+ context "when the value is an array of strings" do
+
+ let(:other_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:hash) do
+ { id: [ object_id.to_s, other_id.to_s ] }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash with mongoized values" do
+ mongoized.should eq({ id: [ object_id, other_id ] })
+ end
+ end
+ end
+
+ context "when the hash key is not an id" do
+
+ context "when the value is a string" do
+
+ let(:hash) do
+ { key: composite_key }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+
+ context "when the value is an array of strings" do
+
+ let(:hash) do
+ { key: [ composite_key ] }
+ end
+
+ let(:mongoized) do
+ BSON::ObjectId.mongoize(hash)
+ end
+
+ it "returns the hash" do
+ mongoized.should eq(hash)
+ end
+ end
+ end
+ end
+ end
+ end
+
+ describe "#__mongoize_object_id__" do
+
+ it "returns self" do
+ object_id.__mongoize_object_id__.should eq(object_id)
+ end
+
+ it "returns the same instance" do
+ object_id.__mongoize_object_id__.should equal(object_id)
+ end
+ end
end
View
11 spec/mongoid/extensions/object_spec.rb
@@ -13,6 +13,17 @@
end
end
+ describe "#__mongoize_object_id__" do
+
+ let(:object) do
+ Object.new
+ end
+
+ it "returns self" do
+ object.__mongoize_object_id__.should eq(object)
+ end
+ end
+
describe ".__mongoize_fk__" do
context "when the related model uses object ids" do
View
36 spec/mongoid/extensions/string_spec.rb
@@ -6,8 +6,8 @@
context "when the string is blank" do
- it "returns nil" do
- "".__evolve_object_id__.should be_nil
+ it "returns the empty string" do
+ "".__evolve_object_id__.should be_empty
end
end
@@ -34,6 +34,38 @@
end
end
+ describe "#__mongoize_object_id__" do
+
+ context "when the string is blank" do
+
+ it "returns nil" do
+ "".__mongoize_object_id__.should be_nil
+ end
+ end
+
+ context "when the string is a legal object id" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ it "returns the object id" do
+ object_id.to_s.__mongoize_object_id__.should eq(object_id)
+ end
+ end
+
+ context "when the string is not a legal object id" do
+
+ let(:string) do
+ "testing"
+ end
+
+ it "returns the string" do
+ string.__mongoize_object_id__.should eq(string)
+ end
+ end
+ end
+
describe "#__mongoize_time__" do
context "when using active support's time zone" do
View
345 spec/mongoid/fields/foreign_key_spec.rb
@@ -101,6 +101,351 @@
end
end
+ describe "#evolve" do
+
+ let(:metadata) do
+ Person.reflect_on_association(:preferences)
+ end
+
+ context "when provided a document" do
+
+ let(:field) do
+ described_class.new(:person_id, type: Object, metadata: metadata)
+ end
+
+ let(:game) do
+ Game.new
+ end
+
+ let(:evolved) do
+ field.evolve(game)
+ end
+
+ it "returns the id for the document" do
+ evolved.should eq(game.id)
+ end
+ end
+
+ context "when the type is an array" do
+
+ let(:field) do
+ described_class.new(:preference_ids, type: Array, default: [], metadata: metadata)
+ end
+
+ context "when providing a single value" do
+
+ context "when the value is an id string" do
+
+ let(:id) do
+ BSON::ObjectId.new
+ end
+
+ let(:evolved) do
+ field.evolve(id.to_s)
+ end
+
+ it "converts the value to an object id" do
+ evolved.should eq(id)
+ end
+ end
+
+ context "when the value is a normal string" do
+
+ let(:evolved) do
+ field.evolve("testing")
+ end
+
+ it "does not convert the value" do
+ evolved.should eq("testing")
+ end
+ end
+
+ context "when the value is an empty string" do
+
+ let(:evolved) do
+ field.evolve("")
+ end
+
+ it "does not convert the value" do
+ evolved.should be_empty
+ end
+ end
+ end
+
+ context "when providing an array" do
+
+ context "when the values are id strings" do
+
+ context "when the relation stores ids as object ids" do
+
+ let(:id_one) do
+ BSON::ObjectId.new
+ end
+
+ let(:id_two) do
+ BSON::ObjectId.new
+ end
+
+ let(:evolved) do
+ field.evolve([ id_one.to_s, id_two.to_s ])
+ end
+
+ it "converts the value to an object id" do
+ evolved.should eq([ id_one, id_two ])
+ end
+ end
+
+ context "when the relation stores ids as strings" do
+
+ let!(:metadata) do
+ Agent.reflect_on_association(:accounts)
+ end
+
+ let!(:field) do
+ described_class.new(:account_ids, type: Array, default: [], metadata: metadata)
+ end
+
+ let(:id_one) do
+ BSON::ObjectId.new.to_s
+ end
+
+ let(:id_two) do
+ BSON::ObjectId.new.to_s
+ end
+
+ let(:evolved) do
+ field.evolve([ id_one, id_two ])
+ end
+
+ it "does not convert the values to object ids" do
+ evolved.should eq([ id_one, id_two ])
+ end
+ end
+ end
+
+ context "when the values are normal strings" do
+
+ let(:evolved) do
+ field.evolve([ "testing" ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ "testing" ])
+ end
+ end
+
+ context "when the values are empty strings" do
+
+ let(:evolved) do
+ field.evolve([ "" ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ "" ])
+ end
+ end
+
+ context "when the values are nils" do
+
+ let(:evolved) do
+ field.evolve([ nil ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ nil ])
+ end
+ end
+ end
+ end
+
+ context "when the type is an object" do
+
+ let(:metadata) do
+ Game.reflect_on_association(:person)
+ end
+
+ let(:field) do
+ described_class.new(:person_id, type: Object, metadata: metadata)
+ end
+
+ context "when providing a single value" do
+
+ context "when the relation stores object ids" do
+
+ context "when the value is an id string" do
+
+ let(:id) do
+ BSON::ObjectId.new
+ end
+
+ let(:evolved) do
+ field.evolve(id.to_s)
+ end
+
+ it "converts the value to an object id" do
+ evolved.should eq(id)
+ end
+ end
+
+ context "when the value is a normal string" do
+
+ let(:evolved) do
+ field.evolve("testing")
+ end
+
+ it "does not convert the value" do
+ evolved.should eq("testing")
+ end
+ end
+
+ context "when the value is an empty string" do
+
+ let(:evolved) do
+ field.evolve("")
+ end
+
+ it "does not convert the value" do
+ evolved.should be_empty
+ end
+ end
+ end
+
+ context "when the relation stores string ids" do
+
+ let(:metadata) do
+ Comment.reflect_on_association(:account)
+ end
+
+ let(:field) do
+ described_class.new(:person_id, type: Object, metadata: metadata)
+ end
+
+ context "when the value is an id string" do
+
+ let(:id) do
+ BSON::ObjectId.new
+ end
+
+ let(:evolved) do
+ field.evolve(id.to_s)
+ end
+
+ it "does not convert the value to an object id" do
+ evolved.should eq(id.to_s)
+ end
+ end
+
+ context "when the value is a normal string" do
+
+ let(:evolved) do
+ field.evolve("testing")
+ end
+
+ it "does not convert the value" do
+ evolved.should eq("testing")
+ end
+ end
+
+ context "when the value is an empty string" do
+
+ let(:evolved) do
+ field.evolve("")
+ end
+
+ it "does not convert the value" do
+ evolved.should be_empty
+ end
+ end
+ end
+ end
+
+ context "when providing an array" do
+
+ context "when the values are id strings" do
+
+ context "when the relation stores ids as object ids" do
+
+ let(:id_one) do
+ BSON::ObjectId.new
+ end
+
+ let(:id_two) do
+ BSON::ObjectId.new
+ end
+
+ let(:evolved) do
+ field.evolve([ id_one.to_s, id_two.to_s ])
+ end
+
+ it "converts the value to an object id" do
+ evolved.should eq([ id_one, id_two ])
+ end
+ end
+
+ context "when the relation stores ids as strings" do
+
+ let(:metadata) do
+ Comment.reflect_on_association(:account)
+ end
+
+ let(:field) do
+ described_class.new(:person_id, type: Object, metadata: metadata)
+ end
+
+ let(:id_one) do
+ BSON::ObjectId.new.to_s
+ end
+
+ let(:id_two) do
+ BSON::ObjectId.new.to_s
+ end
+
+ let(:evolved) do
+ field.evolve([ id_one, id_two ])
+ end
+
+ it "does not convert the values to object ids" do
+ evolved.should eq([ id_one, id_two ])
+ end
+ end
+ end
+
+ context "when the values are normal strings" do
+
+ let(:evolved) do
+ field.evolve([ "testing" ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ "testing" ])
+ end
+ end
+
+ context "when the values are empty strings" do
+
+ let(:evolved) do
+ field.evolve([ "" ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ "" ])
+ end
+ end
+
+ context "when the values are nils" do
+
+ let(:evolved) do
+ field.evolve([ nil ])
+ end
+
+ it "does not convert the value" do
+ evolved.should eq([ nil ])
+ end
+ end
+ end
+ end
+ end
+
describe "#mongoize" do
context "when the type is array" do
View
2  spec/mongoid/relations/metadata_spec.rb
@@ -282,7 +282,7 @@
end
it "returns root namespace" do
- mod.should eq("")
+ mod.should be_empty
end
end
View
60 spec/mongoid/relations/referenced/many_spec.rb
@@ -309,36 +309,6 @@
movie.ratings.count.should eq(1)
end
end
-
- context "when parent has String identity" do
-
- before do
- Movie.field(
- :_id,
- type: String,
- pre_processed: true,
- default: ->{ BSON::ObjectId.new.to_s }
- )
- movie.ratings << Rating.new
- end
-
- after do
- Movie.field(
- :_id,
- type: BSON::ObjectId,
- pre_processed: true,
- default: ->{ BSON::ObjectId.new }
- )
- end
-
- let(:movie) do
- Movie.create
- end
-
- it "has rating references" do
- movie.ratings.count.should eq(1)
- end
- end
end
end
end
@@ -1354,36 +1324,6 @@
movie.ratings.count.should eq(1)
end
end
-
- context "when parent has String identity" do
-
- before do
- Movie.field(
- :_id,
- pre_processed: true,
- type: String,
- default: ->{ BSON::ObjectId.new.to_s }
- )
- movie.ratings << Rating.new
- end
-
- after do
- Movie.field(
- :_id,
- pre_processed: true,
- type: BSON::ObjectId,
- default: ->{ BSON::ObjectId.new }
- )
- end
-
- let(:movie) do
- Movie.create
- end
-
- it "has rating references" do
- movie.ratings.count.should eq(1)
- end
- end
end
describe "#count" do
Please sign in to comment.
Something went wrong with that request. Please try again.