1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Use attr_readers in association declaration

This commit separates the factory name from the overrides on initialize,
then uses attr_readers throughout instead of manipulating instance
variables.

This is a bit cleaner, and will make it easier to reused the
factory_name and overrides in a future code change.
This commit is contained in:
Daniel Colson 2020-06-21 21:41:30 -04:00
parent a7379af690
commit f223c7d763

View file

@ -6,6 +6,7 @@ module FactoryBot
super(name, false)
@options = options.dup
@overrides = options.extract_options!
@factory_name = @overrides.delete(:factory) || name
@traits = options
end
@ -21,18 +22,25 @@ module FactoryBot
private
attr_reader :factory_name, :overrides, :traits
def build
ensure_factory_is_not_a_declaration!
factory_name = @overrides[:factory] || name
[Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
[
Attribute::Association.new(
name,
factory_name,
[traits, overrides].flatten
)
]
end
def ensure_factory_is_not_a_declaration!
if @overrides[:factory].is_a?(Declaration)
if factory_name.is_a?(Declaration)
raise ArgumentError.new(<<~MSG)
Association '#{name}' received an invalid factory argument.
Did you mean? 'factory: :#{@overrides[:factory].name}'
Did you mean? 'factory: :#{factory_name.name}'
MSG
end
end