mapper.rb 49.0 KB
Newer Older
1
require 'erb'
2
require 'active_support/core_ext/hash/except'
3
require 'active_support/core_ext/object/blank'
4
require 'active_support/inflector'
5
require 'action_dispatch/routing/redirection'
6

J
Joshua Peek 已提交
7 8
module ActionDispatch
  module Routing
J
Joshua Peek 已提交
9
    class Mapper
10
      class Constraints #:nodoc:
11
        def self.new(app, constraints, request = Rack::Request)
12
          if constraints.any?
13
            super(app, constraints, request)
14 15 16 17 18
          else
            app
          end
        end

19 20
        attr_reader :app

21 22
        def initialize(app, constraints, request)
          @app, @constraints, @request = app, constraints, request
23 24
        end

25
        def matches?(env)
26
          req = @request.new(env)
27 28 29

          @constraints.each { |constraint|
            if constraint.respond_to?(:matches?) && !constraint.matches?(req)
30
              return false
31
            elsif constraint.respond_to?(:call) && !constraint.call(*constraint_args(constraint, req))
32
              return false
33 34 35
            end
          }

36 37 38 39 40
          return true
        end

        def call(env)
          matches?(env) ? @app.call(env) : [ 404, {'X-Cascade' => 'pass'}, [] ]
41
        end
42 43 44 45 46

        private
          def constraint_args(constraint, request)
            constraint.arity == 1 ? [request] : [request.symbolized_path_parameters, request]
          end
47 48
      end

49
      class Mapping #:nodoc:
50
        IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
51

52
        def initialize(set, scope, path, options)
53 54
          @set, @scope = set, scope
          @options = (@scope[:options] || {}).merge(options)
55
          @path = normalize_path(path)
56
          normalize_options!
57
        end
J
Joshua Peek 已提交
58

59
        def to_route
60
          [ app, conditions, requirements, defaults, @options[:as], @options[:anchor] ]
61
        end
J
Joshua Peek 已提交
62

63
        private
64 65 66

          def normalize_options!
            path_without_format = @path.sub(/\(\.:format\)$/, '')
67

68 69 70
            if using_match_shorthand?(path_without_format, @options)
              to_shorthand    = @options[:to].blank?
              @options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1')
71 72
            end

73
            @options.merge!(default_controller_and_action(to_shorthand))
74 75 76 77 78 79 80 81 82 83 84 85

            requirements.each do |name, requirement|
              # segment_keys.include?(k.to_s) || k == :controller
              next unless Regexp === requirement && !constraints[name]

              if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
                raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
              end
              if requirement.multiline?
                raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
              end
            end
86
          end
87

88
          # match "account/overview"
89
          def using_match_shorthand?(path, options)
90
            path && options.except(:via, :anchor, :to, :as).empty? && path =~ %r{^/[\w\/]+$}
91
          end
92

93
          def normalize_path(path)
94 95
            raise ArgumentError, "path is required" if path.blank?
            path = Mapper.normalize_path(path)
96 97 98 99 100 101 102 103 104 105 106

            if path.match(':controller')
              raise ArgumentError, ":controller segment is not allowed within a namespace block" if @scope[:module]

              # Add a default constraint for :controller path segments that matches namespaced
              # controllers with default routes like :controller/:action/:id(.:format), e.g:
              # GET /admin/products/show/1
              # => { :controller => 'admin/products', :action => 'show', :id => '1' }
              @options.reverse_merge!(:controller => /.+?/)
            end

107 108 109 110
            if @options[:format] == false
              @options.delete(:format)
              path
            elsif path.include?(":format")
111 112 113 114
              path
            else
              "#{path}(.:format)"
            end
115
          end
116

117 118
          def app
            Constraints.new(
119
              to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults),
120 121
              blocks,
              @set.request_class
122
            )
123 124
          end

125 126 127
          def conditions
            { :path_info => @path }.merge(constraints).merge(request_method_condition)
          end
J
Joshua Peek 已提交
128

129
          def requirements
130
            @requirements ||= (@options[:constraints].is_a?(Hash) ? @options[:constraints] : {}).tap do |requirements|
131 132 133 134
              requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints]
              @options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) }
            end
          end
135

136
          def defaults
137 138 139 140 141 142
            @defaults ||= (@options[:defaults] || {}).tap do |defaults|
              defaults.reverse_merge!(@scope[:defaults]) if @scope[:defaults]
              @options.each { |k, v| defaults[k] = v unless v.is_a?(Regexp) || IGNORE_OPTIONS.include?(k.to_sym) }
            end
          end

143
          def default_controller_and_action(to_shorthand=nil)
144
            if to.respond_to?(:call)
145 146
              { }
            else
147
              if to.is_a?(String)
148
                controller, action = to.split('#')
149 150
              elsif to.is_a?(Symbol)
                action = to.to_s
151
              end
J
Joshua Peek 已提交
152

153 154
              controller ||= default_controller
              action     ||= default_action
155

156 157 158
              unless controller.is_a?(Regexp) || to_shorthand
                controller = [@scope[:module], controller].compact.join("/").presence
              end
159

160 161 162 163
              if controller.is_a?(String) && controller =~ %r{\A/}
                raise ArgumentError, "controller name should not start with a slash"
              end

164 165
              controller = controller.to_s unless controller.is_a?(Regexp)
              action     = action.to_s     unless action.is_a?(Regexp)
166

167
              if controller.blank? && segment_keys.exclude?("controller")
168 169
                raise ArgumentError, "missing :controller"
              end
J
Joshua Peek 已提交
170

171
              if action.blank? && segment_keys.exclude?("action")
172 173
                raise ArgumentError, "missing :action"
              end
J
Joshua Peek 已提交
174

A
Aaron Patterson 已提交
175
              hash = {}
A
Aaron Patterson 已提交
176 177
              hash[:controller] = controller unless controller.blank?
              hash[:action]     = action unless action.blank?
A
Aaron Patterson 已提交
178
              hash
179 180
            end
          end
181

182
          def blocks
A
Aaron Patterson 已提交
183 184
            block = @scope[:blocks] || []

185
            if @options[:constraints].present? && !@options[:constraints].is_a?(Hash)
A
Aaron Patterson 已提交
186
              block << @options[:constraints]
