Sha256: 592feff3daf8e23a4a21bbe0d313178ca56ef04c13adac58be39f7ab35de7360

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

module FactoryBot
  class Declaration
    # @api private
    class Association < Declaration
      def initialize(name, *options)
        super(name, false)
        @options = options.dup
        @overrides = options.extract_options!
        @factory_name = @overrides.delete(:factory) || name
        @traits = options
      end

      def ==(other)
        self.class == other.class &&
          name == other.name &&
          options == other.options
      end

      protected

      attr_reader :options

      private

      attr_reader :factory_name, :overrides, :traits

      def build
        raise_if_arguments_are_declarations!

        [
          Attribute::Association.new(
            name,
            factory_name,
            [traits, overrides].flatten
          )
        ]
      end

      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
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
factory_bot-6.5.0 lib/factory_bot/declaration/association.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/factory_bot-6.4.6/lib/factory_bot/declaration/association.rb
factory_bot-6.4.6 lib/factory_bot/declaration/association.rb
factory_bot-6.4.5 lib/factory_bot/declaration/association.rb
factory_bot-6.4.4 lib/factory_bot/declaration/association.rb
factory_bot-6.4.3 lib/factory_bot/declaration/association.rb
factory_bot-6.4.2 lib/factory_bot/declaration/association.rb
factory_bot-6.4.1 lib/factory_bot/declaration/association.rb
factory_bot-6.4.0 lib/factory_bot/declaration/association.rb
factory_bot-6.3.0 lib/factory_bot/declaration/association.rb
factory_bot-6.2.1 lib/factory_bot/declaration/association.rb
factory_bot-6.2.0 lib/factory_bot/declaration/association.rb
factory_bot-6.1.0 lib/factory_bot/declaration/association.rb