Sha256: 8ef43476922750aacfac7dc18344d9280805a0dbeeee101bf6d5ed1f3e3064b0

Contents?: true

Size: 1 KB

Versions: 7

Compression:

Stored size: 1 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!
        @traits = options
      end

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

      protected

      attr_reader :options

      private

      def build
        ensure_factory_is_not_a_declaration!

        factory_name = @overrides[:factory] || name
        [Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
      end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
factory_bot-6.0.2 lib/factory_bot/declaration/association.rb
factory_bot-6.0.1 lib/factory_bot/declaration/association.rb
factory_bot-6.0.0 lib/factory_bot/declaration/association.rb
factory_bot-5.2.0 lib/factory_bot/declaration/association.rb
factory_bot-5.1.2 lib/factory_bot/declaration/association.rb
factory_bot-5.1.1 lib/factory_bot/declaration/association.rb
factory_bot-5.1.0 lib/factory_bot/declaration/association.rb