187
            end
J
Joshua Peek 已提交
188

A
Aaron Patterson 已提交
189
            block
190
          end
J
Joshua Peek 已提交
191

192 193 194
          def constraints
            @constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller }
          end
195

196 197
          def request_method_condition
            if via = @options[:via]
198 199
              via = Array(via).map { |m| m.to_s.dasherize.upcase }
              { :request_method => %r[^#{via.join('|')}$] }
200 201
            else
              { }
202
            end
203
          end
J
Joshua Peek 已提交
204

205 206
          def segment_keys
            @segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new(
207 208
              Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS)
            ).names
209
          end
210

211 212 213
          def to
            @options[:to]
          end
J
Joshua Peek 已提交
214

215
          def default_controller
216
            if @options[:controller]
217
              @options[:controller]
218
            elsif @scope[:controller]
219
              @scope[:controller]
220
            end
221
          end
222 223 224

          def default_action
            if @options[:action]
225
              @options[:action]
226 227
            elsif @scope[:action]
              @scope[:action]
228 229
            end
          end
230
      end
231

232
      # Invokes Rack::Mount::Utils.normalize path and ensure that
233 234
      # (:locale) becomes (/:locale) instead of /(:locale). Except
      # for root cases, where the latter is the correct one.
235 236
      def self.normalize_path(path)
        path = Rack::Mount::Utils.normalize_path(path)
237
        path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^/]+\)$}
238 239 240
        path
      end

241 242 243 244
      def self.normalize_name(name)
        normalize_path(name)[1..-1].gsub("/", "_")
      end

245
      module Base
246
        def initialize(set) #:nodoc:
247 248
          @set = set
        end
249

250 251 252 253
        # You can specify what Rails should route "/" to with the root method:
        #
        #   root :to => 'pages#main'
        #
254
        # For options, see +match+, as +root+ uses it internally.
255
        #
256 257 258
        # You should put the root route at the top of <tt>config/routes.rb</tt>,
        # because this means it will be matched first. As this is the most popular route
        # of most Rails applications, this is beneficial.
259 260 261
        def root(options = {})
          match '/', options.reverse_merge(:as => :root)
        end
262

263 264 265
        # Matches a url pattern to one or more routes. Any symbols in a pattern
        # are interpreted as url query parameters and thus available as +params+
        # in an action:
266
        #
267
        #   # sets :controller, :action and :id in params
268
        #   match ':controller/:action/:id'
269
        #
270 271 272 273 274 275 276 277 278 279 280 281
        # Two of these symbols are special, +:controller+ maps to the controller
        # and +:action+ to the controller's action. A pattern can also map
        # wildcard segments (globs) to params:
        #
        #   match 'songs/*category/:title' => 'songs#show'
        #
        #   # 'songs/rock/classic/stairway-to-heaven' sets
        #   #  params[:category] = 'rock/classic'
        #   #  params[:title] = 'stairway-to-heaven'
        #
        # When a pattern points to an internal route, the route's +:action+ and
        # +:controller+ should be set in options or hash shorthand. Examples:
282 283 284 285
        #
        #   match 'photos/:id' => 'photos#show'
        #   match 'photos/:id', :to => 'photos#show'
        #   match 'photos/:id', :controller => 'photos', :action => 'show'
286
        #
287 288 289 290 291 292 293 294
        # A pattern can also point to a +Rack+ endpoint i.e. anything that
        # responds to +call+:
        #
        #   match 'photos/:id' => lambda {|hash| [200, {}, "Coming soon" }
        #   match 'photos/:id' => PhotoRackApp
        #   # Yes, controller actions are just rack endpoints
        #   match 'photos/:id' => PhotosController.action(:show)
        #
295
        # === Options
296
        #
297
        # Any options not seen here are passed on as params with the url.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        #
        # [:controller]
        #   The route's controller.
        #
        # [:action]
        #   The route's action.
        #
        # [:path]
        #   The path prefix for the routes.
        #
        # [:module]
        #   The namespace for :controller.
        #
        #     match 'path' => 'c#a', :module => 'sekret', :controller => 'posts'
        #     #=> Sekret::PostsController
        #
        #   See <tt>Scoping#namespace</tt> for its scope equivalent.
        #
        # [:as]
        #   The name used to generate routing helpers.
        #
        # [:via]
        #   Allowed HTTP verb(s) for route.
        #
        #      match 'path' => 'c#a', :via => :get
        #      match 'path' => 'c#a', :via => [:get, :post]
        #
        # [:to]
326 327
        #   Points to a +Rack+ endpoint. Can be an object that responds to
        #   +call+ or a string representing a controller's action.
328
        #
329 330 331
        #      match 'path', :to => 'controller#action'
        #      match 'path', :to => lambda { [200, {}, "Success!"] }
        #      match 'path', :to => RackApp
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        #
        # [:on]
        #   Shorthand for wrapping routes in a specific RESTful context. Valid
        #   values are :member, :collection, and :new.  Only use within
        #   <tt>resource(s)</tt> block. For example:
        #
        #      resource :bar do
        #        match 'foo' => 'c#a', :on => :member, :via => [:get, :post]
        #      end
        #
        #   Is equivalent to:
        #
        #      resource :bar do
        #        member do
        #          match 'foo' => 'c#a', :via => [:get, :post]
        #        end
        #      end
        #
        # [:constraints]
        #   Constrains parameters with a hash of regular expressions or an
        #   object that responds to #matches?
        #
        #     match 'path/:id', :constraints => { :id => /[A-Z]\d{5}/ }
        #
        #     class Blacklist
        #       def matches?(request) request.remote_ip == '1.2.3.4' end
        #     end
        #     match 'path' => 'c#a', :constraints => Blacklist.new
        #
        #   See <tt>Scoping#constraints</tt> for more examples with its scope
        #   equivalent.
        #
        # [:defaults]
        #   Sets defaults for parameters
        #
        #     # Sets params[:format] to 'jpg' by default
        #     match 'path' => 'c#a', :defaults => { :format => 'jpg' }
        #
        #   See <tt>Scoping#defaults</tt> for its scope equivalent.
371 372
        def match(path, options=nil)
          mapping = Mapping.new(@set, @scope, path, options || {}).to_route
