mapper.rb 53.9 KB
Newer Older
1
require 'active_support/core_ext/hash/except'
B
Bogdan Gusiev 已提交
2
require 'active_support/core_ext/hash/reverse_merge'
3
require 'active_support/core_ext/hash/slice'
4
require 'active_support/core_ext/object/blank'
S
Santiago Pastorino 已提交
5
require 'active_support/core_ext/enumerable'
6
require 'active_support/inflector'
7
require 'action_dispatch/routing/redirection'
8

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

21
        attr_reader :app, :constraints
22

23 24
        def initialize(app, constraints, request)
          @app, @constraints, @request = app, constraints, request
25 26
        end

27
        def matches?(env)
28
          req = @request.new(env)
29 30 31

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

38
          return true
39 40
        ensure
          req.reset_parameters
41 42 43 44
        end

        def call(env)
          matches?(env) ? @app.call(env) : [ 404, {'X-Cascade' => 'pass'}, [] ]
45
        end
46 47 48 49 50

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

53
      class Mapping #:nodoc:
54
        IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
55
        ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
56
        SHORTHAND_REGEX = %r{/[\w/]+$}
57
        WILDCARD_PATH = %r{\*([^/\)]+)\)?$}
58

59
        def initialize(set, scope, path, options)
60
          @set, @scope = set, scope
61
          @segment_keys = nil
62
          @options = (@scope[:options] || {}).merge(options)
63
          @path = normalize_path(path)
64
          normalize_options!
65 66 67 68 69 70 71 72 73 74

          via_all = @options.delete(:via) if @options[:via] == :all

          if !via_all && request_method_condition.empty?
            msg = "You should not use the `match` method in your router without specifying an HTTP method.\n" \
                  "If you want to expose your action to GET, use `get` in the router:\n\n" \
                  "  Instead of: match \"controller#action\"\n" \
                  "  Do: get \"controller#action\""
            raise msg
          end
75
        end
J
Joshua Peek 已提交
76

77
        def to_route
78
          [ app, conditions, requirements, defaults, @options[:as], @options[:anchor] ]
79
        end
J
Joshua Peek 已提交
80

81
        private
82 83 84

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

86 87
            if using_match_shorthand?(path_without_format, @options)
              to_shorthand    = @options[:to].blank?
88
              @options[:to] ||= path_without_format.gsub(/\(.*\)/, "")[1..-1].sub(%r{/([^/]*)$}, '#\1')
89 90
            end

91
            @options.merge!(default_controller_and_action(to_shorthand))
92 93 94 95 96

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

97
              if requirement.source =~ ANCHOR_CHARACTERS_REGEX
98 99 100 101 102 103
                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
104 105 106 107

            if @options[:constraints].is_a?(Hash)
              (@options[:defaults] ||= {}).reverse_merge!(defaults_from_constraints(@options[:constraints]))
            end
108
          end
109

110
          # match "account/overview"
111
          def using_match_shorthand?(path, options)
112
            path && (options[:to] || options[:action]).nil? && path =~ SHORTHAND_REGEX
113
          end
114

115
          def normalize_path(path)
116 117
            raise ArgumentError, "path is required" if path.blank?
            path = Mapper.normalize_path(path)
118 119 120 121 122 123 124 125

            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' }
126
              @options[:controller] ||= /.+?/
127 128
            end

129 130
            # Add a constraint for wildcard route to make it non-greedy and match the
            # optional format part of the route by default
131
            if path.match(WILDCARD_PATH) && @options[:format] != false
132
              @options[$1.to_sym] ||= /.+?/
133 134
            end

135 136 137
            if @options[:format] == false
              @options.delete(:format)
              path
138
            elsif path.include?(":format") || path.end_with?('/')
139
              path
140 141
            elsif @options[:format] == true
              "#{path}.:format"
142 143 144
            else
              "#{path}(.:format)"
            end
145
          end
146

147 148
          def app
            Constraints.new(
149
              to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults),
150 151
              blocks,
              @set.request_class
152
            )
153 154
          end

155 156 157
          def conditions
            { :path_info => @path }.merge(constraints).merge(request_method_condition)
          end
J
Joshua Peek 已提交
158

159
          def requirements
160
            @requirements ||= (@options[:constraints].is_a?(Hash) ? @options[:constraints] : {}).tap do |requirements|
161 162 163 164
              requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints]
              @options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) }
            end
          end
165

166
          def defaults
167 168 169 170 171 172
            @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

173
          def default_controller_and_action(to_shorthand=nil)
174
            if to.respond_to?(:call)
175 176
              { }
            else
177
              if to.is_a?(String)
178
                controller, action = to.split('#')
179 180
              elsif to.is_a?(Symbol)
                action = to.to_s
181
              end
J
Joshua Peek 已提交
182

183 184
              controller ||= default_controller
              action     ||= default_action
185

186 187 188
              unless controller.is_a?(Regexp) || to_shorthand
                controller = [@scope[:module], controller].compact.join("/").presence
              end
