2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2011-09-23 13:14:02 -04:00
|
|
|
class Declaration
|
2012-05-05 02:31:31 -04:00
|
|
|
# @api private
|
2011-09-23 13:14:02 -04:00
|
|
|
class Association < Declaration
|
2012-08-02 10:19:17 -04:00
|
|
|
def initialize(name, *options)
|
2011-10-07 18:19:27 -04:00
|
|
|
super(name, false)
|
2012-08-02 10:19:17 -04:00
|
|
|
@options = options.dup
|
|
|
|
@overrides = options.extract_options!
|
|
|
|
@traits = options
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
def ==(other)
|
2018-10-21 17:14:46 -04:00
|
|
|
self.class == other.class &&
|
|
|
|
name == other.name &&
|
2011-10-14 15:14:43 -04:00
|
|
|
options == other.options
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2018-09-27 21:35:05 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
attr_reader :options
|
|
|
|
|
2011-09-23 13:14:02 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def build
|
2019-04-26 11:12:05 -04:00
|
|
|
ensure_factory_is_not_a_declaration!
|
|
|
|
|
2012-08-02 10:19:17 -04:00
|
|
|
factory_name = @overrides[:factory] || name
|
|
|
|
[Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
2019-04-26 11:12:05 -04:00
|
|
|
|
|
|
|
def ensure_factory_is_not_a_declaration!
|
|
|
|
if @overrides[:factory].is_a?(Declaration)
|
|
|
|
raise ArgumentError.new(<<~MSG)
|
|
|
|
Association '#{name}' received an invalid factory argument.
|
|
|
|
Did you mean? 'factory: :#{@overrides[:factory].name}'
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|