373
          @set.add_route(*mapping)
374 375
          self
        end
376

377 378
        # Mount a Rack-based application to be used within the application.
        #
R
Ryan Bigg 已提交
379
        #   mount SomeRackApp, :at => "some_route"
380 381 382
        #
        # Alternatively:
        #
R
Ryan Bigg 已提交
383
        #   mount(SomeRackApp => "some_route")
384
        #
385 386
        # For options, see +match+, as +mount+ uses it internally.
        #
387 388 389 390 391
        # All mounted applications come with routing helpers to access them.
        # These are named after the class specified, so for the above example
        # the helper is either +some_rack_app_path+ or +some_rack_app_url+.
        # To customize this helper's name, use the +:as+ option:
        #
R
Ryan Bigg 已提交
392
        #   mount(SomeRackApp => "some_route", :as => "exciting")
393 394 395
        #
        # This will generate the +exciting_path+ and +exciting_url+ helpers
        # which can be used to navigate to this mounted app.
396 397 398 399 400 401 402 403 404 405 406
        def mount(app, options = nil)
          if options
            path = options.delete(:at)
          else
            options = app
            app, path = options.find { |k, v| k.respond_to?(:call) }
            options.delete(app) if app
          end

          raise "A rack application must be specified" unless path

407 408
          options[:as] ||= app_name(app)

409
          match(path, options.merge(:to => app, :anchor => false, :format => false))
410 411

          define_generate_prefix(app, options[:as])
412 413 414
          self
        end

415 416 417 418
        def default_url_options=(options)
          @set.default_url_options = options
        end
        alias_method :default_url_options, :default_url_options=
419

420 421 422 423 424 425
        def with_default_scope(scope, &block)
          scope(scope) do
            instance_exec(&block)
          end
        end

426 427 428
        private
          def app_name(app)
            return unless app.respond_to?(:routes)
429 430 431 432 433 434 435

            if app.respond_to?(:railtie_name)
              app.railtie_name
            else
              class_name = app.class.is_a?(Class) ? app.name : app.class.name
              ActiveSupport::Inflector.underscore(class_name).gsub("/", "_")
            end
436 437 438
          end

          def define_generate_prefix(app, name)
439
            return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper)
440 441

            _route = @set.named_routes.routes[name.to_sym]
P
Piotr Sarnacki 已提交
442 443
            _routes = @set
            app.routes.define_mounted_helper(name)
444 445
            app.routes.class_eval do
              define_method :_generate_prefix do |options|
P
Piotr Sarnacki 已提交
446
                prefix_options = options.slice(*_route.segment_keys)
447 448
                # we must actually delete prefix segment keys to avoid passing them to next url_for
                _route.segment_keys.each { |k| options.delete(k) }
P
Piotr Sarnacki 已提交
449
                _routes.url_helpers.send("#{name}_path", prefix_options)
450 451 452
              end
            end
          end
453 454 455
      end

      module HttpHelpers
456
        # Define a route that only recognizes HTTP GET.
457
        # For supported arguments, see <tt>Base#match</tt>.
458 459 460 461
        #
        # Example:
        #
        # get 'bacon', :to => 'food#bacon'
462 463 464 465
        def get(*args, &block)
          map_method(:get, *args, &block)
        end

466
        # Define a route that only recognizes HTTP POST.
467
        # For supported arguments, see <tt>Base#match</tt>.
468 469 470 471
        #
        # Example:
        #
        # post 'bacon', :to => 'food#bacon'
472 473 474 475
        def post(*args, &block)
          map_method(:post, *args, &block)
        end

476
        # Define a route that only recognizes HTTP PUT.
477
        # For supported arguments, see <tt>Base#match</tt>.
478 479 480 481
        #
        # Example:
        #
        # put 'bacon', :to => 'food#bacon'
482 483 484 485
        def put(*args, &block)
          map_method(:put, *args, &block)
        end

486
        # Define a route that only recognizes HTTP PUT.
487
        # For supported arguments, see <tt>Base#match</tt>.
488 489 490 491
        #
        # Example:
        #
        # delete 'broccoli', :to => 'food#broccoli'
492 493 494 495 496 497 498 499 500 501 502 503 504 505
        def delete(*args, &block)
          map_method(:delete, *args, &block)
        end

        private
          def map_method(method, *args, &block)
            options = args.extract_options!
            options[:via] = method
            args.push(options)
            match(*args, &block)
            self
          end
      end

506 507 508 509 510 511 512 513 514
      # You may wish to organize groups of controllers under a namespace.
      # Most commonly, you might group a number of administrative controllers
      # under an +admin+ namespace. You would place these controllers under
      # the app/controllers/admin directory, and you can group them together
      # in your router:
      #
      #   namespace "admin" do
      #     resources :posts, :comments
      #   end
515
      #
516 517
      # This will create a number of routes for each of the posts and comments
      # controller. For Admin::PostsController, Rails will create:
518
      #
519 520 521 522 523 524 525
      #   GET	    /admin/posts
      #   GET	    /admin/posts/new
      #   POST	  /admin/posts
      #   GET	    /admin/posts/1
      #   GET	    /admin/posts/1/edit
      #   PUT	    /admin/posts/1
      #   DELETE  /admin/posts/1
526
      #
527
      # If you want to route /posts (without the prefix /admin) to
528
      # Admin::PostsController, you could use
529
      #
530
      #   scope :module => "admin" do
531
      #     resources :posts
532 533 534
      #   end
      #
      # or, for a single case
535
      #
536
      #   resources :posts, :module => "admin"
537
      #
538
      # If you want to route /admin/posts to PostsController
539
      # (without the Admin:: module prefix), you could use
540
      #
541
      #   scope "/admin" do
542
      #     resources :posts
543 544 545
      #   end
      #
      # or, for a single case
546
      #
547 548 549 550 551
      #   resources :posts, :path => "/admin"
      #
      # In each of these cases, the named routes remain the same as if you did
      # not use scope. In the last case, the following paths map to
      # PostsController:
552
      #
553 554 555 556 557 558 559
      #   GET	    /admin/posts
      #   GET	    /admin/posts/new
      #   POST	  /admin/posts
      #   GET	    /admin/posts/1
      #   GET	    /admin/posts/1/edit
      #   PUT	    /admin/posts/1
      #   DELETE  /admin/posts/1