189

190 191 192 193
              if controller.is_a?(String) && controller =~ %r{\A/}
                raise ArgumentError, "controller name should not start with a slash"
              end

194 195
              controller = controller.to_s unless controller.is_a?(Regexp)
              action     = action.to_s     unless action.is_a?(Regexp)
196

197
              if controller.blank? && segment_keys.exclude?("controller")
198 199
                raise ArgumentError, "missing :controller"
              end
J
Joshua Peek 已提交
200

201
              if action.blank? && segment_keys.exclude?("action")
202 203
                raise ArgumentError, "missing :action"
              end
J
Joshua Peek 已提交
204

A
Aaron Patterson 已提交
205
              hash = {}
A
Aaron Patterson 已提交
206 207
              hash[:controller] = controller unless controller.blank?
              hash[:action]     = action unless action.blank?
A
Aaron Patterson 已提交
208
              hash
209 210
            end
          end
211

212
          def blocks
213 214 215 216 217
            constraints = @options[:constraints]
            if constraints.present? && !constraints.is_a?(Hash)
              [constraints]
            else
              @scope[:blocks] || []
218 219
            end
          end
J
Joshua Peek 已提交
220

221 222 223
          def constraints
            @constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller }
          end
224

225 226
          def request_method_condition
            if via = @options[:via]
227 228
              list = Array(via).map { |m| m.to_s.dasherize.upcase }
              { :request_method => list }
229 230
            else
              { }
231
            end
232
          end
J
Joshua Peek 已提交
233

234
          def segment_keys
235 236 237
            return @segment_keys if @segment_keys

            @segment_keys = Journey::Path::Pattern.new(
238
              Journey::Router::Strexp.compile(@path, requirements, SEPARATORS)
239
            ).names
240
          end
241

242 243 244
          def to
            @options[:to]
          end
J
Joshua Peek 已提交
245

246
          def default_controller
247
            @options[:controller] || @scope[:controller]
248
          end
249 250

          def default_action
251
            @options[:action] || @scope[:action]
252
          end
253 254 255 256 257

          def defaults_from_constraints(constraints)
            url_keys = [:protocol, :subdomain, :domain, :host, :port]
            constraints.slice(*url_keys).select{ |k, v| v.is_a?(String) || v.is_a?(Fixnum) }
          end
258
      end
259

260
      # Invokes Rack::Mount::Utils.normalize path and ensure that
261 262
      # (:locale) becomes (/:locale) instead of /(:locale). Except
      # for root cases, where the latter is the correct one.
263
      def self.normalize_path(path)
264
        path = Journey::Router::Utils.normalize_path(path)
265
        path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^/]+\)$}
266 267 268
        path
      end

269
      def self.normalize_name(name)
270
        normalize_path(name)[1..-1].tr("/", "_")
271 272
      end

273
      module Base
274 275 276 277
        # You can specify what Rails should route "/" to with the root method:
        #
        #   root :to => 'pages#main'
        #
278
        # For options, see +match+, as +root+ uses it internally.
279
        #
B
Brian Cardarella 已提交
280 281 282 283
        # You can also pass a string which will expand
        #
        #   root 'pages#main'
        #
284 285 286
        # 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.
287
        def root(options = {})
B
Brian Cardarella 已提交
288
          options = { :to => options } if options.is_a?(String)
289
          match '/', { :as => :root, :via => :get }.merge(options)
290
        end
291

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

410 411
        # Mount a Rack-based application to be used within the application.
        #
R
Ryan Bigg 已提交
412
        #   mount SomeRackApp, :at => "some_route"
413 414 415
        #
        # Alternatively:
        #
R
Ryan Bigg 已提交
416
        #   mount(SomeRackApp => "some_route")
417
        #
418 419
        # For options, see +match+, as +mount+ uses it internally.
        #
420 421 422 423 424
        # 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 已提交
425
        #   mount(SomeRackApp => "some_route", :as => "exciting")
426 427 428
        #
        # This will generate the +exciting_path+ and +exciting_url+ helpers
        # which can be used to navigate to this mounted app.
429 430 431 432
        def mount(app, options = nil)
          if options
            path = options.delete(:at)
          else
433 434 435 436
            unless Hash === app
              raise ArgumentError, "must be called with mount point"
            end

437 438 439 440 441 442 443
            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

444 445
          options[:as] ||= app_name(app)

446
          match(path, options.merge(:to => app, :anchor => false, :format => false, :via => :all))
447 448

          define_generate_prefix(app, options[:as])
449 450 451
          self
        end

452 453 454 455
        def default_url_options=(options)
          @set.default_url_options = options
        end
        alias_method :default_url_options, :default_url_options=
456

457 458 459 460 461 462
        def with_default_scope(scope, &block)
          scope(scope) do
            instance_exec(&block)
          end
        end

463 464 465
        private
          def app_name(app)
            return unless app.respond_to?(:routes)
