File: controller_builder.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (67 lines) | stat: -rw-r--r-- 1,730 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module UnitTests
  module ControllerBuilder
    def self.configure_example_group(example_group)
      example_group.include(self)

      example_group.after do
        delete_temporary_views
        restore_original_routes
      end
    end

    def define_controller(class_name, &block)
      new_class_name = if class_name.to_s =~ /Controller$/
                         class_name.to_s
                       else
                         "#{class_name}Controller"
                       end

      define_class(new_class_name, ActionController::Base, &block)
    end

    def define_routes(&block)
      self.routes = $test_app.draw_routes(&block)
    end

    def build_fake_response(opts = {}, &block)
      action = opts[:action] || 'example'
      partial = opts[:partial] || '_partial'
      block ||= lambda { head :ok }
      controller_class = define_controller('Examples') do
        layout false
        define_method(action, &block)
      end
      controller_class.view_paths = [$test_app.temp_views_directory.to_s]

      define_routes do
        get 'examples', to: "examples##{action}"
      end

      create_view("examples/#{action}.html.erb", 'action')
      create_view("examples/#{partial}.html.erb", 'partial')

      setup_rails_controller_test(controller_class)
      self.class.render_views(true)

      get action

      controller
    end

    def setup_rails_controller_test(controller_class)
      @controller = controller_class.new
    end

    def create_view(path, contents)
      $test_app.create_temp_view(path, contents)
    end

    def delete_temporary_views
      $test_app.delete_temp_views
    end

    def restore_original_routes
      Rails.application.reload_routes!
    end
  end
end