File: route_matcher.rb

package info (click to toggle)
libshoulda-ruby 2.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 880 kB
  • ctags: 726
  • sloc: ruby: 5,764; makefile: 6
file content (93 lines) | stat: -rw-r--r-- 2,769 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Shoulda # :nodoc:
  module ActionController # :nodoc:
    module Matchers

      # Ensures that requesting +path+ using +method+ routes to +options+.
      #
      # If you don't specify a controller, it will use the controller from the
      # example group.
      #
      # +to_param+ is called on the +options+ given.
      #
      # Examples:
      #
      #   it { should route(:get, "/posts").
      #                 to(:controller => :posts, :action => :index) }
      #   it { should route(:get, "/posts/new").to(:action => :new) }
      #   it { should route(:post, "/posts").to(:action => :create) }
      #   it { should route(:get, "/posts/1").to(:action => :show, :id => 1) }
      #   it { should route(:edit, "/posts/1").to(:action => :show, :id => 1) }
      #   it { should route(:put, "/posts/1").to(:action => :update, :id => 1) }
      #   it { should route(:delete, "/posts/1").
      #                 to(:action => :destroy, :id => 1) }
      #   it { should route(:get, "/users/1/posts/1").
      #                 to(:action => :show, :id => 1, :user_id => 1) }
      def route(method, path)
        RouteMatcher.new(method, path, self)
      end

      class RouteMatcher # :nodoc:

        def initialize(method, path, context)
          @method  = method
          @path    = path
          @context = context
        end

        def to(params)
          @params = params
          self
        end

        def in_context(context)
          @context = context
          self
        end

        def matches?(controller)
          @controller = controller
          guess_controller!
          stringify_params!
          route_recognized?
        end

        attr_reader :failure_message, :negative_failure_message

        def description
          "route #{@method.to_s.upcase} #{@path} to/from #{@params.inspect}"
        end

        private

        def guess_controller!
          @params[:controller] ||= @controller.controller_path
        end

        def stringify_params!
          @params.each do |key, value|
            @params[key] = value.is_a?(Array) ? value.collect {|v| v.to_param } : value.to_param
          end
        end

        def route_recognized?
          begin
            @context.send(:assert_routing, 
                          { :method => @method, :path => @path },
                          @params)

            @negative_failure_message = "Didn't expect to #{description}"
            true
          rescue ::ActionController::RoutingError => error
            @failure_message = error.message
            false
          rescue Test::Unit::AssertionFailedError => error
            @failure_message = error.message
            false
          end
        end

      end

    end
  end
end