Skip to content

Commit

Permalink
Added make_unsaved for DataMapper.
Browse files Browse the repository at this point in the history
The with_save_nerfed and nerfed? methods now live in the Machinist
module, rather than in the ActiveRecord adapter.
  • Loading branch information
notahat committed May 29, 2009
1 parent 1745657 commit c25abca
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
18 changes: 18 additions & 0 deletions lib/machinist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,22 @@ def generate_attribute_value(attribute, *args)
end

end

# This sets a flag that stops make from saving objects, so
# that calls to make from within a blueprint don't create
# anything inside make_unsaved.
def self.with_save_nerfed
begin
@@nerfed = true
yield
ensure
@@nerfed = false
end
end

@@nerfed = false
def self.nerfed?
@@nerfed
end

end
23 changes: 3 additions & 20 deletions lib/machinist/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@ def self.assigned_attributes_without_associations(lathe)
attributes
end

# This sets a flag that stops make from saving objects, so
# that calls to make from within a blueprint don't create
# anything inside make_unsaved.
def self.with_save_nerfed
begin
@@nerfed = true
yield
ensure
@@nerfed = false
end
end

@@nerfed = false
def self.nerfed?
@@nerfed
end

end

module ActiveRecordExtensions
Expand All @@ -67,15 +50,15 @@ def self.included(base)
module ClassMethods
def make(*args, &block)
lathe = Lathe.run(Machinist::ActiveRecordAdapter, self.new, *args)
unless Machinist::ActiveRecordAdapter.nerfed?
unless Machinist.nerfed?
lathe.object.save!
lathe.object.reload
end
lathe.object(&block)
end

def make_unsaved(*args)
returning(Machinist::ActiveRecordAdapter.with_save_nerfed { make(*args) }) do |object|
returning(Machinist.with_save_nerfed { make(*args) }) do |object|
yield object if block_given?
end
end
Expand All @@ -90,7 +73,7 @@ def plan(*args)
module ActiveRecordHasManyExtensions
def make(*args, &block)
lathe = Lathe.run(Machinist::ActiveRecordAdapter, self.build, *args)
unless Machinist::ActiveRecordAdapter.nerfed?
unless Machinist.nerfed?
lathe.object.save!
lathe.object.reload
end
Expand Down
12 changes: 10 additions & 2 deletions lib/machinist/data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ def self.class_for_association(object, attribute)
module DataMapperExtensions
def make(*args, &block)
lathe = Lathe.run(Machinist::DataMapperAdapter, self.new, *args)
lathe.object.save || raise("Save failed")
lathe.object.reload
unless Machinist.nerfed?
lathe.object.save || raise("Save failed")
lathe.object.reload
end
lathe.object(&block)
end

def make_unsaved(*args)
returning(Machinist.with_save_nerfed { make(*args) }) do |object|
yield object if block_given?
end
end
end

end
Expand Down
24 changes: 24 additions & 0 deletions spec/data_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ class Comment
it "should raise an exception if the object can't be saved"
end

describe "make_unsaved method" do
it "should not save the constructed object" do
Person.blueprint { }
person = Person.make_unsaved
person.should be_new_record
end

it "should not save associated objects" do
Post.blueprint { }
Comment.blueprint { post }
comment = Comment.make_unsaved
comment.post.should be_new_record
end

it "should save objects made within a passed-in block" do
Post.blueprint { }
Comment.blueprint { }
comment = nil
post = Post.make_unsaved { comment = Comment.make }
post.should be_new_record
comment.should_not be_new_record
end
end

end
end

0 comments on commit c25abca

Please sign in to comment.