560
      module Scoping
561
        def initialize(*args) #:nodoc:
562 563 564 565
          @scope = {}
          super
        end

566
        # Scopes a set of routes to the given default options.
567 568 569 570 571 572 573 574 575 576 577
        #
        # Take the following route definition as an example:
        #
        #   scope :path => ":account_id", :as => "account" do
        #     resources :projects
        #   end
        #
        # This generates helpers such as +account_projects_path+, just like +resources+ does.
        # The difference here being that the routes generated are like /rails/projects/2,
        # rather than /accounts/rails/projects/2.
        #
578
        # === Options
579
        #
580
        # Takes same options as <tt>Base#match</tt> and <tt>Resources#resources</tt>.
581
        #
582
        # === Examples
583
        #
584 585 586 587
        #   # route /posts (without the prefix /admin) to Admin::PostsController
        #   scope :module => "admin" do
        #     resources :posts
        #   end
588
        #
589 590 591 592
        #   # prefix the posts resource's requests with '/admin'
        #   scope :path => "/admin" do
        #     resources :posts
        #   end
593
        #
594 595 596 597
        #   # prefix the routing helper name: sekret_posts_path instead of posts_path
        #   scope :as => "sekret" do
        #     resources :posts
        #   end
598 599
        def scope(*args)
          options = args.extract_options!
600
          options = options.dup
601

602
          options[:path] = args.first if args.first.is_a?(String)
603
          recover = {}
604

605 606 607
          options[:constraints] ||= {}
          unless options[:constraints].is_a?(Hash)
            block, options[:constraints] = options[:constraints], {}
608
          end
609

610 611 612 613 614
          scope_options.each do |option|
            if value = options.delete(option)
              recover[option] = @scope[option]
              @scope[option]  = send("merge_#{option}_scope", @scope[option], value)
            end
615 616
          end

617 618
          recover[:block] = @scope[:blocks]
          @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block)
619

620 621
          recover[:options] = @scope[:options]
          @scope[:options]  = merge_options_scope(@scope[:options], options)
622 623 624 625

          yield
          self
        ensure
626 627 628 629 630 631
          scope_options.each do |option|
            @scope[option] = recover[option] if recover.has_key?(option)
          end

          @scope[:options] = recover[:options]
          @scope[:blocks]  = recover[:block]
632 633
        end

634 635 636 637 638 639
        # Scopes routes to a specific controller
        #
        # Example:
        #   controller "food" do
        #     match "bacon", :action => "bacon"
        #   end
640 641 642
        def controller(controller, options={})
          options[:controller] = controller
          scope(options) { yield }
643 644
        end

645 646 647 648 649 650 651 652
        # Scopes routes to a specific namespace. For example:
        #
        #   namespace :admin do
        #     resources :posts
        #   end
        #
        # This generates the following routes:
        #
653 654 655 656 657 658 659
        #       admin_posts GET    /admin/posts(.:format)          {:action=>"index", :controller=>"admin/posts"}
        #       admin_posts POST   /admin/posts(.:format)          {:action=>"create", :controller=>"admin/posts"}
        #    new_admin_post GET    /admin/posts/new(.:format)      {:action=>"new", :controller=>"admin/posts"}
        #   edit_admin_post GET    /admin/posts/:id/edit(.:format) {:action=>"edit", :controller=>"admin/posts"}
        #        admin_post GET    /admin/posts/:id(.:format)      {:action=>"show", :controller=>"admin/posts"}
        #        admin_post PUT    /admin/posts/:id(.:format)      {:action=>"update", :controller=>"admin/posts"}
        #        admin_post DELETE /admin/posts/:id(.:format)      {:action=>"destroy", :controller=>"admin/posts"}
660
        #
661
        # === Options
662
        #
663 664
        # The +:path+, +:as+, +:module+, +:shallow_path+ and +:shallow_prefix+
        # options all default to the name of the namespace.
665
        #
666 667
        # For options, see <tt>Base#match</tt>. For +:shallow_path+ option, see
        # <tt>Resources#resources</tt>.
668
        #
669
        # === Examples
670
        #
671 672 673 674
        #   # accessible through /sekret/posts rather than /admin/posts
        #   namespace :admin, :path => "sekret" do
        #     resources :posts
        #   end
675
        #
676 677 678 679
        #   # maps to Sekret::PostsController rather than Admin::PostsController
        #   namespace :admin, :module => "sekret" do
        #     resources :posts
        #   end
680
        #
681 682 683 684
        #   # generates sekret_posts_path rather than admin_posts_path
        #   namespace :admin, :as => "sekret" do
        #     resources :posts
        #   end
685
        def namespace(path, options = {})
686
          path = path.to_s
687 688 689
          options = { :path => path, :as => path, :module => path,
                      :shallow_path => path, :shallow_prefix => path }.merge!(options)
          scope(options) { yield }
690
        end
691

R
Ryan Bigg 已提交
692 693 694 695 696 697 698 699 700 701
        # === Parameter Restriction
        # Allows you to constrain the nested routes based on a set of rules.
        # For instance, in order to change the routes to allow for a dot character in the +id+ parameter:
        #
        #   constraints(:id => /\d+\.\d+) do
        #     resources :posts
        #   end
        #
        # Now routes such as +/posts/1+ will no longer be valid, but +/posts/1.1+ will be.
        # The +id+ parameter must match the constraint passed in for this example.
702
        #
R
Ryan Bigg 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
        # You may use this to also resrict other parameters:
        #
        #   resources :posts do
        #     constraints(:post_id => /\d+\.\d+) do
        #       resources :comments
        #     end
        #
        # === Restricting based on IP
        #
        # Routes can also be constrained to an IP or a certain range of IP addresses:
        #
        #   constraints(:ip => /192.168.\d+.\d+/) do
        #     resources :posts
        #   end
        #
        # Any user connecting from the 192.168.* range will be able to see this resource,
        # where as any user connecting outside of this range will be told there is no such route.
        #
        # === Dynamic request matching
        #
        # Requests to routes can be constrained based on specific critera:
        #
        #    constraints(lambda { |req| req.env["HTTP_USER_AGENT"] =~ /iPhone/ }) do
        #      resources :iphones
        #    end
        #
        # You are able to move this logic out into a class if it is too complex for routes.
        # This class must have a +matches?+ method defined on it which either returns +true+
        # if the user should be given access to that route, or +false+ if the user should not.
        #
        #    class Iphone
        #      def self.matches(request)
        #        request.env["HTTP_USER_AGENT"] =~ /iPhone/
        #      end
        #    end
        #
        # An expected place for this code would be +lib/constraints+.
        #
        # This class is then used like this:
        #
        #    constraints(Iphone) do
        #      resources :iphones
        #    end
