lib/mongoid/fields/foreign_key.rb in mongoid-3.0.0.rc vs lib/mongoid/fields/foreign_key.rb in mongoid-3.0.0

- old
+ new

@@ -60,13 +60,15 @@ # # @return [ Object ] The evolved object. # # @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.is_a?(Document) + object.__evolve_object_id__ + else + related_id_field.evolve(object) + end end # Mongoize the object into the Mongo friendly value. # # @example Mongoize the object. @@ -79,20 +81,20 @@ # @since 3.0.0 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 - # Is the field a BSON::ObjectId? + # Is the field a Moped::BSON::ObjectId? # - # @example Is the field a BSON::ObjectId? + # @example Is the field a Moped::BSON::ObjectId? # field.object_id_field? # - # @return [ true, false ] If the field is a BSON::ObjectId. + # @return [ true, false ] If the field is a Moped::BSON::ObjectId. # # @since 2.2.0 def object_id_field? @object_id_field ||= metadata.polymorphic? ? true : metadata.klass.using_object_ids? @@ -111,9 +113,23 @@ # @return [ Object ] The called proc. # # @since 3.0.0 def evaluate_default_proc(doc) serialize_default(default_val[]) + end + + # Get the id field of the relation. + # + # @api private + # + # @example Get the related id field. + # field.related_id_field + # + # @return [ Fields::Standard ] The field. + # + # @since 3.0.0 + 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. #