diff options
| author | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-15 13:38:10 +0000 | 
|---|---|---|
| committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-15 17:21:33 +0000 | 
| commit | 90d2802b71a6e89aedfe40564a37bd35f777e541 (patch) | |
| tree | f0d52164eeb95337282aa5140eda1188092ee0ca /actionpack/lib/action_dispatch/journey | |
| parent | b28fc685a98e6ff1a3777116b97b5c1c41e01391 (diff) | |
| download | rails-90d2802b71a6e89aedfe40564a37bd35f777e541.tar.gz rails-90d2802b71a6e89aedfe40564a37bd35f777e541.tar.bz2 rails-90d2802b71a6e89aedfe40564a37bd35f777e541.zip  | |
Add support for other types of routing constraints
This now allows the use of arrays like this:
  get '/foo/:action', to: 'foo', constraints: { subdomain: %w[www admin] }
or constraints where the request method returns an Fixnum like this:
  get '/foo', to: 'foo#index', constraints: { port: 8080 }
Note that this only applies to constraints on the request - path
constraints still need to be specified as Regexps as the various
constraints are compiled into a single Regexp.
Diffstat (limited to 'actionpack/lib/action_dispatch/journey')
| -rw-r--r-- | actionpack/lib/action_dispatch/journey/route.rb | 29 | ||||
| -rw-r--r-- | actionpack/lib/action_dispatch/journey/router.rb | 6 | 
2 files changed, 25 insertions, 10 deletions
diff --git a/actionpack/lib/action_dispatch/journey/route.rb b/actionpack/lib/action_dispatch/journey/route.rb index d3988cf31e..b08ac4b4eb 100644 --- a/actionpack/lib/action_dispatch/journey/route.rb +++ b/actionpack/lib/action_dispatch/journey/route.rb @@ -1,7 +1,7 @@  module ActionDispatch    module Journey # :nodoc:      class Route # :nodoc: -      attr_reader :app, :path, :verb, :defaults, :ip, :name +      attr_reader :app, :path, :defaults, :name        attr_reader :constraints        alias :conditions :constraints @@ -12,15 +12,11 @@ module ActionDispatch        # +path+ is a path constraint.        # +constraints+ is a hash of constraints to be applied to this route.        def initialize(name, app, path, constraints, defaults = {}) -        constraints  = constraints.dup          @name        = name          @app         = app          @path        = path -        @verb        = constraints[:request_method] || // -        @ip          = constraints.delete(:ip) || //          @constraints = constraints -        @constraints.keep_if { |_,v| Regexp === v || String === v }          @defaults    = defaults          @required_defaults = nil          @required_parts    = nil @@ -89,6 +85,29 @@ module ActionDispatch            @defaults.dup.delete_if { |k,_| matches.include?(k) }          end        end + +      def matches?(request) +        constraints.all? do |method, value| +          next true unless request.respond_to?(method) + +          case value +          when Regexp, String +            value === request.send(method).to_s +          when Array +            value.include?(request.send(method)) +          else +            value === request.send(method) +          end +        end +      end + +      def ip +        constraints[:ip] || // +      end + +      def verb +        constraints[:request_method] || // +      end      end    end  end diff --git a/actionpack/lib/action_dispatch/journey/router.rb b/actionpack/lib/action_dispatch/journey/router.rb index 1fc45a2109..31868b1814 100644 --- a/actionpack/lib/action_dispatch/journey/router.rb +++ b/actionpack/lib/action_dispatch/journey/router.rb @@ -131,11 +131,7 @@ module ActionDispatch            }            routes.concat get_routes_as_head(routes) -          routes.sort_by!(&:precedence).select! { |r| -            r.constraints.all? { |k, v| v === req.send(k) } && -              r.verb === req.request_method -          } -          routes.reject! { |r| req.ip && !(r.ip === req.ip) } +          routes.sort_by!(&:precedence).select! { |r| r.matches?(req) }            routes.map! { |r|              match_data  = r.path.match(req.path_info)  | 
