Skip to content

Commit

Permalink
Pass evaluator to to_create (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Colson authored and joshuaclayton committed Sep 28, 2017
1 parent 3872ab7 commit 5e6f018
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/factory_girl/evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ module FactoryGirl
class Evaluation
include Observable

def initialize(attribute_assigner, to_create)
def initialize(evaluator, attribute_assigner, to_create)
@evaluator = evaluator
@attribute_assigner = attribute_assigner
@to_create = to_create
end

delegate :object, :hash, to: :@attribute_assigner

def create(result_instance)
@to_create[result_instance]
case @to_create.arity
when 2 then @to_create[result_instance, @evaluator]
else @to_create[result_instance]
end
end

def notify(name, result_instance)
Expand Down
3 changes: 2 additions & 1 deletion lib/factory_girl/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def run(build_strategy, overrides, &block)
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)

evaluation = Evaluation.new(attribute_assigner, compiled_to_create)
evaluation =
Evaluation.new(evaluator, attribute_assigner, compiled_to_create)
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))

strategy.result(evaluation).tap(&block)
Expand Down
22 changes: 22 additions & 0 deletions spec/acceptance/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ def persisted?
end
end

describe "a custom create passing in an evaluator" do
before do
define_class("User") do
attr_accessor :name
end

FactoryGirl.define do
factory :user do
transient { creation_name "evaluator" }

to_create do |user, evaluator|
user.name = evaluator.creation_name
end
end
end
end

it "passes the evaluator to the custom create block" do
expect(FactoryGirl.create(:user).name).to eq "evaluator"
end
end

describe "calling `create` with a block" do
include FactoryGirl::Syntax::Methods

Expand Down

0 comments on commit 5e6f018

Please sign in to comment.