746 747 748 749
        def constraints(constraints = {})
          scope(:constraints => constraints) { yield }
        end

R
Ryan Bigg 已提交
750
        # Allows you to set default parameters for a route, such as this:
751 752 753
        #   defaults :id => 'home' do
        #     match 'scoped_pages/(:id)', :to => 'pages#show'
        #   end
R
Ryan Bigg 已提交
754
        # Using this, the +:id+ parameter here will default to 'home'.
755 756 757 758
        def defaults(defaults = {})
          scope(:defaults => defaults) { yield }
        end

759
        private
J
José Valim 已提交
760
          def scope_options #:nodoc:
761 762 763
            @scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
          end

J
José Valim 已提交
764
          def merge_path_scope(parent, child) #:nodoc:
765
            Mapper.normalize_path("#{parent}/#{child}")
766 767
          end

J
José Valim 已提交
768
          def merge_shallow_path_scope(parent, child) #:nodoc:
769 770 771
            Mapper.normalize_path("#{parent}/#{child}")
          end

J
José Valim 已提交
772
          def merge_as_scope(parent, child) #:nodoc:
773
            parent ? "#{parent}_#{child}" : child
774 775
          end

J
José Valim 已提交
776
          def merge_shallow_prefix_scope(parent, child) #:nodoc:
777 778 779
            parent ? "#{parent}_#{child}" : child
          end

J
José Valim 已提交
780
          def merge_module_scope(parent, child) #:nodoc:
781 782 783
            parent ? "#{parent}/#{child}" : child
          end

J
José Valim 已提交
784
          def merge_controller_scope(parent, child) #:nodoc:
785
            child
786 787
          end

J
José Valim 已提交
788
          def merge_path_names_scope(parent, child) #:nodoc:
789 790 791
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
792
          def merge_constraints_scope(parent, child) #:nodoc:
793 794 795
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
796
          def merge_defaults_scope(parent, child) #:nodoc:
797 798 799
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
800
          def merge_blocks_scope(parent, child) #:nodoc:
801 802 803
            merged = parent ? parent.dup : []
            merged << child if child
            merged
804 805
          end

J
José Valim 已提交
806
          def merge_options_scope(parent, child) #:nodoc:
807
            (parent || {}).except(*override_keys(child)).merge(child)
808
          end
809

J
José Valim 已提交
810
          def merge_shallow_scope(parent, child) #:nodoc:
811 812
            child ? true : false
          end
813

J
José Valim 已提交
814
          def override_keys(child) #:nodoc:
815 816
            child.key?(:only) || child.key?(:except) ? [:only, :except] : []
          end
817 818
      end

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
      # Resource routing allows you to quickly declare all of the common routes
      # for a given resourceful controller. Instead of declaring separate routes
      # for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+
      # actions, a resourceful route declares them in a single line of code:
      #
      #  resources :photos
      #
      # Sometimes, you have a resource that clients always look up without
      # referencing an ID. A common example, /profile always shows the profile of
      # the currently logged in user. In this case, you can use a singular resource
      # to map /profile (rather than /profile/:id) to the show action.
      #
      #  resource :profile
      #
      # It's common to have resources that are logically children of other
      # resources:
      #
      #   resources :magazines do
      #     resources :ads
      #   end
      #
      # You may wish to organize groups of controllers under a namespace. Most
      # commonly, you might group a number of administrative controllers under
      # an +admin+ namespace. You would place these controllers under the
      # app/controllers/admin directory, and you can group them together in your
      # router:
      #
      #   namespace "admin" do
      #     resources :posts, :comments
      #   end
      #
850 851 852 853 854 855 856 857
      # By default the :id parameter doesn't accept dots. If you need to
      # use dots as part of the :id parameter add a constraint which
      # overrides this restriction, e.g:
      #
      #   resources :articles, :id => /[^\/]+/
      #
      # This allows any character other than a slash as part of your :id.
      #
J
Joshua Peek 已提交
858
      module Resources
859 860
        # CANONICAL_ACTIONS holds all actions that does not need a prefix or
        # a path appended since they fit properly in their scope level.
861 862 863
        VALID_ON_OPTIONS  = [:new, :collection, :member]
        RESOURCE_OPTIONS  = [:as, :controller, :path, :only, :except]
        CANONICAL_ACTIONS = %w(index create new show update destroy)
864

865
        class Resource #:nodoc:
866
          DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
867

868
          attr_reader :controller, :path, :options
869 870

          def initialize(entities, options = {})
871
            @name       = entities.to_s
872
            @path       = (options.delete(:path) || @name).to_s
873
            @controller = (options.delete(:controller) || @name).to_s
874
            @as         = options.delete(:as)
875
            @options    = options
876 877
          end

878
          def default_actions
879
            self.class::DEFAULT_ACTIONS
880 881
          end

882
          def actions
883
            if only = @options[:only]
884
              Array(only).map(&:to_sym)
885
            elsif except = @options[:except]
886 887 888 889 890 891
              default_actions - Array(except).map(&:to_sym)
            else
              default_actions
            end
          end

892
          def name
893
            @as || @name
894 895
          end

896
          def plural
897
            @plural ||= name.to_s
898 899 900
          end

          def singular
901
            @singular ||= name.to_s.singularize
902 903
          end

904
          alias :member_name :singular
905

906 907
          # Checks for uncountable plurals, and appends "_index" if the plural 
          # and singular form are the same.
908
          def collection_name
909
            singular == plural ? "#{plural}_index" : plural
910 911
          end

912
          def resource_scope
913
            { :controller => controller }
914 915
          end

916
          alias :collection_scope :path
