mapper.rb 52.4 KB
Newer Older
1
require 'active_support/core_ext/hash/except'
2
require 'active_support/core_ext/object/blank'
S
Santiago Pastorino 已提交
3
require 'active_support/core_ext/enumerable'
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
        attr_reader :app, :constraints
20

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
        ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
52
        SHORTHAND_REGEX = %r{/[\w/]+$}
53
        WILDCARD_PATH = %r{\*([^/\)]+)\)?$}
54

55
        def initialize(set, scope, path, options)
56
          @set, @scope = set, scope
57
          @segment_keys = nil
58
          @options = (@scope[:options] || {}).merge(options)
59
          @path = normalize_path(path)
60
          normalize_options!
61
        end
J
Joshua Peek 已提交
62

63
        def to_route
64
          [ app, conditions, requirements, defaults, @options[:as], @options[:anchor] ]
65
        end
J
Joshua Peek 已提交
66

67
        private
68 69 70

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

72 73
            if using_match_shorthand?(path_without_format, @options)
              to_shorthand    = @options[:to].blank?
74
              @options[:to] ||= path_without_format.gsub(/\(.*\)/, "")[1..-1].sub(%r{/([^/]*)$}, '#\1')
75 76
            end

77
            @options.merge!(default_controller_and_action(to_shorthand))
78 79 80 81 82

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

83
              if requirement.source =~ ANCHOR_CHARACTERS_REGEX
84 85 86 87 88 89
                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
90
          end
91

92
          # match "account/overview"
93
          def using_match_shorthand?(path, options)
94
            path && (options[:to] || options[:action]).nil? && path =~ SHORTHAND_REGEX
95
          end
96

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

            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' }
108
              @options[:controller] ||= /.+?/
109 110
            end

111 112
            # Add a constraint for wildcard route to make it non-greedy and match the
            # optional format part of the route by default
113
            if path.match(WILDCARD_PATH) && @options[:format] != false
114
              @options[$1.to_sym] ||= /.+?/
115 116
            end

117 118 119
            if @options[:format] == false
              @options.delete(:format)
              path
120
            elsif path.include?(":format") || path.end_with?('/')
121
              path
122 123
            elsif @options[:format] == true
              "#{path}.:format"
124 125 126
            else
              "#{path}(.:format)"
            end
127
          end
128

129 130
          def app
            Constraints.new(
131
              to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults),
132 133
              blocks,
              @set.request_class
134
            )
135 136
          end

137 138 139
          def conditions
            { :path_info => @path }.merge(constraints).merge(request_method_condition)
          end
J
Joshua Peek 已提交
140

141
          def requirements
142
            @requirements ||= (@options[:constraints].is_a?(Hash) ? @options[:constraints] : {}).tap do |requirements|
143 144 145 146
              requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints]
              @options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) }
            end
          end
147

148
          def defaults
149 150 151 152 153 154
            @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

155
          def default_controller_and_action(to_shorthand=nil)
156
            if to.respond_to?(:call)
157 158
              { }
            else
159
              if to.is_a?(String)
160
                controller, action = to.split('#')
161 162
              elsif to.is_a?(Symbol)
                action = to.to_s
163
              end
J
Joshua Peek 已提交
164

165 166
              controller ||= default_controller
              action     ||= default_action
167

168 169 170
              unless controller.is_a?(Regexp) || to_shorthand
                controller = [@scope[:module], controller].compact.join("/").presence
              end
171

172 173 174 175
              if controller.is_a?(String) && controller =~ %r{\A/}
                raise ArgumentError, "controller name should not start with a slash"
              end

176 177
              controller = controller.to_s unless controller.is_a?(Regexp)
              action     = action.to_s     unless action.is_a?(Regexp)
178

179
              if controller.blank? && segment_keys.exclude?("controller")
180 181
                raise ArgumentError, "missing :controller"
              end
J
Joshua Peek 已提交
182

183
              if action.blank? && segment_keys.exclude?("action")
184 185
                raise ArgumentError, "missing :action"
              end
J
Joshua Peek 已提交
186

A
Aaron Patterson 已提交
187
              hash = {}
A
Aaron Patterson 已提交
188 189
              hash[:controller] = controller unless controller.blank?
              hash[:action]     = action unless action.blank?
A
Aaron Patterson 已提交
190
              hash
191 192
            end
          end
193

194
          def blocks
195 196 197 198 199
            constraints = @options[:constraints]
            if constraints.present? && !constraints.is_a?(Hash)
              [constraints]
            else
              @scope[:blocks] || []
200 201
            end
          end
J
Joshua Peek 已提交
202

203 204 205
          def constraints
            @constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller }
          end
206

207 208
          def request_method_condition
            if via = @options[:via]
209 210
              list = Array(via).map { |m| m.to_s.dasherize.upcase }
              { :request_method => list }
211 212
            else
              { }
213
            end
214
          end
J
Joshua Peek 已提交
215

216
          def segment_keys
217 218 219
            return @segment_keys if @segment_keys

            @segment_keys = Journey::Path::Pattern.new(
220
              Journey::Router::Strexp.compile(@path, requirements, SEPARATORS)
221
            ).names
222
          end
223

224 225 226
          def to
            @options[:to]
          end
J
Joshua Peek 已提交
227

228
          def default_controller
229
            @options[:controller] || @scope[:controller]
230
          end
231 232

          def default_action
233
            @options[:action] || @scope[:action]
234
          end
235
      end
236

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

246 247 248 249
      def self.normalize_name(name)
        normalize_path(name)[1..-1].gsub("/", "_")
      end

250
      module Base
