Class: MtkFramework::ActiveInteractionMocks::MocksLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/mtk_framework/active_interaction_mocks/mocks_loader.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mtk_framework/active_interaction_mocks/mocks_loader.rb', line 6

def run
  # force require of all interactions
  Dir['app/interactions/**/*.rb'].each do |x|
    require Rails.root.join(x)
  rescue StandardError
    false
  end

  ApplicationInteraction.descendants.each do |klass|
    mocked = Class.new(ApplicationInteraction) do
      import_filters klass

      # delegate i18n translation to mocked class
      define_singleton_method :human_attribute_name do |*args|
        klass.human_attribute_name(*args)
      end

      define_singleton_method :model_name do |*args|
        klass.model_name(*args)
      end

      define_method :to_model do
        klass.to_s.split('::').first.singularize.safe_constantize&.new
      end

      private

      def execute; end
    end

    failing = Class.new(mocked) do
      private

      def execute
        errors.add(:base, 'shit happens')
      end
    end

    klass.const_set('Mocked', mocked)
    klass.const_set('Fail', failing)
  end

  require_relative 'interaction_mocks_helper.rb'
  require_relative 'interaction_group_mocks_helper.rb'
end