917 918

          def member_scope
919
            "#{path}/:id"
920 921
          end

922
          def new_scope(new_path)
923
            "#{path}/#{new_path}"
924 925 926
          end

          def nested_scope
927
            "#{path}/:#{singular}_id"
928
          end
929

930 931 932
        end

        class SingletonResource < Resource #:nodoc:
933
          DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit]
934

935
          def initialize(entities, options)
936
            @as         = nil
937
            @name       = entities.to_s
938
            @path       = (options.delete(:path) || @name).to_s
939
            @controller = (options.delete(:controller) || plural).to_s
940 941 942 943
            @as         = options.delete(:as)
            @options    = options
          end

944 945
          def plural
            @plural ||= name.to_s.pluralize
946 947
          end

948 949
          def singular
            @singular ||= name.to_s
950
          end
951 952 953 954 955 956

          alias :member_name :singular
          alias :collection_name :singular

          alias :member_scope :path
          alias :nested_scope :path
957 958
        end

959
        def initialize(*args) #:nodoc:
960
          super
961
          @scope[:path_names] = @set.resources_path_names
962 963
        end

964 965 966 967
        def resources_path_names(options)
          @scope[:path_names].merge!(options)
        end

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
        # Sometimes, you have a resource that clients always look up without
        # referencing an ID. A common example, /profile always shows the
        # profile of the currently logged in user. In this case, you can use
        # a singular resource to map /profile (rather than /profile/:id) to
        # the show action:
        #
        #   resource :geocoder
        #
        # creates six different routes in your application, all mapping to
        # the GeoCoders controller (note that the controller is named after
        # the plural):
        #
        #   GET     /geocoder/new
        #   POST    /geocoder
        #   GET     /geocoder
        #   GET     /geocoder/edit
        #   PUT     /geocoder
        #   DELETE  /geocoder
986
        #
987
        # === Options
988
        # Takes same options as +resources+.
J
Joshua Peek 已提交
989
        def resource(*resources, &block)
J
Joshua Peek 已提交
990
          options = resources.extract_options!
J
Joshua Peek 已提交
991

992
          if apply_common_behavior_for(:resource, resources, options, &block)
993 994 995
            return self
          end

996 997
          resource_scope(SingletonResource.new(resources.pop, options)) do
            yield if block_given?
998

999
            collection do
1000
              post :create
1001
            end if parent_resource.actions.include?(:create)
1002

1003
            new do
1004
              get :new
1005
            end if parent_resource.actions.include?(:new)
1006

1007
            member do
1008
              get    :edit if parent_resource.actions.include?(:edit)
1009 1010 1011
              get    :show if parent_resource.actions.include?(:show)
              put    :update if parent_resource.actions.include?(:update)
              delete :destroy if parent_resource.actions.include?(:destroy)
1012 1013 1014
            end
          end

J
Joshua Peek 已提交
1015
          self
1016 1017
        end

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        # In Rails, a resourceful route provides a mapping between HTTP verbs
        # and URLs and controller actions. By convention, each action also maps
        # to particular CRUD operations in a database. A single entry in the
        # routing file, such as
        #
        #   resources :photos
        #
        # creates seven different routes in your application, all mapping to
        # the Photos controller:
        #
        #   GET     /photos/new
        #   POST    /photos
        #   GET     /photos/:id
        #   GET     /photos/:id/edit
        #   PUT     /photos/:id
        #   DELETE  /photos/:id
1034
        #
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
        # Resources can also be nested infinitely by using this block syntax:
        #
        #   resources :photos do
        #     resources :comments
        #   end
        #
        # This generates the following comments routes:
        #
        #   GET     /photos/:id/comments/new
        #   POST    /photos/:id/comments
        #   GET     /photos/:id/comments/:id
        #   GET     /photos/:id/comments/:id/edit
        #   PUT     /photos/:id/comments/:id
        #   DELETE  /photos/:id/comments/:id
        #
1050
        # === Options
1051 1052
        # Takes same options as <tt>Base#match</tt> as well as:
        #
1053 1054 1055 1056 1057 1058 1059
        # [:path_names]
        #   Allows you to change the paths of the seven default actions.
        #   Paths not specified are not changed.
        #
        #     resources :posts, :path_names => { :new => "brand_new" }
        #
        #   The above example will now change /posts/new to /posts/brand_new
1060
        #
1061 1062
        # [:only]
        #   Only generate routes for the given actions.
1063
        #
1064 1065
        #     resources :cows, :only => :show
        #     resources :cows, :only => [:show, :index]
1066
        #
1067 1068
        # [:except]
        #   Generate all routes except for the given actions.
1069
        #
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        #     resources :cows, :except => :show
        #     resources :cows, :except => [:show, :index]
        #
        # [:shallow]
        #   Generates shallow routes for nested resource(s). When placed on a parent resource,
        #   generates shallow routes for all nested resources.
        #
        #     resources :posts, :shallow => true do
        #       resources :comments
        #     end
        #
        #   Is the same as:
        #
        #     resources :posts do
        #       resources :comments
        #     end
        #     resources :comments
        #
        # [:shallow_path]
        #   Prefixes nested shallow routes with the specified path.
        #
        #   scope :shallow_path => "sekret" do
        #     resources :posts do
        #       resources :comments, :shallow => true
        #     end
        #   end
        #
        #   The +comments+ resource here will have the following routes generated for it:
        #
        #     post_comments    GET    /sekret/posts/:post_id/comments(.:format)
        #     post_comments    POST   /sekret/posts/:post_id/comments(.:format)
        #     new_post_comment GET    /sekret/posts/:post_id/comments/new(.:format)
        #     edit_comment     GET    /sekret/comments/:id/edit(.:format)
        #     comment          GET    /sekret/comments/:id(.:format)
        #     comment          PUT    /sekret/comments/:id(.:format)
        #     comment          DELETE /sekret/comments/:id(.:format)
        #
        # === Examples
1108
        #
1109 1110
        #   # routes call Admin::PostsController
        #   resources :posts, :module => "admin"
1111
        #
1112 1113
        #   # resource actions are at /admin/posts.
        #   resources :posts, :path => "admin"
J
Joshua Peek 已提交
1114
        def resources(*resources, &block)
