Skip to content

Commit

Permalink
Remove support for :method to define build strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Mar 16, 2012
1 parent f68a617 commit eca05c9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 94 deletions.
7 changes: 2 additions & 5 deletions lib/factory_girl/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ def initialize(build_strategy, overrides = {})
end

def association(factory_name, overrides = {})
build_strategy = if overrides.has_key?(:method)
$stderr.puts "DEPRECATION WARNING: using :method to specify a build strategy is deprecated; use :strategy instead"
overrides[:method]
elsif overrides.has_key?(:strategy)
build_strategy = if overrides.has_key?(:strategy)
overrides[:strategy]
else
Strategy::Create
end

build_strategy = StrategyCalculator.new(build_strategy).strategy
runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:method, :strategy)])
runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:strategy)])
@build_strategy.association(runner)
end

Expand Down
47 changes: 9 additions & 38 deletions spec/acceptance/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,16 @@
end
end

describe "a created instance, specifying :strategy => build" do
describe "a created instance, specifying :strategy => :build" do
include FactoryGirl::Syntax::Methods

def define_factories_with_method
FactoryGirl.define do
factory :user
before do
define_model('User')

factory :post do
association(:user, :method => :build)
end
define_model('Post', :user_id => :integer) do
belongs_to :user
end
end

def define_factories_with_strategy
FactoryGirl.define do
factory :user

Expand All @@ -54,36 +50,11 @@ def define_factories_with_strategy
end
end

before do
define_model('User')

define_model('Post', :user_id => :integer) do
belongs_to :user
end
end

context "associations declared with :strategy" do
before { define_factories_with_strategy }
subject { build_stubbed(:post) }
subject { create(:post) }

subject { create('post') }

it "still saves associations (:strategy => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end

context "associations declared with :method" do
before { define_factories_with_method }
subject { build_stubbed(:post) }

subject { create('post') }

it "still saves associations (:method => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
it "saves associations (:strategy => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end

Expand Down
49 changes: 10 additions & 39 deletions spec/acceptance/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@
describe "a stubbed instance overriding strategy" do
include FactoryGirl::Syntax::Methods

def define_factories_with_method
FactoryGirl.define do
factory :user

factory :post do
association(:user, :method => :build)
end
before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
end

def define_factories_with_strategy
FactoryGirl.define do
factory :user

Expand All @@ -54,38 +49,14 @@ def define_factories_with_strategy
end
end

before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
end

context "associations declared with :strategy" do
before { define_factories_with_strategy }
subject { build_stubbed(:post) }

it "acts as if it is saved in the database" do
should_not be_new_record
end
subject { build_stubbed(:post) }

it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
it "acts as if it is saved in the database" do
should_not be_new_record
end

context "associations declared with :method" do
before { define_factories_with_method }
subject { build_stubbed(:post) }

it "acts as if it is saved in the database" do
should_not be_new_record
end

it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end
14 changes: 2 additions & 12 deletions spec/support/shared_examples/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def association_named(name, strategy, overrides)
let(:factory) { stub("associate_factory") }

def association_named(name, overrides)
strategy = FactoryGirl::StrategyCalculator.new(overrides[:strategy] || overrides[:method]).strategy
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides.except(:strategy, :method)])
strategy = FactoryGirl::StrategyCalculator.new(overrides[:strategy]).strategy
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides.except(:strategy)])
subject.association(runner)
end

Expand All @@ -66,16 +66,6 @@ def association_named(name, overrides)
association_named(:author, :strategy => :build, :great => "value")
FactoryGirl.should have_received(:factory_by_name).with(:author)
end

it "runs the factory with the correct overrides with :method" do
association_named(:author, :method => :build, :great => "value")
factory.should have_received(:run).with(factory_girl_strategy_class, { :great => "value" })
end

it "finds the factory with the correct factory name with :method" do
association_named(:author, :method => :build, :great => "value")
FactoryGirl.should have_received(:factory_by_name).with(:author)
end
end

shared_examples_for "strategy with callbacks" do |*callback_names|
Expand Down

0 comments on commit eca05c9

Please sign in to comment.