Skip to content

Commit 931283f

Browse files
authored
FactoryBot::DefinitionProxy: remove definition attr_reader (#1752)
:definition was added as an attr_reader but raises an exception when a model with an attribute called :definition is set via :method_missing. This commit replaces the :definition method with an instance variable.
1 parent bc18385 commit 931283f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/factory_bot/definition_proxy.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DefinitionProxy
2121

2222
delegate :before, :after, :callback, to: :@definition
2323

24-
attr_reader :child_factories, :definition
24+
attr_reader :child_factories
2525

2626
def initialize(definition, ignore = false)
2727
@definition = definition
@@ -121,7 +121,7 @@ def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondTo
121121
# Except that no globally available sequence will be defined.
122122
def sequence(name, *args, &block)
123123
options = args.extract_options!
124-
options[:uri_paths] = definition.uri_manager.to_a
124+
options[:uri_paths] = @definition.uri_manager.to_a
125125
args << options
126126

127127
new_sequence = Sequence.new(name, *args, &block)
@@ -177,7 +177,7 @@ def factory(name, options = {}, &block)
177177
end
178178

179179
def trait(name, &block)
180-
@definition.define_trait(Trait.new(name, uri_paths: definition.uri_manager.to_a, &block))
180+
@definition.define_trait(Trait.new(name, uri_paths: @definition.uri_manager.to_a, &block))
181181
end
182182

183183
# Creates traits for enumerable values.

spec/acceptance/defining_methods_inside_a_factory_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,19 @@ def generate_name
1717
/Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/
1818
)
1919
end
20+
21+
it "accepts a method named :definition when set through :method_missing" do
22+
define_model("User", definition: :string)
23+
24+
FactoryBot.define do
25+
factory :user do
26+
definition do
27+
"Jester"
28+
end
29+
end
30+
end
31+
32+
user = FactoryBot.build(:user)
33+
expect(user.definition).to eq("Jester")
34+
end
2035
end

0 commit comments

Comments
 (0)