J
Joshua Peek 已提交
1115
          options = resources.extract_options!
1116

1117
          if apply_common_behavior_for(:resources, resources, options, &block)
1118 1119 1120
            return self
          end

1121
          resource_scope(Resource.new(resources.pop, options)) do
1122
            yield if block_given?
J
Joshua Peek 已提交
1123

1124
            collection do
1125 1126
              get  :index if parent_resource.actions.include?(:index)
              post :create if parent_resource.actions.include?(:create)
1127
            end
1128

1129
            new do
1130
              get :new
1131
            end if parent_resource.actions.include?(:new)
1132

1133
            member do
1134
              get    :edit if parent_resource.actions.include?(:edit)
1135 1136 1137
              get    :show if parent_resource.actions.include?(:show)
              put    :update if parent_resource.actions.include?(:update)
              delete :destroy if parent_resource.actions.include?(:destroy)
1138 1139 1140
            end
          end

J
Joshua Peek 已提交
1141
          self
1142 1143
        end

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
        # To add a route to the collection:
        #
        #   resources :photos do
        #     collection do
        #       get 'search'
        #     end
        #   end
        #
        # This will enable Rails to recognize paths such as <tt>/photos/search</tt>
        # with GET, and route to the search action of PhotosController. It will also
        # create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt>
        # route helpers.
J
Joshua Peek 已提交
1156
        def collection
1157 1158
          unless resource_scope?
            raise ArgumentError, "can't use collection outside resource(s) scope"
1159 1160
          end

1161 1162 1163 1164
          with_scope_level(:collection) do
            scope(parent_resource.collection_scope) do
              yield
            end
J
Joshua Peek 已提交
1165
          end
1166
        end
J
Joshua Peek 已提交
1167

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        # To add a member route, add a member block into the resource block:
        #
        #   resources :photos do
        #     member do
        #       get 'preview'
        #     end
        #   end
        #
        # This will recognize <tt>/photos/1/preview</tt> with GET, and route to the
        # preview action of PhotosController. It will also create the
        # <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.
J
Joshua Peek 已提交
1179
        def member
1180 1181
          unless resource_scope?
            raise ArgumentError, "can't use member outside resource(s) scope"
J
Joshua Peek 已提交
1182
          end
J
Joshua Peek 已提交
1183

1184 1185 1186 1187
          with_scope_level(:member) do
            scope(parent_resource.member_scope) do
              yield
            end
1188 1189 1190 1191 1192 1193 1194
          end
        end

        def new
          unless resource_scope?
            raise ArgumentError, "can't use new outside resource(s) scope"
          end
1195

1196 1197 1198 1199
          with_scope_level(:new) do
            scope(parent_resource.new_scope(action_path(:new))) do
              yield
            end
J
Joshua Peek 已提交
1200
          end
J
Joshua Peek 已提交
1201 1202
        end

1203
        def nested
1204 1205
          unless resource_scope?
            raise ArgumentError, "can't use nested outside resource(s) scope"
1206 1207 1208
          end

          with_scope_level(:nested) do
1209
            if shallow?
1210
              with_exclusive_scope do
1211
                if @scope[:shallow_path].blank?
1212
                  scope(parent_resource.nested_scope, nested_options) { yield }
1213
                else
1214
                  scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
1215
                    scope(parent_resource.nested_scope, nested_options) { yield }
1216 1217 1218 1219
                  end
                end
              end
            else
1220
              scope(parent_resource.nested_scope, nested_options) { yield }
1221 1222 1223 1224
            end
          end
        end

1225
        # See ActionDispatch::Routing::Mapper::Scoping#namespace
1226
        def namespace(path, options = {})
1227
          if resource_scope?
1228 1229 1230 1231 1232 1233
            nested { super }
          else
            super
          end
        end

1234
        def shallow
1235
          scope(:shallow => true, :shallow_path => @scope[:path]) do
1236 1237 1238 1239
            yield
          end
        end

1240 1241 1242 1243
        def shallow?
          parent_resource.instance_of?(Resource) && @scope[:shallow]
        end

J
Joshua Peek 已提交
1244
        def match(*args)
1245
          options = args.extract_options!.dup
1246 1247
          options[:anchor] = true unless options.key?(:anchor)

1248
          if args.length > 1
1249
            args.each { |path| match(path, options.dup) }
1250 1251 1252
            return self
          end

1253 1254
          on = options.delete(:on)
          if VALID_ON_OPTIONS.include?(on)
1255
            args.push(options)
1256 1257 1258
            return send(on){ match(*args) }
          elsif on
            raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
1259 1260
          end

1261 1262 1263 1264
          if @scope[:scope_level] == :resources
            args.push(options)
            return nested { match(*args) }
          elsif @scope[:scope_level] == :resource
1265
            args.push(options)
J
Joshua Peek 已提交
1266 1267
            return member { match(*args) }
          end
J
Joshua Peek 已提交
1268

1269
          action = args.first
1270
          path = path_for_action(action, options.delete(:path))
1271

1272 1273 1274
          if action.to_s =~ /^[\w\/]+$/
            options[:action] ||= action unless action.to_s.include?("/")
          else
1275 1276 1277 1278 1279 1280 1281
            action = nil
          end

          if options.key?(:as) && !options[:as]
            options.delete(:as)
          else
            options[:as] = name_for_action(options[:as], action)
J
Joshua Peek 已提交
1282
          end
J
Joshua Peek 已提交
1283

1284
          super(path, options)
J
Joshua Peek 已提交
1285 1286
        end

1287
        def root(options={})
1288
          if @scope[:scope_level] == :resources
1289 1290
            with_scope_level(:root) do
              scope(parent_resource.path) do
1291 1292 1293 1294 1295 1296
                super(options)
              end
            end
          else
            super(options)
          end
1297 1298
        end

1299
        protected
1300

1301
          def parent_resource #:nodoc:
1302 1303 1304
            @scope[:scope_level_resource]
          end

J
José Valim 已提交
1305
          def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
1306 1307 1308 1309 1310
            if resources.length > 1
              resources.each { |r| send(method, r, options, &block) }
              return true
            end

1311 1312 1313 1314 1315
            if resource_scope?
              nested { send(method, resources.pop, options, &block) }
              return true
            end

