Skip to content

Commit 6a63eea

Browse files
fix before_build_callback
change the approach for calling the before_build_callback, now it is called before the object is built, and thus called without any attributes
1 parent edf9fae commit 6a63eea

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

lib/factory_bot/callback.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def initialize(name, block)
77
@block = block
88
end
99

10+
def run_before_build
11+
syntax_runner.instance_exec(&block)
12+
end
13+
1014
def run(instance, evaluator)
1115
case block.arity
1216
when 1, -1, -2 then syntax_runner.instance_exec(instance, &block)
@@ -20,6 +24,10 @@ def ==(other)
2024
block == other.block
2125
end
2226

27+
def before_build?
28+
name == :before_build
29+
end
30+
2331
protected
2432

2533
attr_reader :block

lib/factory_bot/factory.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def build_class
3232
def run(build_strategy, overrides, &block)
3333
block ||= ->(result) { result }
3434
compile
35+
run_before_build_callbacks
3536

3637
strategy = StrategyCalculator.new(build_strategy).strategy.new
3738

@@ -139,6 +140,12 @@ def compiled_constructor
139140
hierarchy_instance.constructor
140141
end
141142

143+
def run_before_build_callbacks
144+
callbacks.each do |callback|
145+
callback.run_before_build if callback.before_build?
146+
end
147+
end
148+
142149
private
143150

144151
def assert_valid_options(options)

lib/factory_bot/strategy/build.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def association(runner)
77

88
def result(evaluation)
99
evaluation.object.tap do |instance|
10-
evaluation.notify(:before_build, instance)
1110
evaluation.notify(:after_build, instance)
1211
end
1312
end

lib/factory_bot/strategy/create.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def association(runner)
77

88
def result(evaluation)
99
evaluation.object.tap do |instance|
10-
evaluation.notify(:before_build, instance)
1110
evaluation.notify(:after_build, instance)
1211
evaluation.notify(:before_create, instance)
1312
evaluation.create(instance)

spec/acceptance/callbacks_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,33 @@ def name
249249
expect(build(:company).name).to eq "ACME SUPPLIERS"
250250
end
251251
end
252+
253+
describe "before build callback" do
254+
class TitleSetter
255+
def self.title=(new_title)
256+
@@title = new_title
257+
end
258+
259+
def self.title
260+
@@title
261+
end
262+
end
263+
264+
before do
265+
define_model("Article", title: :string)
266+
267+
FactoryBot.define do
268+
factory :article_with_before_callbacks, class: :article do
269+
before(:build) { TitleSetter.title = "title from before build" }
270+
after(:build) { TitleSetter.title = "title from after build" }
271+
272+
title { TitleSetter.title }
273+
end
274+
end
275+
end
276+
277+
it "runs the before callback" do
278+
article = FactoryBot.build(:article_with_before_callbacks)
279+
expect(article.title).to eq("title from before build")
280+
end
281+
end

spec/factory_bot/callback_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@
2424
FactoryBot::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two)
2525
expect(ran_with).to eq [:one, :two]
2626
end
27+
28+
it "runs run_before_build callback without attributes" do
29+
ran_with = nil
30+
FactoryBot::Callback.new(:before_build, -> { ran_with = "before build" }).run_before_build
31+
expect(ran_with).to eq "before build"
32+
end
33+
34+
it "#before_build?" do
35+
expect(FactoryBot::Callback.new(:before_build, -> {}).before_build?).to be true
36+
expect(FactoryBot::Callback.new(:after_create, -> {}).before_build?).to be false
37+
end
2738
end

0 commit comments

Comments
 (0)