File: factory_validator.rb

package info (click to toggle)
ruby-factory-bot-rails 6.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: ruby: 635; makefile: 6; sh: 4
file content (39 lines) | stat: -rw-r--r-- 859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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