1316
            options.keys.each do |k|
1317 1318 1319
              (options[:constraints] ||= {})[k] = options.delete(k) if options[k].is_a?(Regexp)
            end

1320 1321 1322
            scope_options = options.slice!(*RESOURCE_OPTIONS)
            unless scope_options.empty?
              scope(scope_options) do
1323 1324 1325 1326 1327
                send(method, resources.pop, options, &block)
              end
              return true
            end

1328 1329 1330 1331
            unless action_options?(options)
              options.merge!(scope_action_options) if scope_action_options?
            end

1332 1333 1334
            false
          end

J
José Valim 已提交
1335
          def action_options?(options) #:nodoc:
1336 1337 1338
            options[:only] || options[:except]
          end

J
José Valim 已提交
1339
          def scope_action_options? #:nodoc:
1340 1341 1342
            @scope[:options].is_a?(Hash) && (@scope[:options][:only] || @scope[:options][:except])
          end

J
José Valim 已提交
1343
          def scope_action_options #:nodoc:
1344 1345 1346
            @scope[:options].slice(:only, :except)
          end

J
José Valim 已提交
1347
          def resource_scope? #:nodoc:
1348 1349 1350
            [:resource, :resources].include?(@scope[:scope_level])
          end

J
José Valim 已提交
1351
          def resource_method_scope? #:nodoc:
1352 1353 1354
            [:collection, :member, :new].include?(@scope[:scope_level])
          end

1355
          def with_exclusive_scope
1356
            begin
1357 1358
              old_name_prefix, old_path = @scope[:as], @scope[:path]
              @scope[:as], @scope[:path] = nil, nil
1359

1360 1361 1362
              with_scope_level(:exclusive) do
                yield
              end
1363
            ensure
1364
              @scope[:as], @scope[:path] = old_name_prefix, old_path
1365 1366 1367
            end
          end

1368
          def with_scope_level(kind, resource = parent_resource)
J
Joshua Peek 已提交
1369
            old, @scope[:scope_level] = @scope[:scope_level], kind
1370
            old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
J
Joshua Peek 已提交
1371 1372 1373
            yield
          ensure
            @scope[:scope_level] = old
1374
            @scope[:scope_level_resource] = old_resource
J
Joshua Peek 已提交
1375
          end
1376

J
José Valim 已提交
1377
          def resource_scope(resource) #:nodoc:
1378
            with_scope_level(resource.is_a?(SingletonResource) ? :resource : :resources, resource) do
1379
              scope(parent_resource.resource_scope) do
1380 1381 1382 1383 1384
                yield
              end
            end
          end

J
José Valim 已提交
1385
          def nested_options #:nodoc:
1386 1387 1388 1389 1390 1391
            {}.tap do |options|
              options[:as] = parent_resource.member_name
              options[:constraints] = { "#{parent_resource.singular}_id".to_sym => id_constraint } if id_constraint?
            end
          end

J
José Valim 已提交
1392
          def id_constraint? #:nodoc:
1393
            @scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp)
1394 1395
          end

J
José Valim 已提交
1396
          def id_constraint #:nodoc:
1397
            @scope[:constraints][:id]
1398 1399
          end

J
José Valim 已提交
1400
          def canonical_action?(action, flag) #:nodoc:
1401
            flag && resource_method_scope? && CANONICAL_ACTIONS.include?(action.to_s)
1402 1403
          end

J
José Valim 已提交
1404
          def shallow_scoping? #:nodoc:
1405
            shallow? && @scope[:scope_level] == :member
1406 1407
          end

J
José Valim 已提交
1408
          def path_for_action(action, path) #:nodoc:
1409
            prefix = shallow_scoping? ?
1410 1411
              "#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]

1412 1413
            path = if canonical_action?(action, path.blank?)
              prefix.to_s
1414
            else
1415
              "#{prefix}/#{action_path(action, path)}"
1416 1417 1418
            end
          end

J
José Valim 已提交
1419
          def action_path(name, path = nil) #:nodoc:
1420
            path || @scope[:path_names][name.to_sym] || name.to_s
1421 1422
          end

J
José Valim 已提交
1423
          def prefix_name_for_action(as, action) #:nodoc:
1424
            if as
1425
              as.to_s
1426
            elsif !canonical_action?(action, @scope[:scope_level])
1427
              action.to_s
1428
            end
1429 1430
          end

J
José Valim 已提交
1431
          def name_for_action(as, action) #:nodoc:
1432
            prefix = prefix_name_for_action(as, action)
1433
            prefix = Mapper.normalize_name(prefix) if prefix
1434 1435 1436
            name_prefix = @scope[:as]

            if parent_resource
1437 1438
              return nil if as.nil? && action.nil?

1439 1440
              collection_name = parent_resource.collection_name
              member_name = parent_resource.member_name
1441
            end
1442

1443
            name = case @scope[:scope_level]
1444 1445
            when :nested
              [member_name, prefix]
1446
            when :collection
1447
              [prefix, name_prefix, collection_name]
1448
            when :new
1449 1450 1451 1452 1453
              [prefix, :new, name_prefix, member_name]
            when :member
              [prefix, shallow_scoping? ? @scope[:shallow_prefix] : name_prefix, member_name]
            when :root
              [name_prefix, collection_name, prefix]
1454
            else
1455
              [name_prefix, member_name, prefix]
1456
            end
1457

1458
            candidate = name.select(&:present?).join("_").presence
1459
            candidate unless as.nil? && @set.routes.find { |r| r.name == candidate }
1460
          end
J
Joshua Peek 已提交
1461
      end
J
Joshua Peek 已提交
1462

J
José Valim 已提交
1463
      module Shorthand #:nodoc:
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        def match(*args)
          if args.size == 1 && args.last.is_a?(Hash)
            options  = args.pop
            path, to = options.find { |name, value| name.is_a?(String) }
            options.merge!(:to => to).delete(path)
            super(path, options)
          else
            super
          end
        end
      end

1476 1477
      include Base
      include HttpHelpers
1478
      include Redirection
1479 1480
      include Scoping
      include Resources
1481
      include Shorthand
J
Joshua Peek 已提交
1482 1483
    end
  end
J
Joshua Peek 已提交
1484
end