251 252 253 254
        # You can specify what Rails should route "/" to with the root method:
        #
        #   root :to => 'pages#main'
        #
255
        # For options, see +match+, as +root+ uses it internally.
256
        #
B
Brian Cardarella 已提交
257 258 259 260
        # You can also pass a string which will expand
        #
        #   root 'pages#main'
        #
261 262 263
        # 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.
264
        def root(options = {})
B
Brian Cardarella 已提交
265
          options = { :to => options } if options.is_a?(String)
266
          match '/', { :as => :root }.merge(options)
267
        end
268

269 270 271
        # 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:
272
        #
273
        #   # sets :controller, :action and :id in params
274
        #   match ':controller/:action/:id'
275
        #
276 277 278 279 280 281 282 283 284 285 286 287
        # 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:
288 289 290 291
        #
        #   match 'photos/:id' => 'photos#show'
        #   match 'photos/:id', :to => 'photos#show'
        #   match 'photos/:id', :controller => 'photos', :action => 'show'
292
        #
293 294 295
        # A pattern can also point to a +Rack+ endpoint i.e. anything that
        # responds to +call+:
        #
A
Alexey Vakhov 已提交
296
        #   match 'photos/:id' => lambda {|hash| [200, {}, "Coming soon"] }
297 298 299 300
        #   match 'photos/:id' => PhotoRackApp
        #   # Yes, controller actions are just rack endpoints
        #   match 'photos/:id' => PhotosController.action(:show)
        #
301
        # === Options
302
        #
303
        # Any options not seen here are passed on as params with the url.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        #
        # [: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]
332 333
        #   Points to a +Rack+ endpoint. Can be an object that responds to
        #   +call+ or a string representing a controller's action.
334
        #
335
        #      match 'path', :to => 'controller#action'
J
Justin Woodbridge 已提交
336
        #      match 'path', :to => lambda { |env| [200, {}, "Success!"] }
337
        #      match 'path', :to => RackApp
338 339 340
        #
        # [:on]
        #   Shorthand for wrapping routes in a specific RESTful context. Valid
341
        #   values are +:member+, +:collection+, and +:new+. Only use within
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        #   <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
358
        #   object that responds to <tt>matches?</tt>
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        #
        #     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.
377 378
        #
        # [:anchor]
379
        #   Boolean to anchor a <tt>match</tt> pattern. Default is true. When set to
380 381 382 383
        #   false, the pattern matches any request prefixed with the given path.
        #
        #     # Matches any request starting with 'path'
        #     match 'path' => 'c#a', :anchor => false
384
        def match(path, options=nil)
385
        end
386

387 388
        # Mount a Rack-based application to be used within the application.
        #
R
Ryan Bigg 已提交
389
        #   mount SomeRackApp, :at => "some_route"
390 391 392
        #
        # Alternatively:
        #
R
Ryan Bigg 已提交
393
        #   mount(SomeRackApp => "some_route")
394
        #
395 396
        # For options, see +match+, as +mount+ uses it internally.
        #
397 398 399 400 401
        # 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 已提交
402
        #   mount(SomeRackApp => "some_route", :as => "exciting")
403 404 405
        #
        # This will generate the +exciting_path+ and +exciting_url+ helpers
        # which can be used to navigate to this mounted app.
406 407 408 409 410 411 412 413 414 415 416
        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

417 418
          options[:as] ||= app_name(app)

419
          match(path, options.merge(:to => app, :anchor => false, :format => false))
420 421

          define_generate_prefix(app, options[:as])
422 423 424
          self
        end

425 426 427 428
        def default_url_options=(options)
          @set.default_url_options = options
        end
        alias_method :default_url_options, :default_url_options=
429

430 431 432 433 434 435
        def with_default_scope(scope, &block)
          scope(scope) do
            instance_exec(&block)
          end
        end

436 437 438
        private
          def app_name(app)
            return unless app.respond_to?(:routes)
439 440 441 442 443 444 445

            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
446 447 448
          end

          def define_generate_prefix(app, name)
449
            return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper)
450 451

            _route = @set.named_routes.routes[name.to_sym]
P
Piotr Sarnacki 已提交
452 453
            _routes = @set
            app.routes.define_mounted_helper(name)
J
José Valim 已提交
454 455 456 457 458
            app.routes.singleton_class.class_eval do
              define_method :mounted? do
                true
              end

459
              define_method :_generate_prefix do |options|
P
Piotr Sarnacki 已提交
460
                prefix_options = options.slice(*_route.segment_keys)
461 462
                # we must actually delete prefix segment keys to avoid passing them to next url_for
                _route.segment_keys.each { |k| options.delete(k) }
R
rails-noob 已提交
463 464 465
                prefix = _routes.url_helpers.send("#{name}_path", prefix_options)
                prefix = '' if prefix == '/'
                prefix
466 467 468
              end
            end
          end
469 470 471
      end

      module HttpHelpers
472
        # Define a route that only recognizes HTTP GET.
473
        # For supported arguments, see <tt>Base#match</tt>.
474 475 476
        #
        # Example:
        #
477
        #   get 'bacon', :to => 'food#bacon'
478
        def get(*args, &block)
479
          map_method(:get, args, &block)
480 481
        end

482
        # Define a route that only recognizes HTTP POST.
483
        # For supported arguments, see <tt>Base#match</tt>.
484 485 486
        #
        # Example:
        #
487
        #   post 'bacon', :to => 'food#bacon'
488
        def post(*args, &block)
489
          map_method(:post, args, &block)
490 491
        end

