class FactoryBot::Declaration::Association

@api private

Attributes

factory_name[R]
options[R]
overrides[R]
traits[R]

Public Class Methods

new(name, *options) click to toggle source
Calls superclass method FactoryBot::Declaration::new
# File lib/factory_bot/declaration/association.rb, line 5
def initialize(name, *options)
  super(name, false)
  @options = options.dup
  @overrides = options.extract_options!
  @factory_name = @overrides.delete(:factory) || name
  @traits = options
end

Public Instance Methods

==(other) click to toggle source
# File lib/factory_bot/declaration/association.rb, line 13
def ==(other)
  self.class == other.class &&
    name == other.name &&
    options == other.options
end

Private Instance Methods

build() click to toggle source
# File lib/factory_bot/declaration/association.rb, line 27
def build
  raise_if_arguments_are_declarations!

  [
    Attribute::Association.new(
      name,
      factory_name,
      [traits, overrides].flatten
    )
  ]
end
raise_if_arguments_are_declarations!() click to toggle source
# File lib/factory_bot/declaration/association.rb, line 39
      def raise_if_arguments_are_declarations!
        if factory_name.is_a?(Declaration)
          raise ArgumentError.new(<<~MSG)
            Association '#{name}' received an invalid factory argument.
            Did you mean? 'factory: :#{factory_name.name}'
          MSG
        end

        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
      end