diff --git a/lib/machinist.rb b/lib/machinist.rb index f6bc0ed..477620f 100644 --- a/lib/machinist.rb +++ b/lib/machinist.rb @@ -81,7 +81,11 @@ def nil_or_empty?(object) def assign_attribute(key, value) assigned_attributes[key.to_sym] = value - @object.send("#{key}=", value) + if @adapter.respond_to?(:assign_attribute) + @adapter.assign_attribute(@object, key, value) + else + @object.send("#{key}=", value) + end end def attribute_assigned?(key) diff --git a/lib/machinist/sequel.rb b/lib/machinist/sequel.rb index b26f390..0307908 100644 --- a/lib/machinist/sequel.rb +++ b/lib/machinist/sequel.rb @@ -12,6 +12,14 @@ def self.class_for_association(object, attribute) object.class.association_reflection(attribute).associated_class end + def self.assign_attribute(object, attribute, value) + if Machinist.nerfed? && has_association?(object, attribute) + object.associations[attribute] = value + else + object.send("#{attribute}=", value) + end + end + def self.assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| diff --git a/spec/sequel_spec.rb b/spec/sequel_spec.rb index 98cdc72..4712d75 100644 --- a/spec/sequel_spec.rb +++ b/spec/sequel_spec.rb @@ -124,20 +124,20 @@ class Comment < Sequel::Model end end - # Note that building up an unsaved object graph using just the - # association methods is not possible in Sequel, so - # make_unsaved will break in amusing ways unless you manually - # override the setters. - # - # From sequel-talk "Sequel does not have such an API and will not be adding one" - # Feb 17 describe "make_unsaved method" do it "should not save the constructed object" do Person.blueprint { } person = Person.make_unsaved person.should be_new end - + + it "should not save associated objects" do + Post.blueprint { } + Comment.blueprint { post } + comment = Comment.make_unsaved + comment.post.should be_new + end + it "should save objects made within a passed-in block" do Post.blueprint { } Comment.blueprint { }