Skip to content

Commit

Permalink
Allow make_unsaved to work for Sequel object graphs.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Firebaugh committed Apr 26, 2010
1 parent 36bafdb commit 2ad5c4b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/machinist.rb
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions lib/machinist/sequel.rb
Expand Up @@ -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|
Expand Down
16 changes: 8 additions & 8 deletions spec/sequel_spec.rb
Expand Up @@ -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 { }
Expand Down

0 comments on commit 2ad5c4b

Please sign in to comment.