466 467 468 469 470

            if app.respond_to?(:railtie_name)
              app.railtie_name
            else
              class_name = app.class.is_a?(Class) ? app.name : app.class.name
471
              ActiveSupport::Inflector.underscore(class_name).tr("/", "_")
472
            end
473 474 475
          end

          def define_generate_prefix(app, name)
476
            return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper)
477 478

            _route = @set.named_routes.routes[name.to_sym]
P
Piotr Sarnacki 已提交
479 480
            _routes = @set
            app.routes.define_mounted_helper(name)
J
José Valim 已提交
481 482 483 484 485
            app.routes.singleton_class.class_eval do
              define_method :mounted? do
                true
              end

486
              define_method :_generate_prefix do |options|
P
Piotr Sarnacki 已提交
487
                prefix_options = options.slice(*_route.segment_keys)
488 489
                # 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 已提交
490 491 492
                prefix = _routes.url_helpers.send("#{name}_path", prefix_options)
                prefix = '' if prefix == '/'
                prefix
493 494 495
              end
            end
          end
496 497 498
      end

      module HttpHelpers
499
        # Define a route that only recognizes HTTP GET.
500
        # For supported arguments, see <tt>Base#match</tt>.
501
        #
502
        #   get 'bacon', :to => 'food#bacon'
503
        def get(*args, &block)
504
          map_method(:get, args, &block)
505 506
        end

507
        # Define a route that only recognizes HTTP POST.
508
        # For supported arguments, see <tt>Base#match</tt>.
509
        #
510
        #   post 'bacon', :to => 'food#bacon'
511
        def post(*args, &block)
512
          map_method(:post, args, &block)
513 514
        end

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

523
        # Define a route that only recognizes HTTP PUT.
524
        # For supported arguments, see <tt>Base#match</tt>.
525
        #
526
        #   put 'bacon', :to => 'food#bacon'
527
        def put(*args, &block)
528
          map_method(:put, args, &block)
529 530
        end

531
        # Define a route that only recognizes HTTP DELETE.
532
        # For supported arguments, see <tt>Base#match</tt>.
533
        #
534
        #   delete 'broccoli', :to => 'food#broccoli'
535
        def delete(*args, &block)
536
          map_method(:delete, args, &block)
537 538 539
        end

        private
540
          def map_method(method, args, &block)
541
            options = args.extract_options!
542 543
            options[:via]    = method
            options[:path] ||= args.first if args.first.is_a?(String)
544
            match(*args, options, &block)
545 546 547 548
            self
          end
      end

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

640
          options[:path] = args.first if args.first.is_a?(String)
641
          recover = {}
642

643 644
          options[:constraints] ||= {}
          unless options[:constraints].is_a?(Hash)
645
            block, options[:constraints] = options[:constraints], {}
646
          end
647

648 649 650 651
          if options[:constraints].is_a?(Hash)
            (options[:defaults] ||= {}).reverse_merge!(defaults_from_constraints(options[:constraints]))
          end

652 653 654 655 656
          scope_options.each do |option|
            if value = options.delete(option)
              recover[option] = @scope[option]
              @scope[option]  = send("merge_#{option}_scope", @scope[option], value)
            end
657 658
          end

659 660 661 662 663 664
          recover[:block] = @scope[:blocks]
          @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block)

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

665 666 667
          yield
          self
        ensure
668 669 670
          scope_options.each do |option|
            @scope[option] = recover[option] if recover.has_key?(option)
          end
671 672 673

          @scope[:options] = recover[:options]
          @scope[:blocks]  = recover[:block]
674 675
        end

676 677 678 679 680
        # Scopes routes to a specific controller
        #
        #   controller "food" do
        #     match "bacon", :action => "bacon"
        #   end
681 682 683
        def controller(controller, options={})
          options[:controller] = controller
          scope(options) { yield }
684 685
        end

686 687 688 689 690 691 692 693
        # Scopes routes to a specific namespace. For example:
        #
        #   namespace :admin do
        #     resources :posts
        #   end
        #
        # This generates the following routes:
        #
694 695 696 697 698
        #       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
699
        #        admin_post PATCH/PUT /admin/posts/:id(.:format)      admin/posts#update
700
        #        admin_post DELETE    /admin/posts/:id(.:format)      admin/posts#destroy
701
        #
702
        # === Options
703
        #
704 705
        # The +:path+, +:as+, +:module+, +:shallow_path+ and +:shallow_prefix+
        # options all default to the name of the namespace.
706
        #
707 708
        # For options, see <tt>Base#match</tt>. For +:shallow_path+ option, see
        # <tt>Resources#resources</tt>.
709
        #
710
        # === Examples
711
        #
712 713 714 715
        #   # accessible through /sekret/posts rather than /admin/posts
        #   namespace :admin, :path => "sekret" do
        #     resources :posts
        #   end
716
        #
S
Sebastian Martinez 已提交
717
        #   # maps to <tt>Sekret::PostsController</tt> rather than <tt>Admin::PostsController</tt>
