lib/machinist/mongo_mapper.rb in machinist_mongo-1.0.1 vs lib/machinist/mongo_mapper.rb in machinist_mongo-1.0.2

- old
+ new

@@ -1,8 +1,15 @@ -require 'machinist' -require 'machinist/blueprints' +require "machinist" +require "machinist/blueprints" +begin + require "mongo_mapper" +rescue LoadError + puts "MongoMapper is not installed (gem install mongo_mapper)" + exit +end + module Machinist class MongoMapperAdapter def self.has_association?(object, attribute) object.class.associations[attribute] @@ -15,11 +22,11 @@ def self.assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| association = lathe.object.class.associations[attribute] - if association && association.belongs_to? + if association && association.belongs_to? && !value.nil? attributes[association.foreign_key.to_sym] = value.id else attributes[attribute] = value end end @@ -29,11 +36,14 @@ module MongoMapperExtensions module Document def make(*args, &block) lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args) - lathe.object.save! unless Machinist.nerfed? + unless Machinist.nerfed? + lathe.object.save! + lathe.object.reload + end lathe.object(&block) end def make_unsaved(*args) returning(Machinist.with_save_nerfed { make(*args) }) do |object| @@ -57,6 +67,6 @@ end MongoMapper::Document.append_extensions(Machinist::Blueprints::ClassMethods) MongoMapper::Document.append_extensions(Machinist::MongoMapperExtensions::Document) MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods) -MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument) +MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument) \ No newline at end of file