lib/mongo_mapper/plugins/keys.rb in mongo_mapper-0.7.3 vs lib/mongo_mapper/plugins/keys.rb in mongo_mapper-0.7.4

- old
+ new

@@ -93,11 +93,10 @@ include accessors_module end def create_key_in_descendants(*args) - return if descendants.blank? descendants.each { |descendant| descendant.key(*args) } end def create_indexes_for(key) ensure_index key.name if key.options[:index] && !key.embeddable? @@ -186,11 +185,15 @@ attrs[name] = value end embedded_associations.each do |association| if documents = instance_variable_get(association.ivar) - attrs[association.name] = documents.map { |document| document.to_mongo } + if association.one? + attrs[association.name] = documents.to_mongo + else + attrs[association.name] = documents.map { |document| document.to_mongo } + end end end attrs end @@ -250,11 +253,11 @@ private def default_id_value(attrs) unless attrs.nil? provided_keys = attrs.keys.map { |k| k.to_s } unless provided_keys.include?('_id') || provided_keys.include?('id') - write_key :_id, Mongo::ObjectID.new + write_key :_id, BSON::ObjectID.new end end end def assign_type @@ -263,14 +266,21 @@ def ensure_key_exists(name) self.class.key(name) unless respond_to?("#{name}=") end + def set_parent_document(key, value) + if key.embeddable? && value.is_a?(key.type) + value._parent_document = self + end + end + def read_key(name) if key = keys[name] var_name = "@#{name}" value = key.get(instance_variable_get(var_name)) + set_parent_document(key, value) instance_variable_set(var_name, value) else raise KeyNotFound, "Could not find key: #{name.inspect}" end end @@ -280,13 +290,10 @@ end def write_key(name, value) key = keys[name] - if key.embeddable? && value.is_a?(key.type) - value._parent_document = self - end - + set_parent_document(key, value) instance_variable_set "@#{name}_before_typecast", value instance_variable_set "@#{name}", key.set(value) end end