718 719 720
        #   namespace :admin, :module => "sekret" do
        #     resources :posts
        #   end
721
        #
S
Sebastian Martinez 已提交
722
        #   # generates +sekret_posts_path+ rather than +admin_posts_path+
723 724 725
        #   namespace :admin, :as => "sekret" do
        #     resources :posts
        #   end
726
        def namespace(path, options = {})
727
          path = path.to_s
728 729 730
          options = { :path => path, :as => path, :module => path,
                      :shallow_path => path, :shallow_prefix => path }.merge!(options)
          scope(options) { yield }
731
        end
732

R
Ryan Bigg 已提交
733 734 735 736
        # === 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 已提交
737
        #   constraints(:id => /\d+\.\d+/) do
R
Ryan Bigg 已提交
738 739 740 741 742
        #     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.
743
        #
R
R.T. Lechow 已提交
744
        # You may use this to also restrict other parameters:
R
Ryan Bigg 已提交
745 746
        #
        #   resources :posts do
M
mjy 已提交
747
        #     constraints(:post_id => /\d+\.\d+/) do
R
Ryan Bigg 已提交
748 749
        #       resources :comments
        #     end
J
James Miller 已提交
750
        #   end
R
Ryan Bigg 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764
        #
        # === 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 已提交
765
        # Requests to routes can be constrained based on specific criteria:
R
Ryan Bigg 已提交
766 767 768 769 770 771 772 773 774 775
        #
        #    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
776
        #      def self.matches?(request)
R
Ryan Bigg 已提交
777 778 779 780 781 782 783 784 785 786 787
        #        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
788 789 790 791
        def constraints(constraints = {})
          scope(:constraints => constraints) { yield }
        end

R
Ryan Bigg 已提交
792
        # Allows you to set default parameters for a route, such as this:
793 794 795
        #   defaults :id => 'home' do
        #     match 'scoped_pages/(:id)', :to => 'pages#show'
        #   end
R
Ryan Bigg 已提交
796
        # Using this, the +:id+ parameter here will default to 'home'.
797 798 799 800
        def defaults(defaults = {})
          scope(:defaults => defaults) { yield }
        end

801
        private
J
José Valim 已提交
802
          def scope_options #:nodoc:
803 804 805
            @scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
          end

J
José Valim 已提交
806
          def merge_path_scope(parent, child) #:nodoc:
807
            Mapper.normalize_path("#{parent}/#{child}")
808 809
          end

J
José Valim 已提交
810
          def merge_shallow_path_scope(parent, child) #:nodoc:
811 812 813
            Mapper.normalize_path("#{parent}/#{child}")
          end

J
José Valim 已提交
814
          def merge_as_scope(parent, child) #:nodoc:
815
            parent ? "#{parent}_#{child}" : child
816 817
          end

J
José Valim 已提交
818
          def merge_shallow_prefix_scope(parent, child) #:nodoc:
819 820 821
            parent ? "#{parent}_#{child}" : child
          end

J
José Valim 已提交
822
          def merge_module_scope(parent, child) #:nodoc:
823 824 825
            parent ? "#{parent}/#{child}" : child
          end

J
José Valim 已提交
826
          def merge_controller_scope(parent, child) #:nodoc:
827
            child
828 829
          end

J
José Valim 已提交
830
          def merge_path_names_scope(parent, child) #:nodoc:
831 832 833
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
834
          def merge_constraints_scope(parent, child) #:nodoc:
835 836 837
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
838
          def merge_defaults_scope(parent, child) #:nodoc:
839 840 841
            merge_options_scope(parent, child)
          end

J
José Valim 已提交
842
          def merge_blocks_scope(parent, child) #:nodoc:
843 844 845
            merged = parent ? parent.dup : []
            merged << child if child
            merged
846 847
          end

J
José Valim 已提交
848
          def merge_options_scope(parent, child) #:nodoc:
849
            (parent || {}).except(*override_keys(child)).merge(child)
850
          end
851

J
José Valim 已提交
852
          def merge_shallow_scope(parent, child) #:nodoc:
853 854
            child ? true : false
          end
855

J
José Valim 已提交
856
          def override_keys(child) #:nodoc:
857 858
            child.key?(:only) || child.key?(:except) ? [:only, :except] : []
          end
859 860 861 862 863

          def defaults_from_constraints(constraints)
            url_keys = [:protocol, :subdomain, :domain, :host, :port]
            constraints.slice(*url_keys).select{ |k, v| v.is_a?(String) || v.is_a?(Fixnum) }
          end
864 865
      end

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
      # 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 已提交
890 891
      # <tt>app/controllers/admin</tt> directory, and you can group them together
      # in your router:
892 893 894 895 896
      #
      #   namespace "admin" do
      #     resources :posts, :comments
      #   end
      #
S
Sebastian Martinez 已提交
897 898
      # 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
899 900 901 902
      # overrides this restriction, e.g:
      #
      #   resources :articles, :id => /[^\/]+/
      #
