Skip to content

Commit

Permalink
Initial pass at removing parse_method and cleaning up :build vs :crea…
Browse files Browse the repository at this point in the history
…te for associations
  • Loading branch information
joshuaclayton committed Oct 20, 2011
1 parent 8f4e052 commit 50a66d0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 49 deletions.
30 changes: 13 additions & 17 deletions lib/factory_girl/proxy/build.rb
Expand Up @@ -19,9 +19,7 @@ def set(attribute, value)
end

def associate(name, factory_name, overrides)
method = get_method(overrides[:method])
factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(method, remove_method(overrides)))
set(name, association(factory_name, overrides))
end

def association(factory_name, overrides = {})
Expand All @@ -30,27 +28,25 @@ def association(factory_name, overrides = {})
factory.run(method, remove_method(overrides))
end

def remove_method(overrides)
overrides.dup.delete_if {|key, value| key == :method}
end

def result(to_create)
run_callbacks(:after_build)
@instance
end

def parse_method(method)
method ||= :create
if :build == method
return Proxy::Build
elsif :create == method
return Proxy::Create
else
raise "unrecognized method #{method}"
end
private

def remove_method(overrides)
overrides.dup.delete_if {|key, value| key == :method}
end

alias_method :get_method, :parse_method
def get_method(method)
case method
when :build then Proxy::Build
when :create then Proxy::Create
when nil then Proxy::Create
else raise "unrecognized method #{method}"
end
end
end
end
end
6 changes: 0 additions & 6 deletions lib/factory_girl/proxy/create.rb
Expand Up @@ -11,12 +11,6 @@ def result(to_create)
run_callbacks(:after_create)
@instance
end

def get_method(method_string)
# Leaving this as Proxy::Build in the :method => :build case
# is a bit strange, but does it have any user-visible behaviors?
parse_method(method_string)
end
end
end
end
3 changes: 1 addition & 2 deletions lib/factory_girl/proxy/stub.rb
Expand Up @@ -60,8 +60,7 @@ def set(attribute, value)
end

def associate(name, factory_name, overrides)
factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(Proxy::Stub, remove_method(overrides)))
set(name, association(factory_name, overrides))
end

def association(factory_name, overrides = {})
Expand Down
25 changes: 1 addition & 24 deletions spec/factory_girl/proxy/build_spec.rb
Expand Up @@ -9,28 +9,5 @@
it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Create
it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
it_should_behave_like "proxy with callbacks", :after_build
it_should_behave_like "proxy with :method => :build",
FactoryGirl::Proxy::Build

describe "specifying method" do
it "defaults to create" do
subject.send(:get_method, nil).should == FactoryGirl::Proxy::Create
end

it "can specify create explicitly" do
subject.send(:get_method, :create).should ==
FactoryGirl::Proxy::Create
end

it "can specify build explicitly" do
subject.send(:get_method, :build).should ==
FactoryGirl::Proxy::Build
end

it "complains if method is unrecognized" do
lambda { subject.send(:get_method, :froboznicate) }.
should raise_error("unrecognized method froboznicate")
end
end

it_should_behave_like "proxy with :method => :build", FactoryGirl::Proxy::Build
end

0 comments on commit 50a66d0

Please sign in to comment.