492 493 494 495 496 497 498 499 500 501
        # Define a route that only recognizes HTTP PATCH.
        # For supported arguments, see <tt>Base#match</tt>.
        #
        # Example:
        #
        #   patch 'bacon', :to => 'food#bacon'
        def patch(*args, &block)
          map_method(:patch, args, &block)
        end

502
        # Define a route that only recognizes HTTP PUT.
503
        # For supported arguments, see <tt>Base#match</tt>.
504 505 506
        #
        # Example:
        #
507
        #   put 'bacon', :to => 'food#bacon'
508
        def put(*args, &block)
509
          map_method(:put, args, &block)
510 511
        end

512
        # Define a route that only recognizes HTTP DELETE.
513
        # For supported arguments, see <tt>Base#match</tt>.
514 515 516
        #
        # Example:
        #
517
        #   delete 'broccoli', :to => 'food#broccoli'
518
        def delete(*args, &block)
519
          map_method(:delete, args, &block)
520 521 522
        end

        private
523
          def map_method(method, args, &block)
524 525
            options = args.extract_options!
            options[:via] = method
526
            match(*args, options, &block)
527 528 529 530
            self
          end
      end

531 532 533
      # 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
S
Sebastian Martinez 已提交
534 535
      # the <tt>app/controllers/admin</tt> directory, and you can group them
      # together in your router:
536 537 538 539
      #
      #   namespace "admin" do
      #     resources :posts, :comments
      #   end
540
      #
541
      # This will create a number of routes for each of the posts and comments
S
Sebastian Martinez 已提交
542
      # controller. For <tt>Admin::PostsController</tt>, Rails will create:
543
      #
544 545 546 547 548
      #   GET       /admin/posts
      #   GET       /admin/posts/new
      #   POST      /admin/posts
      #   GET       /admin/posts/1
      #   GET       /admin/posts/1/edit
549
      #   PATCH/PUT /admin/posts/1
550
      #   DELETE    /admin/posts/1
551
      #
552
      # If you want to route /posts (without the prefix /admin) to
S
Sebastian Martinez 已提交
553
      # <tt>Admin::PostsController</tt>, you could use
554
      #
555
      #   scope :module => "admin" do
556
      #     resources :posts
557 558 559
      #   end
      #
      # or, for a single case
560
      #
561
      #   resources :posts, :module => "admin"
562
      #
S
Sebastian Martinez 已提交
563
      # If you want to route /admin/posts to +PostsController+
564
      # (without the Admin:: module prefix), you could use
565
      #
566
      #   scope "/admin" do
567
      #     resources :posts
568 569 570
      #   end
      #
      # or, for a single case
571
      #
572
      #   resources :posts, :path => "/admin/posts"
573 574 575
      #
      # 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
S
Sebastian Martinez 已提交
576
      # +PostsController+:
577
      #
578 579 580 581 582
      #   GET       /admin/posts
      #   GET       /admin/posts/new
      #   POST      /admin/posts
      #   GET       /admin/posts/1
      #   GET       /admin/posts/1/edit
583
      #   PATCH/PUT /admin/posts/1
584
      #   DELETE    /admin/posts/1
585
      module Scoping
586
        # Scopes a set of routes to the given default options.
587 588 589 590 591 592 593 594
        #
        # 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.
595 596
        # The difference here being that the routes generated are like /:account_id/projects,
        # rather than /accounts/:account_id/projects.
597
        #
598
        # === Options
599
        #
600
        # Takes same options as <tt>Base#match</tt> and <tt>Resources#resources</tt>.
601
        #
602
        # === Examples
603
        #
S
Sebastian Martinez 已提交
604
        #   # route /posts (without the prefix /admin) to <tt>Admin::PostsController</tt>
605 606 607
        #   scope :module => "admin" do
        #     resources :posts
        #   end
608
        #
609 610 611 612
        #   # prefix the posts resource's requests with '/admin'
        #   scope :path => "/admin" do
        #     resources :posts
        #   end
613
        #
S
Sebastian Martinez 已提交
614
        #   # prefix the routing helper name: +sekret_posts_path+ instead of +posts_path+
615 616 617
        #   scope :as => "sekret" do
        #     resources :posts
        #   end
618 619
        def scope(*args)
          options = args.extract_options!
620
          options = options.dup
621

622
          options[:path] = args.first if args.first.is_a?(String)
623
          recover = {}
624

625 626
          options[:constraints] ||= {}
          unless options[:constraints].is_a?(Hash)
627
            block, options[:constraints] = options[:constraints], {}
628
          end
629

630 631 632 633 634
          scope_options.each do |option|
            if value = options.delete(option)
              recover[option] = @scope[option]
              @scope[option]  = send("merge_#{option}_scope", @scope[option], value)
            end
635 636
          end

637 638 639 640 641 642
          recover[:block] = @scope[:blocks]
          @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block)

          recover[:options] = @scope[:options]
          @scope[:options]  = merge_options_scope(@scope[:options], options)

643 644 645
          yield
          self
        ensure
646 647 648
          scope_options.each do |option|
            @scope[option] = recover[option] if recover.has_key?(option)
          end
649 650 651

          @scope[:options] = recover[:options]
          @scope[:blocks]  = recover[:block]
652 653
        end

654 655 656 657 658 659
        # Scopes routes to a specific controller
        #
        # Example:
        #   controller "food" do
        #     match "bacon", :action => "bacon"
        #   end
660 661 662
        def controller(controller, options={})
          options[:controller] = controller
          scope(options) { yield }
663 664
        end

665 666 667 668 669 670 671 672
        # Scopes routes to a specific namespace. For example:
        #
        #   namespace :admin do
        #     resources :posts
        #   end
        #
        # This generates the following routes:
        #