S
Sebastian Martinez 已提交
903
      # This allows any character other than a slash as part of your +:id+.
904
      #
J
Joshua Peek 已提交
905
      module Resources
906 907
        # CANONICAL_ACTIONS holds all actions that does not need a prefix or
        # a path appended since they fit properly in their scope level.
908
        VALID_ON_OPTIONS  = [:new, :collection, :member]
909
        RESOURCE_OPTIONS  = [:as, :controller, :path, :only, :except, :param]
910
        CANONICAL_ACTIONS = %w(index create new show update destroy)
911

912
        class Resource #:nodoc:
913
          attr_reader :controller, :path, :options, :param
914 915

          def initialize(entities, options = {})
916
            @name       = entities.to_s
917 918 919
            @path       = (options[:path] || @name).to_s
            @controller = (options[:controller] || @name).to_s
            @as         = options[:as]
920
            @param      = options[:param] || :id
921
            @options    = options
922 923
          end

924
          def default_actions
925
            [:index, :create, :new, :show, :update, :destroy, :edit]
926 927
          end

928
          def actions
929
            if only = @options[:only]
930
              Array(only).map(&:to_sym)
931
            elsif except = @options[:except]
932 933 934 935 936 937
              default_actions - Array(except).map(&:to_sym)
            else
              default_actions
            end
          end

938
          def name
939
            @as || @name
940 941
          end

942
          def plural
943
            @plural ||= name.to_s
944 945 946
          end

          def singular
947
            @singular ||= name.to_s.singularize
948 949
          end

950
          alias :member_name :singular
951

952
          # Checks for uncountable plurals, and appends "_index" if the plural
953
          # and singular form are the same.
954
          def collection_name
955
            singular == plural ? "#{plural}_index" : plural
956 957
          end

958
          def resource_scope
959
            { :controller => controller }
960 961
          end

962
          alias :collection_scope :path
963 964

          def member_scope
965
            "#{path}/:#{param}"
966 967
          end

968
          def new_scope(new_path)
969
            "#{path}/#{new_path}"
970 971 972
          end

          def nested_scope
973
            "#{path}/:#{singular}_#{param}"
974
          end
975

976 977 978
        end

        class SingletonResource < Resource #:nodoc:
979
          def initialize(entities, options)
980
            super
981
            @as         = nil
982 983
            @controller = (options[:controller] || plural).to_s
            @as         = options[:as]
984 985
          end

986 987 988 989
          def default_actions
            [:show, :create, :update, :destroy, :new, :edit]
          end

990 991
          def plural
            @plural ||= name.to_s.pluralize
992 993
          end

994 995
          def singular
            @singular ||= name.to_s
996
          end
997 998 999 1000 1001 1002

          alias :member_name :singular
          alias :collection_name :singular

          alias :member_scope :path
          alias :nested_scope :path
1003 1004
        end

1005 1006 1007 1008
        def resources_path_names(options)
          @scope[:path_names].merge!(options)
        end

1009 1010 1011 1012 1013 1014 1015 1016 1017
        # 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 已提交
1018
        # the +GeoCoders+ controller (note that the controller is named after
1019 1020
        # the plural):
        #
1021 1022 1023 1024
        #   GET       /geocoder/new
        #   POST      /geocoder
        #   GET       /geocoder
        #   GET       /geocoder/edit
1025
        #   PATCH/PUT /geocoder
1026
        #   DELETE    /geocoder
1027
        #
1028
        # === Options
1029
        # Takes same options as +resources+.
J
Joshua Peek 已提交
1030
        def resource(*resources, &block)
J
Joshua Peek 已提交
1031
          options = resources.extract_options!
J
Joshua Peek 已提交
1032

1033
          if apply_common_behavior_for(:resource, resources, options, &block)
1034 1035 1036
            return self
          end

1037
          resource_scope(:resource, SingletonResource.new(resources.pop, options)) do
1038
            yield if block_given?
1039

1040
            collection do
1041
              post :create
1042
            end if parent_resource.actions.include?(:create)
1043

1044
            new do
1045
              get :new
1046
            end if parent_resource.actions.include?(:new)
1047

1048
            member do
1049 1050
              get :edit if parent_resource.actions.include?(:edit)
              get :show if parent_resource.actions.include?(:show)
1051
              if parent_resource.actions.include?(:update)
1052 1053
                patch :update
                put   :update
1054
              end
1055
              delete :destroy if parent_resource.actions.include?(:destroy)
1056 1057 1058
            end
          end

J
Joshua Peek 已提交
1059
          self
1060 1061
        end

1062 1063 1064 1065 1066 1067 1068 1069
        # 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 已提交
1070
        # the +Photos+ controller:
1071
        #
1072 1073 1074 1075 1076
        #   GET       /photos
        #   GET       /photos/new
        #   POST      /photos
        #   GET       /photos/:id
        #   GET       /photos/:id/edit
