lib/machinist/mongoid.rb in machinist_mongo-1.1.1 vs lib/machinist/mongoid.rb in machinist_mongo-1.2.0

- old
+ new

@@ -10,62 +10,66 @@ module Machinist class Lathe def assign_attribute(key, value) assigned_attributes[key.to_sym] = value - @object.process(key => value) + if @object.respond_to?("#{key}=") + @object.send("#{key}=", value) + else + @object.process(key => value) + end end end - + class MongoidAdapter class << self def has_association?(object, attribute) object.class.associations[attribute.to_s] end - + def class_for_association(object, attribute) association = object.class.associations[attribute.to_s] association && association.klass end def assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| association = lathe.object.class.associations[attribute.to_s] - if association && (association.macro == :belongs_to_related) && !value.nil? + if association && (association.macro == :referenced_in) && !value.nil? attributes[association.foreign_key.to_sym] = value.id else attributes[attribute] = value end end - attributes - end + attributes + end end end - + module MongoidExtensions module Document def make(*args, &block) lathe = Lathe.run(Machinist::MongoidAdapter, self.new, *args) unless Machinist.nerfed? || embedded lathe.object.save! lathe.object.reload end lathe.object(&block) end - + def make_unsaved(*args) - returning(Machinist.with_save_nerfed { make(*args) }) do |object| + Machinist.with_save_nerfed { make(*args) }.tap do |object| yield object if block_given? end end - + def plan(*args) lathe = Lathe.run(Machinist::MongoidAdapter, self.new, *args) Machinist::MongoidAdapter.assigned_attributes_without_associations(lathe) end end end end Mongoid::Document::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods) -Mongoid::Document::ClassMethods.send(:include, Machinist::MongoidExtensions::Document) \ No newline at end of file +Mongoid::Document::ClassMethods.send(:include, Machinist::MongoidExtensions::Document)