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!
|
2020-06-21 21:41:30 -04:00
|
|
|
@factory_name = @overrides.delete(:factory) || name
|
2012-08-02 10:19:17 -04:00
|
|
|
@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
|
|
|
|
|
2020-06-21 21:41:30 -04:00
|
|
|
attr_reader :factory_name, :overrides, :traits
|
|
|
|
|
2011-09-23 13:14:02 -04:00
|
|
|
def build
|
2020-06-21 21:58:44 -04:00
|
|
|
raise_if_arguments_are_declarations!
|
2019-04-26 11:12:05 -04:00
|
|
|
|
2020-06-21 21:41:30 -04:00
|
|
|
[
|
|
|
|
Attribute::Association.new(
|
|
|
|
name,
|
|
|
|
factory_name,
|
|
|
|
[traits, overrides].flatten
|
|
|
|
)
|
|
|
|
]
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
2019-04-26 11:12:05 -04:00
|
|
|
|
2020-06-21 21:58:44 -04:00
|
|
|
def raise_if_arguments_are_declarations!
|
2020-06-21 21:41:30 -04:00
|
|
|
if factory_name.is_a?(Declaration)
|
2019-04-26 11:12:05 -04:00
|
|
|
raise ArgumentError.new(<<~MSG)
|
|
|
|
Association '#{name}' received an invalid factory argument.
|
2020-06-21 21:41:30 -04:00
|
|
|
Did you mean? 'factory: :#{factory_name.name}'
|
2019-04-26 11:12:05 -04:00
|
|
|
MSG
|
|
|
|
end
|
2020-06-21 21:58:44 -04:00
|
|
|
|
|
|
|
overrides.each do |attribute, value|
|
|
|
|
if value.is_a?(Declaration)
|
|
|
|
raise ArgumentError.new(<<~MSG)
|
|
|
|
Association '#{name}' received an invalid attribute override.
|
|
|
|
Did you mean? '#{attribute}: :#{value.name}'
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|
2019-04-26 11:12:05 -04:00
|
|
|
end
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|