1077
        #   PATCH/PUT /photos/:id
1078
        #   DELETE    /photos/:id
1079
        #
1080 1081 1082 1083 1084 1085 1086 1087
        # Resources can also be nested infinitely by using this block syntax:
        #
        #   resources :photos do
        #     resources :comments
        #   end
        #
        # This generates the following comments routes:
        #
1088 1089 1090 1091 1092
        #   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
1093
        #   PATCH/PUT /photos/:photo_id/comments/:id
1094
        #   DELETE    /photos/:photo_id/comments/:id
1095
        #
1096
        # === Options
1097 1098
        # Takes same options as <tt>Base#match</tt> as well as:
        #
1099
        # [:path_names]
A
Aviv Ben-Yosef 已提交
1100 1101
        #   Allows you to change the segment component of the +edit+ and +new+ actions.
        #   Actions not specified are not changed.
1102 1103 1104 1105
        #
        #     resources :posts, :path_names => { :new => "brand_new" }
        #
        #   The above example will now change /posts/new to /posts/brand_new
1106
        #
1107 1108 1109 1110 1111 1112 1113
        # [: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
        #
1114 1115
        # [:only]
        #   Only generate routes for the given actions.
1116
        #
1117 1118
        #     resources :cows, :only => :show
        #     resources :cows, :only => [:show, :index]
1119
        #
1120 1121
        # [:except]
        #   Generate all routes except for the given actions.
1122
        #
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        #     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
1137
        #       resources :comments, :except => [:show, :edit, :update, :destroy]
1138
        #     end
1139 1140 1141 1142 1143
        #     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>.
1144 1145 1146 1147
        #
        # [:shallow_path]
        #   Prefixes nested shallow routes with the specified path.
        #
1148 1149 1150 1151
        #     scope :shallow_path => "sekret" do
        #       resources :posts do
        #         resources :comments, :shallow => true
        #       end
1152 1153 1154 1155
        #     end
        #
        #   The +comments+ resource here will have the following routes generated for it:
        #
1156 1157 1158 1159 1160
        #     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)
1161
        #     comment          PATCH/PUT /sekret/comments/:id(.:format)
1162
        #     comment          DELETE    /sekret/comments/:id(.:format)
1163
        #
1164 1165 1166
        # [:shallow_prefix]
        #   Prefixes nested shallow route names with specified prefix.
        #
V
Vijay Dev 已提交
1167
        #     scope :shallow_prefix => "sekret" do
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        #       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)
        #
1183
        # === Examples
1184
        #
S
Sebastian Martinez 已提交
1185
        #   # routes call <tt>Admin::PostsController</tt>
1186
        #   resources :posts, :module => "admin"
1187
        #
1188
        #   # resource actions are at /admin/posts.
1189
        #   resources :posts, :path => "admin/posts"
J
Joshua Peek 已提交
1190
        def resources(*resources, &block)
J
Joshua Peek 已提交
1191
          options = resources.extract_options!
1192

1193
          if apply_common_behavior_for(:resources, resources, options, &block)
1194 1195 1196
            return self
          end

1197
          resource_scope(:resources, Resource.new(resources.pop, options)) do
1198
            yield if block_given?
J
Joshua Peek 已提交
1199

1200
            collection do
1201 1202
              get  :index if parent_resource.actions.include?(:index)
              post :create if parent_resource.actions.include?(:create)
1203
            end
1204

1205
            new do
1206
              get :new
1207
            end if parent_resource.actions.include?(:new)
1208

1209
            member do
1210 1211
              get :edit if parent_resource.actions.include?(:edit)
              get :show if parent_resource.actions.include?(:show)
1212
              if parent_resource.actions.include?(:update)
1213 1214
                patch :update
                put   :update
1215
              end
1216
              delete :destroy if parent_resource.actions.include?(:destroy)
1217 1218 1219
            end
          end

J
Joshua Peek 已提交
1220
          self
1221 1222
        end

1223 1224 1225 1226 1227 1228 1229 1230 1231
        # 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 已提交
1232
        # with GET, and route to the search action of +PhotosController+. It will also
1233 1234
        # create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt>
        # route helpers.
J
Joshua Peek 已提交
1235
        def collection
1236 1237
          unless resource_scope?
            raise ArgumentError, "can't use collection outside resource(s) scope"
1238 1239
          end

1240 1241 1242 1243
          with_scope_level(:collection) do
            scope(parent_resource.collection_scope) do
              yield
            end
J
Joshua Peek 已提交
1244
          end
1245
        end
J
Joshua Peek 已提交
1246

1247 1248 1249 1250 1251 1252 1253 1254 1255
        # 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 已提交
1256
        # preview action of +PhotosController+. It will also create the
1257
        # <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.
J
Joshua Peek 已提交
1258
        def member
1259 1260
          unless resource_scope?
            raise ArgumentError, "can't use member outside resource(s) scope"
J
Joshua Peek 已提交
1261
          end
J
Joshua Peek 已提交
1262

