Sha256: ae0227cce95b1de5ba05deecdff6a1752d6304c3693e46dd0c78052a4fc2ee20

Contents?: true

Size: 859 Bytes

Versions: 3

Compression:

Stored size: 859 Bytes

Contents

module FactoryBotRails
  class FactoryValidator
    def initialize(validators = [])
      @validators = Array(validators)
    end

    def add_validator(validator)
      @validators << validator
    end

    def run
      ActiveSupport::Notifications.subscribe("factory_bot.compile_factory", &validate_compiled_factory)
    end

    private

    def validate_compiled_factory
      if Rails.version >= "6.0"
        rails_6_0_support
      else
        rails_5_2_support
      end
    end

    def rails_6_0_support
      proc do |event|
        @validators.each { |validator| validator.validate!(event.payload) }
      end
    end

    def rails_5_2_support
      proc do |*notification_event_arguments|
        event = ActiveSupport::Notifications::Event.new(*notification_event_arguments)

        rails_6_0_support.call(event)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_bot_rails-6.4.2 lib/factory_bot_rails/factory_validator.rb
factory_bot_rails-6.4.0 lib/factory_bot_rails/factory_validator.rb
factory_bot_rails-6.3.0 lib/factory_bot_rails/factory_validator.rb