673 674 675 676 677
        #       admin_posts GET       /admin/posts(.:format)          admin/posts#index
        #       admin_posts POST      /admin/posts(.:format)          admin/posts#create
        #    new_admin_post GET       /admin/posts/new(.:format)      admin/posts#new
        #   edit_admin_post GET       /admin/posts/:id/edit(.:format) admin/posts#edit
        #        admin_post GET       /admin/posts/:id(.:format)      admin/posts#show
678
        #        admin_post PATCH/PUT /admin/posts/:id(.:format)      admin/posts#update
679
        #        admin_post DELETE    /admin/posts/:id(.:format)      admin/posts#destroy
680
        #
681
        # === Options
682
        #
683 684
        # The +:path+, +:as+, +:module+, +:shallow_path+ and +:shallow_prefix+
        # options all default to the name of the namespace.
685
        #
686 687
        # For options, see <tt>Base#match</tt>. For +:shallow_path+ option, see
        # <tt>Resources#resources</tt>.
688
        #
689
        # === Examples
690
        #
691 692 693 694
        #   # accessible through /sekret/posts rather than /admin/posts
        #   namespace :admin, :path => "sekret" do
        #     resources :posts
        #   end
695
        #
S
Sebastian Martinez 已提交
696
        #   # maps to <tt>Sekret::PostsController</tt> rather than <tt>Admin::PostsController</tt>
697 698 699
        #   namespace :admin, :module => "sekret" do
        #     resources :posts
        #   end
700
        #
S
Sebastian Martinez 已提交
701
        #   # generates +sekret_posts_path+ rather than +admin_posts_path+
702 703 704
        #   namespace :admin, :as => "sekret" do
        #     resources :posts
        #   end
705
        def namespace(path, options = {})
706
          path = path.to_s
707 708 709
          options = { :path => path, :as => path, :module => path,
                      :shallow_path => path, :shallow_prefix => path }.merge!(options)
          scope(options) { yield }
710
        end
711

R
Ryan Bigg 已提交
712 713 714 715
        # === 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:
        #
M
mjy 已提交
716
        #   constraints(:id => /\d+\.\d+/) do
R
Ryan Bigg 已提交
717 718 719 720 721
        #     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.
722
        #
R
R.T. Lechow 已提交
723
        # You may use this to also restrict other parameters:
R
Ryan Bigg 已提交
724 725
        #
        #   resources :posts do
M
mjy 已提交
726
        #     constraints(:post_id => /\d+\.\d+/) do
R
Ryan Bigg 已提交
727 728
        #       resources :comments
        #     end
J
James Miller 已提交
729
        #   end
R
Ryan Bigg 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743
        #
        # === 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
        #
R
R.T. Lechow 已提交
744
        # Requests to routes can be constrained based on specific criteria:
R
Ryan Bigg 已提交
745 746 747 748 749 750 751 752 753 754
        #
        #    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
755
        #      def self.matches?(request)
R
Ryan Bigg 已提交
756 757 758 759 760 761 762 763 764 765 766
        #        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
767 768 769 770
        def constraints(constraints = {})
          scope(:constraints => constraints) { yield }
        end

R
Ryan Bigg 已提交
771
        # Allows you to set default parameters for a route, such as this:
772 773 774
        #   defaults :id => 'home' do
        #     match 'scoped_pages/(:id)', :to => 'pages#show'
        #   end
R
Ryan Bigg 已提交
775
        # Using this, the +:id+ parameter here will default to 'home'.
776 777 778 779
        def defaults(defaults = {})
          scope(:defaults => defaults) { yield }
        end

780
        private
J
José Valim 已提交
781
          def scope_options #:nodoc:
782 783 784
            @scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
          end

J
José Valim 已提交
785
          def merge_path_scope(parent, child) #:nodoc:
786
            Mapper.normalize_path("#{parent}/#{child}")
787 788
          end

J
José Valim 已提交
789
          def merge_shallow_path_scope(parent, child) #:nodoc:
790 791 792
            Mapper.normalize_path("#{parent}/#{child}")
          end

J
José Valim 已提交
793
          def merge_as_scope(parent, child) #:nodoc:
794
            parent ? "#{parent}_#{child}" : child
795 796
          end

J
José Valim 已提交
797
          def merge_shallow_prefix_scope(parent, child) #:nodoc:
798 799 800
            parent ? "#{parent}_#{child}" : child
          end

J
José Valim 已提交
801
          def merge_module_scope(parent, child) #:nodoc:
802 803 804
            parent ? "#{parent}/#{child}" : child
          end

J
José Valim 已提交
805
          def merge_controller_scope(parent, child) #:nodoc:
806
            child
807 808
          end

J
José Valim 已提交
809
          def merge_path_names_scope(parent, child) #:nodoc:
810 811 812
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
813
          def merge_constraints_scope(parent, child) #:nodoc:
814 815 816
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
817
          def merge_defaults_scope(parent, child) #:nodoc:
818 819 820
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
821
          def merge_blocks_scope(parent, child) #:nodoc:
822 823 824
            merged = parent ? parent.dup : []
            merged << child if child
            merged
825 826
          end

J
José Valim 已提交
827
          def merge_options_scope(parent, child) #:nodoc:
828
            (parent || {}).except(*override_keys(child)).merge(child)
829
          end
830

J
José Valim 已提交
831
          def merge_shallow_scope(parent, child) #:nodoc:
832 833
            child ? true : false
          end
834

J
José Valim 已提交
835
          def override_keys(child) #:nodoc:
836 837
            child.key?(:only) || child.key?(:except) ? [:only, :except] : []
          end
838 839
      end

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
      # 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