1263 1264 1265 1266
          with_scope_level(:member) do
            scope(parent_resource.member_scope) do
              yield
            end
1267 1268 1269 1270 1271 1272 1273
          end
        end

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

1275 1276 1277 1278
          with_scope_level(:new) do
            scope(parent_resource.new_scope(action_path(:new))) do
              yield
            end
J
Joshua Peek 已提交
1279
          end
J
Joshua Peek 已提交
1280 1281
        end

1282
        def nested
1283 1284
          unless resource_scope?
            raise ArgumentError, "can't use nested outside resource(s) scope"
1285 1286 1287
          end

          with_scope_level(:nested) do
1288
            if shallow?
1289
              with_exclusive_scope do
1290
                if @scope[:shallow_path].blank?
1291
                  scope(parent_resource.nested_scope, nested_options) { yield }
1292
                else
1293
                  scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
1294
                    scope(parent_resource.nested_scope, nested_options) { yield }
1295 1296 1297 1298
                  end
                end
              end
            else
1299
              scope(parent_resource.nested_scope, nested_options) { yield }
1300 1301 1302 1303
            end
          end
        end

1304
        # See ActionDispatch::Routing::Mapper::Scoping#namespace
1305
        def namespace(path, options = {})
1306
          if resource_scope?
1307 1308 1309 1310 1311 1312
            nested { super }
          else
            super
          end
        end

1313
        def shallow
1314
          scope(:shallow => true, :shallow_path => @scope[:path]) do
1315 1316 1317 1318
            yield
          end
        end

1319 1320 1321 1322
        def shallow?
          parent_resource.instance_of?(Resource) && @scope[:shallow]
        end

1323
        # match 'path' => 'controller#action'
R
Rafael Mendonça França 已提交
1324
        # match 'path', to: 'controller#action'
1325
        # match 'path', 'otherpath', on: :member, via: :get
1326 1327 1328 1329
        def match(path, *rest)
          if rest.empty? && Hash === path
            options  = path
            path, to = options.find { |name, value| name.is_a?(String) }
1330 1331
            options[:to] = to
            options.delete(path)
1332 1333 1334 1335 1336 1337
            paths = [path]
          else
            options = rest.pop || {}
            paths = [path] + rest
          end

1338 1339
          options[:anchor] = true unless options.key?(:anchor)

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

1344
          paths.each { |_path| decomposed_match(_path, options.dup) }
1345 1346
          self
        end
1347

1348
        def decomposed_match(path, options) # :nodoc:
A
Aaron Patterson 已提交
1349 1350
          if on = options.delete(:on)
            send(on) { decomposed_match(path, options) }
1351
          else
A
Aaron Patterson 已提交
1352 1353 1354 1355 1356 1357 1358 1359
            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 已提交
1360
          end
1361
        end
J
Joshua Peek 已提交
1362

1363
        def add_route(action, options) # :nodoc:
1364
          path = path_for_action(action, options.delete(:path))
1365

1366 1367 1368
          if action.to_s =~ /^[\w\/]+$/
            options[:action] ||= action unless action.to_s.include?("/")
          else
1369 1370 1371
            action = nil
          end

1372
          if !options.fetch(:as, true)
1373 1374 1375
            options.delete(:as)
          else
            options[:as] = name_for_action(options[:as], action)
J
Joshua Peek 已提交
1376
          end
J
Joshua Peek 已提交
1377

1378
          mapping = Mapping.new(@set, @scope, URI.parser.escape(path), options)
1379 1380
          app, conditions, requirements, defaults, as, anchor = mapping.to_route
          @set.add_route(app, conditions, requirements, defaults, as, anchor)
J
Joshua Peek 已提交
1381 1382
        end

1383
        def root(options={})
1384
          if @scope[:scope_level] == :resources
1385 1386
            with_scope_level(:root) do
              scope(parent_resource.path) do
1387 1388 1389 1390 1391 1392
                super(options)
              end
            end
          else
            super(options)
          end
1393 1394
        end

1395
        protected
1396

1397
          def parent_resource #:nodoc:
1398 1399 1400
            @scope[:scope_level_resource]
          end

J
José Valim 已提交
1401
          def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
1402 1403 1404 1405 1406
            if resources.length > 1
              resources.each { |r| send(method, r, options, &block) }
              return true
            end

1407 1408 1409 1410 1411
            if resource_scope?
              nested { send(method, resources.pop, options, &block) }
              return true
            end

1412
            options.keys.each do |k|
1413 1414 1415
              (options[:constraints] ||= {})[k] = options.delete(k) if options[k].is_a?(Regexp)
            end

1416 1417 1418
            scope_options = options.slice!(*RESOURCE_OPTIONS)
            unless scope_options.empty?
              scope(scope_options) do
1419 1420 1421 1422 1423
                send(method, resources.pop, options, &block)
              end
              return true
            end

1424 1425 1426 1427
            unless action_options?(options)
              options.merge!(scope_action_options) if scope_action_options?
            end

1428 1429 1430
            false
          end

J
José Valim 已提交
1431
          def action_options?(options) #:nodoc:
1432 1433 1434
            options[:only] || options[:except]
          end

J
José Valim 已提交
1435
          def scope_action_options? #:nodoc:
A
Aaron Patterson 已提交
1436
            @scope[:options] && (@scope[:options][:only] || @scope[:options][:except])
1437 1438
          end

J
José Valim 已提交
1439
          def scope_action_options #:nodoc:
1440 1441 1442
            @scope[:options].slice(:only, :except)
          end

J
José Valim 已提交
1443
          def resource_scope? #:nodoc:
1444
            [:resource, :resources].include? @scope[:scope_level]
1445 1446
          end

J
José Valim 已提交
1447
          def resource_method_scope? #:nodoc:
1448
            [:collection, :member, :new].include? @scope[:scope_level]
1449 1450
          end

1451
          def with_exclusive_scope
1452
            begin
1453 1454
              old_name_prefix, old_path = @scope[:as], @scope[:path]
              @scope[:as], @scope[:path] = nil, nil
1455

1456 1457 1458
              with_scope_level(:exclusive) do
                yield
              end
1459
            ensure
1460
              @scope[:as], @scope[:path] = old_name_prefix, old_path
1461 1462 1463
            end
          end

1464
          def with_scope_level(kind, resource = parent_resource)
J
Joshua Peek 已提交
1465
            old, @scope[:scope_level] = @scope[:scope_level], kind
1466
            old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
J
Joshua Peek 已提交
1467 1468 1469
            yield
          ensure
            @scope[:scope_level] = old
1470
            @scope[:scope_level_resource] = old_resource
J
Joshua Peek 已提交
1471
          end
1472

1473 1474
          def resource_scope(kind, resource) #:nodoc:
            with_scope_level(kind, resource) do
1475
              scope(parent_resource.resource_scope) do
1476 1477 1478 1479 1480
                yield
              end
            end
          end

J
José Valim 已提交
1481
          def nested_options #:nodoc:
1482 1483 1484 1485 1486 1487
            options = { :as => parent_resource.member_name }
            options[:constraints] = {
              :"#{parent_resource.singular}_id" => id_constraint
            } if id_constraint?

            options
1488 1489
          end

J
José Valim 已提交
1490
          def id_constraint? #:nodoc:
1491
            @scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp)
1492 1493
          end

J
José Valim 已提交
1494
          def id_constraint #:nodoc:
1495
            @scope[:constraints][:id]
1496 1497
          end

J
José Valim 已提交
1498
          def canonical_action?(action, flag) #:nodoc:
1499
            flag && resource_method_scope? && CANONICAL_ACTIONS.include?(action.to_s)
1500 1501
          end

J
José Valim 已提交
1502
          def shallow_scoping? #:nodoc:
1503
            shallow? && @scope[:scope_level] == :member
1504 1505
          end

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

1510
            if canonical_action?(action, path.blank?)
1511
              prefix.to_s
1512
            else
1513
              "#{prefix}/#{action_path(action, path)}"
1514 1515 1516
            end
          end

J
José Valim 已提交
1517
          def action_path(name, path = nil) #:nodoc:
1518
            name = name.to_sym if name.is_a?(String)
1519
            path || @scope[:path_names][name] || name.to_s
1520 1521
          end

J
José Valim 已提交
1522
          def prefix_name_for_action(as, action) #:nodoc:
1523
            if as
1524
              as.to_s
1525
            elsif !canonical_action?(action, @scope[:scope_level])
1526
              action.to_s
1527
            end
1528 1529
          end

J
José Valim 已提交
1530
          def name_for_action(as, action) #:nodoc:
1531
            prefix = prefix_name_for_action(as, action)
1532
            prefix = Mapper.normalize_name(prefix) if prefix
1533 1534 1535
            name_prefix = @scope[:as]

            if parent_resource
1536
              return nil unless as || action
1537

1538 1539
              collection_name = parent_resource.collection_name
              member_name = parent_resource.member_name
1540
            end
1541

1542
            name = case @scope[:scope_level]
1543
            when :nested
1544
              [name_prefix, prefix]
1545
            when :collection
1546
              [prefix, name_prefix, collection_name]
1547
            when :new
1548 1549 1550 1551 1552
              [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]
1553
            else
1554
              [name_prefix, member_name, prefix]
1555
            end
1556

1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
            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
1567
          end
J
Joshua Peek 已提交
1568
      end
J
Joshua Peek 已提交
1569

1570 1571 1572 1573 1574
      def initialize(set) #:nodoc:
        @set = set
        @scope = { :path_names => @set.resources_path_names }
      end

1575 1576
      include Base
      include HttpHelpers
1577
      include Redirection
1578 1579
      include Scoping
      include Resources
J
Joshua Peek 已提交
1580 1581
    end
  end
J
Joshua Peek 已提交
1582
end