S
Sebastian Martinez 已提交
864 865
      # <tt>app/controllers/admin</tt> directory, and you can group them together
      # in your router:
866 867 868 869 870
      #
      #   namespace "admin" do
      #     resources :posts, :comments
      #   end
      #
S
Sebastian Martinez 已提交
871 872
      # 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
873 874 875 876
      # overrides this restriction, e.g:
      #
      #   resources :articles, :id => /[^\/]+/
      #
S
Sebastian Martinez 已提交
877
      # This allows any character other than a slash as part of your +:id+.
878
      #
J
Joshua Peek 已提交
879
      module Resources
880 881
        # CANONICAL_ACTIONS holds all actions that does not need a prefix or
        # a path appended since they fit properly in their scope level.
882 883 884
        VALID_ON_OPTIONS  = [:new, :collection, :member]
        RESOURCE_OPTIONS  = [:as, :controller, :path, :only, :except]
        CANONICAL_ACTIONS = %w(index create new show update destroy)
885

886
        class Resource #:nodoc:
887
          attr_reader :controller, :path, :options
888 889

          def initialize(entities, options = {})
890
            @name       = entities.to_s
891 892 893
            @path       = (options[:path] || @name).to_s
            @controller = (options[:controller] || @name).to_s
            @as         = options[:as]
894
            @options    = options
895 896
          end

897
          def default_actions
898
            [:index, :create, :new, :show, :update, :destroy, :edit]
899 900
          end

901
          def actions
902
            if only = @options[:only]
903
              Array(only).map(&:to_sym)
904
            elsif except = @options[:except]
905 906 907 908 909 910
              default_actions - Array(except).map(&:to_sym)
            else
              default_actions
            end
          end

911
          def name
912
            @as || @name
913 914
          end

915
          def plural
916
            @plural ||= name.to_s
917 918 919
          end

          def singular
920
            @singular ||= name.to_s.singularize
921 922
          end

923
          alias :member_name :singular
924

925
          # Checks for uncountable plurals, and appends "_index" if the plural
926
          # and singular form are the same.
927
          def collection_name
928
            singular == plural ? "#{plural}_index" : plural
929 930
          end

931
          def resource_scope
932
            { :controller => controller }
933 934
          end

935
          alias :collection_scope :path
936 937

          def member_scope
938
            "#{path}/:id"
939 940
          end

941
          def new_scope(new_path)
942
            "#{path}/#{new_path}"
943 944 945
          end

          def nested_scope
946
            "#{path}/:#{singular}_id"
947
          end
948

949 950 951
        end

        class SingletonResource < Resource #:nodoc:
952
          def initialize(entities, options)
953
            super
954
            @as         = nil
955 956
            @controller = (options[:controller] || plural).to_s
            @as         = options[:as]
957 958
          end

959 960 961 962
          def default_actions
            [:show, :create, :update, :destroy, :new, :edit]
          end

963 964
          def plural
            @plural ||= name.to_s.pluralize
965 966
          end

967 968
          def singular
            @singular ||= name.to_s
969
          end
970 971 972 973 974 975

          alias :member_name :singular
          alias :collection_name :singular

          alias :member_scope :path
          alias :nested_scope :path
976 977
        end

978 979 980 981
        def resources_path_names(options)
          @scope[:path_names].merge!(options)
        end

982 983 984 985 986 987 988 989 990
        # 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
S
Sebastian Martinez 已提交
991
        # the +GeoCoders+ controller (note that the controller is named after
992 993
        # the plural):
        #
994 995 996 997
        #   GET       /geocoder/new
        #   POST      /geocoder
        #   GET       /geocoder
        #   GET       /geocoder/edit
998
        #   PATCH/PUT /geocoder
999
        #   DELETE    /geocoder
1000
        #
1001
        # === Options
1002
        # Takes same options as +resources+.
J
Joshua Peek 已提交
1003
        def resource(*resources, &block)
J
Joshua Peek 已提交
1004
          options = resources.extract_options!
J
Joshua Peek 已提交
1005

1006
          if apply_common_behavior_for(:resource, resources, options, &block)
1007 1008 1009
            return self
          end

1010
          resource_scope(:resource, SingletonResource.new(resources.pop, options)) do
1011
            yield if block_given?
1012

1013
            collection do
1014
              post :create
1015
            end if parent_resource.actions.include?(:create)
1016

1017
            new do
1018
              get :new
1019
            end if parent_resource.actions.include?(:new)
1020

1021
            member do
1022 1023
              get :edit if parent_resource.actions.include?(:edit)
              get :show if parent_resource.actions.include?(:show)
1024
              if parent_resource.actions.include?(:update)
1025 1026
                patch :update
                put   :update
1027
              end
1028
              delete :destroy if parent_resource.actions.include?(:destroy)
1029 1030 1031
            end
          end

J
Joshua Peek 已提交
1032
          self
1033 1034
        end

1035 1036 1037 1038 1039 1040 1041 1042
        # 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
S
Sebastian Martinez 已提交
1043
        # the +Photos+ controller:
1044
        #
1045 1046 1047 1048 1049
        #   GET       /photos
        #   GET       /photos/new
        #   POST      /photos
        #   GET       /photos/:id
        #   GET       /photos/:id/edit
1050
        #   PATCH/PUT /photos/:id
1051
        #   DELETE    /photos/:id
1052
        #
1053 1054 1055 1056 1057 1058 1059 1060
        # Resources can also be nested infinitely by using this block syntax:
        #
        #   resources :photos do
        #     resources :comments
        #   end
        #
        # This generates the following comments routes:
        #
1061 1062 1063 1064 1065
        #   GET       /photos/:photo_id/comments
        #   GET       /photos/:photo_id/comments/new
        #   POST      /photos/:photo_id/comments
        #   GET       /photos/:photo_id/comments/:id
        #   GET       /photos/:photo_id/comments/:id/edit
1066
        #   PATCH/PUT /photos/:photo_id/comments/:id
1067
        #   DELETE    /photos/:photo_id/comments/:id
1068
        #
1069
        # === Options
1070 1071
        # Takes same options as <tt>Base#match</tt> as well as:
        #
1072
        # [:path_names]
A
Aviv Ben-Yosef 已提交
1073 1074
        #   Allows you to change the segment component of the +edit+ and +new+ actions.
        #   Actions not specified are not changed.
1075 1076 1077 1078
        #
        #     resources :posts, :path_names => { :new => "brand_new" }
        #
        #   The above example will now change /posts/new to /posts/brand_new
1079
        #
1080 1081 1082 1083 1084 1085 1086
        # [:path]
        #   Allows you to change the path prefix for the resource.
        #
        #     resources :posts, :path => 'postings'
        #
        #   The resource and all segments will now route to /postings instead of /posts
        #
1087 1088
        # [:only]
        #   Only generate routes for the given actions.
1089
        #
1090 1091
        #     resources :cows, :only => :show
        #     resources :cows, :only => [:show, :index]
1092
        #
1093 1094
        # [:except]
        #   Generate all routes except for the given actions.
1095
        #
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
        #     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
1110
        #       resources :comments, :except => [:show, :edit, :update, :destroy]
1111
        #     end
1112 1113 1114 1115 1116
        #     resources :comments, :only => [:show, :edit, :update, :destroy]
        #
        #   This allows URLs for resources that otherwise would be deeply nested such
        #   as a comment on a blog post like <tt>/posts/a-long-permalink/comments/1234</tt>
        #   to be shortened to just <tt>/comments/1234</tt>.
1117 1118 1119 1120
        #
        # [:shallow_path]
        #   Prefixes nested shallow routes with the specified path.
        #
1121 1122 1123 1124
        #     scope :shallow_path => "sekret" do
        #       resources :posts do
        #         resources :comments, :shallow => true
        #       end
1125 1126 1127 1128
        #     end
        #
        #   The +comments+ resource here will have the following routes generated for it:
        #
1129 1130 1131 1132 1133
        #     post_comments    GET       /posts/:post_id/comments(.:format)
        #     post_comments    POST      /posts/:post_id/comments(.:format)
        #     new_post_comment GET       /posts/:post_id/comments/new(.:format)
        #     edit_comment     GET       /sekret/comments/:id/edit(.:format)
        #     comment          GET       /sekret/comments/:id(.:format)
1134
        #     comment          PATCH/PUT /sekret/comments/:id(.:format)
1135
        #     comment          DELETE    /sekret/comments/:id(.:format)
1136
        #
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
        # [:shallow_prefix]
        #   Prefixes nested shallow route names with specified prefix.
        #
        #     scope :shallow_prefix => "sekret"
        #       resources :posts do
        #         resources :comments, :shallow => true
        #       end
        #     end
        #
        #   The +comments+ resource here will have the following routes generated for it:
        #
        #     post_comments           GET       /posts/:post_id/comments(.:format)
        #     post_comments           POST      /posts/:post_id/comments(.:format)
        #     new_post_comment        GET       /posts/:post_id/comments/new(.:format)
        #     edit_sekret_comment     GET       /comments/:id/edit(.:format)
        #     sekret_comment          GET       /comments/:id(.:format)
        #     sekret_comment          PATCH/PUT /comments/:id(.:format)
        #     sekret_comment          DELETE    /comments/:id(.:format)
        #
1156
        # === Examples
1157
        #
S
Sebastian Martinez 已提交
1158
        #   # routes call <tt>Admin::PostsController</tt>
1159
        #   resources :posts, :module => "admin"
1160
        #
1161
        #   # resource actions are at /admin/posts.
1162
        #   resources :posts, :path => "admin/posts"
J
Joshua Peek 已提交
1163
        def resources(*resources, &block)
J
Joshua Peek 已提交
1164
          options = resources.extract_options!
1165

1166
          if apply_common_behavior_for(:resources, resources, options, &block)
1167 1168 1169
            return self
          end

1170
          resource_scope(:resources, Resource.new(resources.pop, options)) do
1171
            yield if block_given?
J
Joshua Peek 已提交
1172

1173
            collection do
1174 1175
              get  :index if parent_resource.actions.include?(:index)
              post :create if parent_resource.actions.include?(:create)
1176
            end
1177

1178
            new do
1179
              get :new
1180
            end if parent_resource.actions.include?(:new)
1181

1182
            member do
1183 1184
              get :edit if parent_resource.actions.include?(:edit)
              get :show if parent_resource.actions.include?(:show)
1185
              if parent_resource.actions.include?(:update)
1186 1187
                patch :update
                put   :update
1188
              end
1189
              delete :destroy if parent_resource.actions.include?(:destroy)
1190 1191 1192
            end
          end

J
Joshua Peek 已提交
1193
          self
1194 1195
        end

1196 1197 1198 1199 1200 1201 1202 1203 1204
        # 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>
S
Sebastian Martinez 已提交
1205
        # with GET, and route to the search action of +PhotosController+. It will also
1206 1207
        # create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt>
        # route helpers.
J
Joshua Peek 已提交
1208
        def collection
1209 1210
          unless resource_scope?
            raise ArgumentError, "can't use collection outside resource(s) scope"
1211 1212
          end

1213 1214 1215 1216
          with_scope_level(:collection) do
            scope(parent_resource.collection_scope) do
              yield
            end
J
Joshua Peek 已提交
1217
          end
1218
        end
J
Joshua Peek 已提交
1219

1220 1221 1222 1223 1224 1225 1226 1227 1228
        # 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
S
Sebastian Martinez 已提交
1229
        # preview action of +PhotosController+. It will also create the
1230
        # <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.
J
Joshua Peek 已提交
1231
        def member
1232 1233
          unless resource_scope?
            raise ArgumentError, "can't use member outside resource(s) scope"
J
Joshua Peek 已提交
1234
          end
J
Joshua Peek 已提交
1235

1236 1237 1238 1239
          with_scope_level(:member) do
            scope(parent_resource.member_scope) do
              yield
            end
1240 1241 1242 1243 1244 1245 1246
          end
        end

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

1248 1249 1250 1251
          with_scope_level(:new) do
            scope(parent_resource.new_scope(action_path(:new))) do
              yield
            end
J
Joshua Peek 已提交
1252
          end
J
Joshua Peek 已提交
1253 1254
        end

1255
        def nested
1256 1257
          unless resource_scope?
            raise ArgumentError, "can't use nested outside resource(s) scope"
1258 1259 1260
          end

          with_scope_level(:nested) do
1261
            if shallow?
1262
              with_exclusive_scope do
1263
                if @scope[:shallow_path].blank?
1264
                  scope(parent_resource.nested_scope, nested_options) { yield }
1265
                else
1266
                  scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
1267
                    scope(parent_resource.nested_scope, nested_options) { yield }
1268 1269 1270 1271
                  end
                end
              end
            else
1272
              scope(parent_resource.nested_scope, nested_options) { yield }
1273 1274 1275 1276
            end
          end
        end

1277
        # See ActionDispatch::Routing::Mapper::Scoping#namespace
1278
        def namespace(path, options = {})
1279
          if resource_scope?
1280 1281 1282 1283 1284 1285
            nested { super }
          else
            super
          end
        end

1286
        def shallow
1287
          scope(:shallow => true, :shallow_path => @scope[:path]) do
1288 1289 1290 1291
            yield
          end
        end

1292 1293 1294 1295
        def shallow?
          parent_resource.instance_of?(Resource) && @scope[:shallow]
        end

1296
        # match 'path' => 'controller#action'
R
Rafael Mendonça França 已提交
1297
        # match 'path', to: 'controller#action'
1298
        # match 'path', 'otherpath', on: :member, via: :get
1299 1300 1301 1302
        def match(path, *rest)
          if rest.empty? && Hash === path
            options  = path
            path, to = options.find { |name, value| name.is_a?(String) }
1303 1304
            options[:to] = to
            options.delete(path)
1305 1306 1307 1308 1309 1310
            paths = [path]
          else
            options = rest.pop || {}
            paths = [path] + rest
          end

1311 1312
          options[:anchor] = true unless options.key?(:anchor)

A
Aaron Patterson 已提交
1313 1314 1315 1316
          if options[:on] && !VALID_ON_OPTIONS.include?(options[:on])
            raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
          end

1317
          paths.each { |_path| decomposed_match(_path, options.dup) }
1318 1319
          self
        end
1320

1321
        def decomposed_match(path, options) # :nodoc:
A
Aaron Patterson 已提交
1322 1323
          if on = options.delete(:on)
            send(on) { decomposed_match(path, options) }
1324
          else
A
Aaron Patterson 已提交
1325 1326 1327 1328 1329 1330 1331 1332
            case @scope[:scope_level]
            when :resources
              nested { decomposed_match(path, options) }
            when :resource
              member { decomposed_match(path, options) }
            else
              add_route(path, options)
            end
J
Joshua Peek 已提交
1333
          end
1334
        end
J
Joshua Peek 已提交
1335

1336
        def add_route(action, options) # :nodoc:
1337
          path = path_for_action(action, options.delete(:path))
1338

1339 1340 1341
          if action.to_s =~ /^[\w\/]+$/
            options[:action] ||= action unless action.to_s.include?("/")
          else
1342 1343 1344
            action = nil
          end

1345
          if !options.fetch(:as, true)
1346 1347 1348
            options.delete(:as)
          else
            options[:as] = name_for_action(options[:as], action)
J
Joshua Peek 已提交
1349
          end
J
Joshua Peek 已提交
1350

1351 1352 1353
          mapping = Mapping.new(@set, @scope, path, options)
          app, conditions, requirements, defaults, as, anchor = mapping.to_route
          @set.add_route(app, conditions, requirements, defaults, as, anchor)
J
Joshua Peek 已提交
1354 1355
        end

1356
        def root(options={})
1357
          if @scope[:scope_level] == :resources
1358 1359
            with_scope_level(:root) do
              scope(parent_resource.path) do
1360 1361 1362 1363 1364 1365
                super(options)
              end
            end
          else
            super(options)
          end
1366 1367
        end

1368
        protected
1369

1370
          def parent_resource #:nodoc:
1371 1372 1373
            @scope[:scope_level_resource]
          end

J
José Valim 已提交
1374
          def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
1375 1376 1377 1378 1379
            if resources.length > 1
              resources.each { |r| send(method, r, options, &block) }
              return true
            end

1380 1381 1382 1383 1384
            if resource_scope?
              nested { send(method, resources.pop, options, &block) }
              return true
            end

1385
            options.keys.each do |k|
1386 1387 1388
              (options[:constraints] ||= {})[k] = options.delete(k) if options[k].is_a?(Regexp)
            end

1389 1390 1391
            scope_options = options.slice!(*RESOURCE_OPTIONS)
            unless scope_options.empty?
              scope(scope_options) do
1392 1393 1394 1395 1396
                send(method, resources.pop, options, &block)
              end
              return true
            end

1397 1398 1399 1400
            unless action_options?(options)
              options.merge!(scope_action_options) if scope_action_options?
            end

1401 1402 1403
            false
          end

J
José Valim 已提交
1404
          def action_options?(options) #:nodoc:
1405 1406 1407
            options[:only] || options[:except]
          end

J
José Valim 已提交
1408
          def scope_action_options? #:nodoc:
A
Aaron Patterson 已提交
1409
            @scope[:options] && (@scope[:options][:only] || @scope[:options][:except])
1410 1411
          end

J
José Valim 已提交
1412
          def scope_action_options #:nodoc:
1413 1414 1415
            @scope[:options].slice(:only, :except)
          end

J
José Valim 已提交
1416
          def resource_scope? #:nodoc:
1417
            [:resource, :resources].include? @scope[:scope_level]
1418 1419
          end

J
José Valim 已提交
1420
          def resource_method_scope? #:nodoc:
1421
            [:collection, :member, :new].include? @scope[:scope_level]
1422 1423
          end

1424
          def with_exclusive_scope
1425
            begin
1426 1427
              old_name_prefix, old_path = @scope[:as], @scope[:path]
              @scope[:as], @scope[:path] = nil, nil
1428

1429 1430 1431
              with_scope_level(:exclusive) do
                yield
              end
1432
            ensure
1433
              @scope[:as], @scope[:path] = old_name_prefix, old_path
1434 1435 1436
            end
          end

1437
          def with_scope_level(kind, resource = parent_resource)
J
Joshua Peek 已提交
1438
            old, @scope[:scope_level] = @scope[:scope_level], kind
1439
            old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
J
Joshua Peek 已提交
1440 1441 1442
            yield
          ensure
            @scope[:scope_level] = old
1443
            @scope[:scope_level_resource] = old_resource
J
Joshua Peek 已提交
1444
          end
1445

1446 1447
          def resource_scope(kind, resource) #:nodoc:
            with_scope_level(kind, resource) do
1448
              scope(parent_resource.resource_scope) do
1449 1450 1451 1452 1453
                yield
              end
            end
          end

J
José Valim 已提交
1454
          def nested_options #:nodoc:
1455 1456 1457 1458 1459 1460
            options = { :as => parent_resource.member_name }
            options[:constraints] = {
              :"#{parent_resource.singular}_id" => id_constraint
            } if id_constraint?

            options
1461 1462
          end

J
José Valim 已提交
1463
          def id_constraint? #:nodoc:
1464
            @scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp)
1465 1466
          end

J
José Valim 已提交
1467
          def id_constraint #:nodoc:
1468
            @scope[:constraints][:id]
1469 1470
          end

J
José Valim 已提交
1471
          def canonical_action?(action, flag) #:nodoc:
1472
            flag && resource_method_scope? && CANONICAL_ACTIONS.include?(action.to_s)
1473 1474
          end

J
José Valim 已提交
1475
          def shallow_scoping? #:nodoc:
1476
            shallow? && @scope[:scope_level] == :member
1477 1478
          end

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

1483 1484
            path = if canonical_action?(action, path.blank?)
              prefix.to_s
1485
            else
1486
              "#{prefix}/#{action_path(action, path)}"
1487 1488 1489
            end
          end

J
José Valim 已提交
1490
          def action_path(name, path = nil) #:nodoc:
1491
            name = name.to_sym if name.is_a?(String)
1492
            path || @scope[:path_names][name] || name.to_s
1493 1494
          end

J
José Valim 已提交
1495
          def prefix_name_for_action(as, action) #:nodoc:
1496
            if as
1497
              as.to_s
1498
            elsif !canonical_action?(action, @scope[:scope_level])
1499
              action.to_s
1500
            end
1501 1502
          end

J
José Valim 已提交
1503
          def name_for_action(as, action) #:nodoc:
1504
            prefix = prefix_name_for_action(as, action)
1505
            prefix = Mapper.normalize_name(prefix) if prefix
1506 1507 1508
            name_prefix = @scope[:as]

            if parent_resource
1509
              return nil unless as || action
1510

1511 1512
              collection_name = parent_resource.collection_name
              member_name = parent_resource.member_name
1513
            end
1514

1515
            name = case @scope[:scope_level]
1516
            when :nested
1517
              [name_prefix, prefix]
1518
            when :collection
1519
              [prefix, name_prefix, collection_name]
1520
            when :new
1521 1522 1523 1524 1525
              [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]
1526
            else
1527
              [name_prefix, member_name, prefix]
1528
            end
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
            if candidate = name.select(&:present?).join("_").presence
              # If a name was not explicitly given, we check if it is valid
              # and return nil in case it isn't. Otherwise, we pass the invalid name
              # forward so the underlying router engine treats it and raises an exception.
              if as.nil?
                candidate unless @set.routes.find { |r| r.name == candidate } || candidate !~ /\A[_a-z]/i
              else
                candidate
              end
            end
1540
          end
J
Joshua Peek 已提交
1541
      end
J
Joshua Peek 已提交
1542

1543 1544 1545 1546 1547
      def initialize(set) #:nodoc:
        @set = set
        @scope = { :path_names => @set.resources_path_names }
      end

1548 1549
      include Base
      include HttpHelpers
1550
      include Redirection
1551 1552
      include Scoping
      include Resources
J
Joshua Peek 已提交
1553 1554
    end
  end
J
Joshua Peek 已提交
1555
end