From 5df26dd7a91bb6d248b96c4308242a51ab59aa5c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 18 Nov 2009 16:39:40 -0600 Subject: Add basic nested named route support to new routing dsl. Also add a bunch of pending tests. --- actionpack/lib/action_dispatch/routing/mapper.rb | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7d770dedd0..6e112c9b54 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -52,30 +52,38 @@ module ActionDispatch resource = resources.pop + plural = resource.to_s + singular = plural.singularize + if @scope[:scope_level] == :resources - member do - resources(resource, options, &block) + parent_resource = @scope[:scope_level_options][:name] + with_scope_level(:member) do + scope(":#{parent_resource}_id", :name_prefix => parent_resource) do + resources(resource, options, &block) + end end return self end - plural = resource.to_s - singular = plural.singularize + if @scope[:options] && (prefix = @scope[:options][:name_prefix]) + plural = "#{prefix}_#{plural}" + singular = "#{prefix}_#{singular}" + end controller(resource) do namespace(resource) do - with_scope_level(:resources) do + with_scope_level(:resources, :name => singular) do yield if block_given? member do - get "", :to => :show, :as => "#{singular}" + get "", :to => :show, :as => singular put "", :to => :update delete "", :to => :destroy get "edit", :to => :edit, :as => "edit_#{singular}" end collection do - get "", :to => :index, :as => "#{plural}" + get "", :to => :index, :as => plural post "", :to => :create get "new", :to => :new, :as => "new_#{singular}" end @@ -127,11 +135,13 @@ module ActionDispatch end private - def with_scope_level(kind) + def with_scope_level(kind, options = {}) old, @scope[:scope_level] = @scope[:scope_level], kind + old_options, @scope[:scope_level_options] = @scope[:scope_level_options], options yield ensure @scope[:scope_level] = old + @scope[:scope_level_options] = old_options end end -- cgit v1.2.3 From 4b325fcd1af4eeb44dae5a741d85ea09d3845e4e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 14 Nov 2009 22:00:38 -0600 Subject: Update routing for rackmount 0.2 api changes --- .../lib/action_dispatch/routing/route_set.rb | 65 ++++------------------ 1 file changed, 11 insertions(+), 54 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c28df76f3f..c15aaceb5b 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -372,7 +372,17 @@ module ActionDispatch end recall[:action] = options.delete(:action) if options[:action] == 'index' - path = _uri(named_route, options, recall) + parameterize = lambda { |name, value| + if name == :controller + value + elsif value.is_a?(Array) + value.map { |v| Rack::Mount::Utils.escape_uri(v.to_param) }.join('/') + else + Rack::Mount::Utils.escape_uri(value.to_param) + end + } + + path = @set.url(named_route, options, recall, :parameterize => parameterize) if path && method == :generate_extras uri = URI(path) extras = uri.query ? @@ -439,59 +449,6 @@ module ActionDispatch def extract_request_environment(request) { :method => request.method } end - - private - def _uri(named_route, params, recall) - params = URISegment.wrap_values(params) - recall = URISegment.wrap_values(recall) - - unless result = @set.generate(:path_info, named_route, params, recall) - return - end - - uri, params = result - params.each do |k, v| - if v._value - params[k] = v._value - else - params.delete(k) - end - end - - uri << "?#{Rack::Mount::Utils.build_nested_query(params)}" if uri && params.any? - uri - end - - class URISegment < Struct.new(:_value, :_escape) - EXCLUDED = [:controller] - - def self.wrap_values(hash) - hash.inject({}) { |h, (k, v)| - h[k] = new(v, !EXCLUDED.include?(k.to_sym)) - h - } - end - - extend Forwardable - def_delegators :_value, :==, :eql?, :hash - - def to_param - @to_param ||= begin - if _value.is_a?(Array) - _value.map { |v| _escaped(v) }.join('/') - else - _escaped(_value) - end - end - end - alias_method :to_s, :to_param - - private - def _escaped(value) - v = value.respond_to?(:to_param) ? value.to_param : value - _escape ? Rack::Mount::Utils.escape_uri(v) : v.to_s - end - end end end end -- cgit v1.2.3 From 8d351eac078642505057351e7113100550ed8bc7 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 23 Nov 2009 19:44:43 -0600 Subject: Extract Routing.controller_constraints --- actionpack/lib/action_dispatch/routing/deprecated_mapper.rb | 3 +-- actionpack/lib/action_dispatch/routing/mapper.rb | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb index 0564ba9797..dd76391870 100644 --- a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb +++ b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb @@ -113,8 +113,7 @@ module ActionDispatch end end - possible_names = Routing.possible_controllers.collect { |n| Regexp.escape(n) } - requirements[:controller] ||= Regexp.union(*possible_names) + requirements[:controller] ||= Routing.controller_constraints if defaults[:controller] defaults[:action] ||= 'index' diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 6e112c9b54..cfe7425a61 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -282,6 +282,8 @@ module ActionDispatch constraints.reject! { |k, v| segment_keys.include?(k.to_s) } conditions.merge!(constraints) + requirements[:controller] ||= Routing.controller_constraints + if via = options[:via] via = Array(via).map { |m| m.to_s.upcase } conditions[:request_method] = Regexp.union(*via) -- cgit v1.2.3 From 15ab3a98a1b6f9b829060ba974048b33466f6814 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 23 Nov 2009 20:20:50 -0600 Subject: Find all controllers in memory to use for routing --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c15aaceb5b..79e15edeaa 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -246,7 +246,9 @@ module ActionDispatch end def load! - Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones + # Clear the controller cache so we may discover new ones + Routing.clear_controller_cache! + load_routes! end -- cgit v1.2.3 From 3f025e64083c4a0cde4841254a88ef54780824f9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 15:23:27 -0600 Subject: Resource collection should be defined before member routes --- actionpack/lib/action_dispatch/routing/mapper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index cfe7425a61..087d4ab478 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -75,18 +75,18 @@ module ActionDispatch with_scope_level(:resources, :name => singular) do yield if block_given? + collection do + get "", :to => :index, :as => plural + post "", :to => :create + get "new", :to => :new, :as => "new_#{singular}" + end + member do get "", :to => :show, :as => singular put "", :to => :update delete "", :to => :destroy get "edit", :to => :edit, :as => "edit_#{singular}" end - - collection do - get "", :to => :index, :as => plural - post "", :to => :create - get "new", :to => :new, :as => "new_#{singular}" - end end end end -- cgit v1.2.3 From 40ae2070d57867bac1c9e7f77c25ac7ac7b5a647 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 16:59:44 -0600 Subject: Extract Resource and SingletonResource helper objects --- actionpack/lib/action_dispatch/routing/mapper.rb | 112 +++++++++++++++++------ 1 file changed, 84 insertions(+), 28 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 087d4ab478..9b00bd3dc0 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -2,6 +2,71 @@ module ActionDispatch module Routing class Mapper module Resources + class Resource #:nodoc: + attr_reader :plural, :singular + attr_reader :path_prefix, :name_prefix + + def initialize(entities, options = {}) + entities = entities.to_s + + @plural = entities.pluralize + @singular = entities.singularize + + @path_prefix = options[:path_prefix] + @name_prefix = options[:name_prefix] + end + + def name + plural + end + + def controller + plural + end + + def member_name + if name_prefix + "#{name_prefix}_#{singular}" + else + singular + end + end + + def collection_name + if name_prefix + "#{name_prefix}_#{plural}" + else + plural + end + end + + def new_name + if name_prefix + "new_#{name_prefix}_#{singular}" + else + "new_#{singular}" + end + end + + def edit_name + if name_prefix + "edit_#{name_prefix}_#{singular}" + else + "edit_#{singular}" + end + end + end + + class SingletonResource < Resource #:nodoc: + def initialize(entity, options = {}) + super(entity) + end + + def name + singular + end + end + def resource(*resources, &block) options = resources.last.is_a?(Hash) ? resources.pop : {} @@ -11,29 +76,27 @@ module ActionDispatch return self end - resource = resources.pop + name_prefix = @scope[:options][:name_prefix] if @scope[:options] + resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources member do - resource(resource, options, &block) + resource(resource.name, options, &block) end return self end - singular = resource.to_s - plural = singular.pluralize - - controller(plural) do - namespace(resource) do - with_scope_level(:resource) do + controller(resource.controller) do + namespace(resource.name) do + with_scope_level(:resource, :name => resource.singular) do yield if block_given? - get "", :to => :show, :as => "#{singular}" + get "", :to => :show, :as => resource.member_name post "", :to => :create put "", :to => :update delete "", :to => :destroy - get "new", :to => :new, :as => "new_#{singular}" - get "edit", :to => :edit, :as => "edit_#{singular}" + get "new", :to => :new, :as => resource.new_name + get "edit", :to => :edit, :as => resource.edit_name end end end @@ -50,42 +113,35 @@ module ActionDispatch return self end - resource = resources.pop - - plural = resource.to_s - singular = plural.singularize + name_prefix = @scope[:options][:name_prefix] if @scope[:options] + resource = Resource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] with_scope_level(:member) do scope(":#{parent_resource}_id", :name_prefix => parent_resource) do - resources(resource, options, &block) + resources(resource.name, options, &block) end end return self end - if @scope[:options] && (prefix = @scope[:options][:name_prefix]) - plural = "#{prefix}_#{plural}" - singular = "#{prefix}_#{singular}" - end - - controller(resource) do - namespace(resource) do - with_scope_level(:resources, :name => singular) do + controller(resource.controller) do + namespace(resource.name) do + with_scope_level(:resources, :name => resource.singular) do yield if block_given? collection do - get "", :to => :index, :as => plural + get "", :to => :index, :as => resource.collection_name post "", :to => :create - get "new", :to => :new, :as => "new_#{singular}" + get "new", :to => :new, :as => resource.new_name end member do - get "", :to => :show, :as => singular + get "", :to => :show, :as => resource.member_name put "", :to => :update delete "", :to => :destroy - get "edit", :to => :edit, :as => "edit_#{singular}" + get "edit", :to => :edit, :as => resource.edit_name end end end -- cgit v1.2.3 From 5da01a92c741b3a9a020a4dec9ddf120c0484e20 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:01:14 -0600 Subject: Make use of extract_options! --- actionpack/lib/action_dispatch/routing/mapper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9b00bd3dc0..9ec85daba8 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -68,7 +68,7 @@ module ActionDispatch end def resource(*resources, &block) - options = resources.last.is_a?(Hash) ? resources.pop : {} + options = resources.extract_options! if resources.length > 1 raise ArgumentError if block_given? @@ -105,7 +105,7 @@ module ActionDispatch end def resources(*resources, &block) - options = resources.last.is_a?(Hash) ? resources.pop : {} + options = resources.extract_options! if resources.length > 1 raise ArgumentError if block_given? @@ -173,7 +173,7 @@ module ActionDispatch end def match(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! args.push(options) case options.delete(:on) @@ -203,7 +203,7 @@ module ActionDispatch module Scoping def scope(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! constraints = options.delete(:constraints) || {} unless constraints.is_a?(Hash) @@ -300,7 +300,7 @@ module ActionDispatch end def match(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! if args.length > 1 args.each { |path| match(path, options) } @@ -384,7 +384,7 @@ module ActionDispatch private def map_method(method, *args, &block) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! options[:via] = method args.push(options) match(*args, &block) -- cgit v1.2.3 From 312c3bfa247249a1562eb9d04c335f4d38a18b28 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:39:37 -0600 Subject: Break down long match routing method --- actionpack/lib/action_dispatch/routing/mapper.rb | 76 +++++++++++++++++------- 1 file changed, 53 insertions(+), 23 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9ec85daba8..57bbb55c1c 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -202,6 +202,12 @@ module ActionDispatch end module Scoping + def self.extended(object) + object.instance_eval do + @scope = {} + end + end + def scope(*args) options = args.extract_options! @@ -249,9 +255,24 @@ module ActionDispatch def constraints(constraints = {}) scope(:constraints => constraints) { yield } end + + def match(*args) + options = args.extract_options! + options = (@scope[:options] || {}).merge(options) + args.push(options) + super(*args) + end end class Constraints + def new(app, constraints = []) + if constraints.any? + super(app, constraints) + else + app + end + end + def initialize(app, constraints = []) @app, @constraints = app, constraints end @@ -273,7 +294,6 @@ module ActionDispatch def initialize(set) @set = set - @scope = {} extend Scoping extend Resources @@ -313,7 +333,6 @@ module ActionDispatch path = args.first - options = (@scope[:options] || {}).merge(options) conditions, defaults = {}, {} path = nil if path == "" @@ -345,29 +364,12 @@ module ActionDispatch conditions[:request_method] = Regexp.union(*via) end - defaults[:controller] = @scope[:controller].to_s if @scope[:controller] - - if options[:to].respond_to?(:call) - app = options[:to] - defaults.delete(:controller) - defaults.delete(:action) - elsif options[:to].is_a?(String) - defaults[:controller], defaults[:action] = options[:to].split('#') - elsif options[:to].is_a?(Symbol) - defaults[:action] = options[:to].to_s - end - app ||= Routing::RouteSet::Dispatcher.new(:defaults => defaults) + defaults[:controller] ||= @scope[:controller].to_s if @scope[:controller] - if app.is_a?(Routing::RouteSet::Dispatcher) - unless defaults.include?(:controller) || segment_keys.include?("controller") - raise ArgumentError, "missing :controller" - end - unless defaults.include?(:action) || segment_keys.include?("action") - raise ArgumentError, "missing :action" - end - end + app = initialize_app_endpoint(options, defaults) + validate_defaults!(app, defaults, segment_keys) + app = Constraints.new(app, blocks) - app = Constraints.new(app, blocks) if blocks.any? @set.add_route(app, conditions, requirements, defaults, options[:as]) self @@ -383,6 +385,34 @@ module ActionDispatch end private + def initialize_app_endpoint(options, defaults) + app = nil + + if options[:to].respond_to?(:call) + app = options[:to] + defaults.delete(:controller) + defaults.delete(:action) + elsif options[:to].is_a?(String) + defaults[:controller], defaults[:action] = options[:to].split('#') + elsif options[:to].is_a?(Symbol) + defaults[:action] = options[:to].to_s + end + + app || Routing::RouteSet::Dispatcher.new(:defaults => defaults) + end + + def validate_defaults!(app, defaults, segment_keys) + return unless app.is_a?(Routing::RouteSet::Dispatcher) + + unless defaults.include?(:controller) || segment_keys.include?("controller") + raise ArgumentError, "missing :controller" + end + + unless defaults.include?(:action) || segment_keys.include?("action") + raise ArgumentError, "missing :action" + end + end + def map_method(method, *args, &block) options = args.extract_options! options[:via] = method -- cgit v1.2.3 From f69f9820ee84f32bb53d001efd6ebc79517fb0e1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:45:12 -0600 Subject: Wrap up http related routing helpers --- actionpack/lib/action_dispatch/routing/mapper.rb | 70 +++++++++++++----------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 57bbb55c1c..8dfac30ac0 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -264,6 +264,42 @@ module ActionDispatch end end + module HttpHelpers + def get(*args, &block) + map_method(:get, *args, &block) + end + + def post(*args, &block) + map_method(:post, *args, &block) + end + + def put(*args, &block) + map_method(:put, *args, &block) + end + + def delete(*args, &block) + map_method(:delete, *args, &block) + end + + def redirect(path, options = {}) + status = options[:status] || 301 + lambda { |env| + req = Rack::Request.new(env) + url = req.scheme + '://' + req.host + path + [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] + } + end + + private + def map_method(method, *args, &block) + options = args.extract_options! + options[:via] = method + args.push(options) + match(*args, &block) + self + end + end + class Constraints def new(app, constraints = []) if constraints.any? @@ -295,26 +331,11 @@ module ActionDispatch def initialize(set) @set = set + extend HttpHelpers extend Scoping extend Resources end - def get(*args, &block) - map_method(:get, *args, &block) - end - - def post(*args, &block) - map_method(:post, *args, &block) - end - - def put(*args, &block) - map_method(:put, *args, &block) - end - - def delete(*args, &block) - map_method(:delete, *args, &block) - end - def root(options = {}) match '/', options.merge(:as => :root) end @@ -375,15 +396,6 @@ module ActionDispatch self end - def redirect(path, options = {}) - status = options[:status] || 301 - lambda { |env| - req = Rack::Request.new(env) - url = req.scheme + '://' + req.host + path - [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] - } - end - private def initialize_app_endpoint(options, defaults) app = nil @@ -412,14 +424,6 @@ module ActionDispatch raise ArgumentError, "missing :action" end end - - def map_method(method, *args, &block) - options = args.extract_options! - options[:via] = method - args.push(options) - match(*args, &block) - self - end end end end -- cgit v1.2.3 From 075f50d62cd02c2cc14b145cdb34bc9ee85cc83c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 18:17:14 -0600 Subject: Fix some nested resource generation tests --- actionpack/lib/action_dispatch/routing/mapper.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8dfac30ac0..34d75e55b6 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -80,15 +80,19 @@ module ActionDispatch resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources - member do - resource(resource.name, options, &block) + parent_resource = @scope[:scope_level_options][:name] + parent_named_prefix = @scope[:scope_level_options][:name_prefix] + with_scope_level(:member) do + scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + resource(resource.name, options, &block) + end end return self end controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resource, :name => resource.singular) do + with_scope_level(:resource, :name => resource.singular, :name_prefix => resource.member_name) do yield if block_given? get "", :to => :show, :as => resource.member_name @@ -118,8 +122,9 @@ module ActionDispatch if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] + parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_resource) do + scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do resources(resource.name, options, &block) end end @@ -128,7 +133,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resources, :name => resource.singular) do + with_scope_level(:resources, :name => resource.singular, :name_prefix => resource.member_name) do yield if block_given? collection do -- cgit v1.2.3 From 61a31f3d3dae55b3ed2a49fafcbfe45b77ea3be2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 1 Dec 2009 14:52:19 -0600 Subject: Fix generating params with optional defaults [#3404 state:resolved] --- actionpack/lib/action_dispatch/routing/deprecated_mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb index dd76391870..87dfaba6c7 100644 --- a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb +++ b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb @@ -175,7 +175,7 @@ module ActionDispatch optional = false elsif segment =~ /^:(\w+)$/ if defaults.has_key?($1.to_sym) - defaults.delete($1.to_sym) + defaults.delete($1.to_sym) if defaults[$1.to_sym].nil? else optional = false end -- cgit v1.2.3 From 7fe19d415ab80727d685c163d7a0413ca6bfe585 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 1 Dec 2009 22:22:48 -0600 Subject: Make recognize try to constantize controller to see if it exists --- actionpack/lib/action_dispatch/routing/route_set.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 79e15edeaa..18e18c5820 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -28,6 +28,7 @@ module ActionDispatch end if env['action_controller.recognize'] + controller(params) [200, {}, params] else controller = controller(params) @@ -41,6 +42,8 @@ module ActionDispatch controller = "#{params[:controller].camelize}Controller" ActiveSupport::Inflector.constantize(controller) end + rescue NameError => e + raise ActionController::RoutingError, e.message end def merge_default_action!(params) -- cgit v1.2.3 From 2fbd6f46fd00a334119f1a25394046963831ce3e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 1 Dec 2009 22:48:42 -0600 Subject: Simply track controller namespaces instead of a complete list of possible controllers to route to --- actionpack/lib/action_dispatch/routing/route_set.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 18e18c5820..c2f6531a74 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -27,11 +27,13 @@ module ActionDispatch end end + unless controller = controller(params) + return [417, {}, []] + end + if env['action_controller.recognize'] - controller(params) [200, {}, params] else - controller = controller(params) controller.action(params[:action]).call(env) end end @@ -42,8 +44,8 @@ module ActionDispatch controller = "#{params[:controller].camelize}Controller" ActiveSupport::Inflector.constantize(controller) end - rescue NameError => e - raise ActionController::RoutingError, e.message + rescue NameError + nil end def merge_default_action!(params) -- cgit v1.2.3 From 84be6cfb6452a23d5617cd8a8b200d8fb0431d5a Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Dec 2009 12:33:33 -0600 Subject: Fork rack build nested query to support to_param --- .../lib/action_dispatch/routing/route_set.rb | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c2f6531a74..f029b634d6 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -379,7 +379,8 @@ module ActionDispatch end recall[:action] = options.delete(:action) if options[:action] == 'index' - parameterize = lambda { |name, value| + opts = {} + opts[:parameterize] = lambda { |name, value| if name == :controller value elsif value.is_a?(Array) @@ -389,7 +390,22 @@ module ActionDispatch end } - path = @set.url(named_route, options, recall, :parameterize => parameterize) + unless result = @set.generate(:path_info, named_route, options, recall, opts) + raise ActionController::RoutingError, "No route matches #{options.inspect}" + end + + uri, params = result + params.each do |k, v| + if v + params[k] = v + else + params.delete(k) + end + end + + uri << "?#{build_nested_query(params)}" if uri && params.any? + path = uri + if path && method == :generate_extras uri = URI(path) extras = uri.query ? @@ -456,6 +472,31 @@ module ActionDispatch def extract_request_environment(request) { :method => request.method } end + + private + def build_nested_query(value, prefix = nil) + case value + when Array + value.map { |v| + build_nested_query(v, "#{prefix}[]") + }.join("&") + when Hash + value.map { |k, v| + build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k) + }.join("&") + when String + raise ArgumentError, "value must be a Hash" if prefix.nil? + "#{Rack::Utils.escape(prefix)}=#{Rack::Utils.escape(value)}" + when NilClass + Rack::Utils.escape(prefix) + else + if value.respond_to?(:to_param) + build_nested_query(value.to_param.to_s, prefix) + else + Rack::Utils.escape(prefix) + end + end + end end end end -- cgit v1.2.3 From 4dee277a9bc05083de6c831cf9aae0846849ecda Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Dec 2009 12:46:14 -0600 Subject: Stop escaping "[]" in query string --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index f029b634d6..5e9c36bbaf 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -486,9 +486,7 @@ module ActionDispatch }.join("&") when String raise ArgumentError, "value must be a Hash" if prefix.nil? - "#{Rack::Utils.escape(prefix)}=#{Rack::Utils.escape(value)}" - when NilClass - Rack::Utils.escape(prefix) + "#{prefix}=#{Rack::Utils.escape(value)}" else if value.respond_to?(:to_param) build_nested_query(value.to_param.to_s, prefix) -- cgit v1.2.3 From 8db038227ca4cbcba01a86ef5fb94cb13c780463 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Dec 2009 14:10:22 -0600 Subject: Move controller namespace tracking into route set so it gets reloaded in dev mode --- .../action_dispatch/routing/deprecated_mapper.rb | 2 +- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- .../lib/action_dispatch/routing/route_set.rb | 36 ++++++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb index 87dfaba6c7..8ce6b2f6d5 100644 --- a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb +++ b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb @@ -113,7 +113,7 @@ module ActionDispatch end end - requirements[:controller] ||= Routing.controller_constraints + requirements[:controller] ||= @set.controller_constraints if defaults[:controller] defaults[:action] ||= 'index' diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 34d75e55b6..400039353c 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -383,7 +383,7 @@ module ActionDispatch constraints.reject! { |k, v| segment_keys.include?(k.to_s) } conditions.merge!(constraints) - requirements[:controller] ||= Routing.controller_constraints + requirements[:controller] ||= @set.controller_constraints if via = options[:via] via = Array(via).map { |m| m.to_s.upcase } diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 5e9c36bbaf..201cf462e4 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -202,10 +202,11 @@ module ActionDispatch end end - attr_accessor :routes, :named_routes, :configuration_files + attr_accessor :routes, :named_routes, :configuration_files, :controller_paths def initialize self.configuration_files = [] + self.controller_paths = [] self.routes = [] self.named_routes = NamedRouteCollection.new @@ -252,7 +253,7 @@ module ActionDispatch def load! # Clear the controller cache so we may discover new ones - Routing.clear_controller_cache! + @controller_constraints = nil load_routes! end @@ -297,6 +298,37 @@ module ActionDispatch routes_changed_at end + CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/ + + def controller_constraints + @controller_constraints ||= begin + source = controller_namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" } + source << CONTROLLER_REGEXP.source + Regexp.compile(source.sort.reverse.join('|')) + end + end + + def controller_namespaces + namespaces = Set.new + + # Find any nested controllers already in memory + ActionController::Base.subclasses.each do |klass| + controller_name = klass.underscore + namespaces << controller_name.split('/')[0...-1].join('/') + end + + # Find namespaces in controllers/ directory + controller_paths.each do |load_path| + load_path = File.expand_path(load_path) + Dir["#{load_path}/**/*_controller.rb"].collect do |path| + namespaces << File.dirname(path).sub(/#{load_path}\/?/, '') + end + end + + namespaces.delete('') + namespaces + end + def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil) route = Route.new(app, conditions, requirements, defaults, name) @set.add_route(*route) -- cgit v1.2.3 From 399909b11c094ab32542d300c72940b1b263b8e6 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Dec 2009 15:23:26 -0600 Subject: Use to_query in route query string generation --- .../lib/action_dispatch/routing/route_set.rb | 25 +--------------------- 1 file changed, 1 insertion(+), 24 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 201cf462e4..a8073c2105 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -435,7 +435,7 @@ module ActionDispatch end end - uri << "?#{build_nested_query(params)}" if uri && params.any? + uri << "?#{params.to_query}" if uri && params.any? path = uri if path && method == :generate_extras @@ -504,29 +504,6 @@ module ActionDispatch def extract_request_environment(request) { :method => request.method } end - - private - def build_nested_query(value, prefix = nil) - case value - when Array - value.map { |v| - build_nested_query(v, "#{prefix}[]") - }.join("&") - when Hash - value.map { |k, v| - build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k) - }.join("&") - when String - raise ArgumentError, "value must be a Hash" if prefix.nil? - "#{prefix}=#{Rack::Utils.escape(value)}" - else - if value.respond_to?(:to_param) - build_nested_query(value.to_param.to_s, prefix) - else - Rack::Utils.escape(prefix) - end - end - end end end end -- cgit v1.2.3 From 66375434b6c7e03a396bbeda3f7029dea2a59f23 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 17:22:09 -0600 Subject: Pass symbol in as route name when match is used with a symbol --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 400039353c..d9724161a9 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -349,7 +349,7 @@ module ActionDispatch options = args.extract_options! if args.length > 1 - args.each { |path| match(path, options) } + args.each { |path| match(path, options.reverse_merge(:as => path.to_sym)) } return self end -- cgit v1.2.3 From 40ad54e3811913c2bc60c7ee292fa48862f12001 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 18:28:02 -0600 Subject: Allow scope to take :path and :controller options --- actionpack/lib/action_dispatch/routing/mapper.rb | 34 +++++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index d9724161a9..fab8a227bf 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -216,6 +216,27 @@ module ActionDispatch def scope(*args) options = args.extract_options! + case args.first + when String + options[:path] = args.first + when Symbol + options[:controller] = args.first + end + + if path = options.delete(:path) + path_set = true + path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}" + else + path_set = false + end + + if controller = options.delete(:controller) + controller_set = true + controller, @scope[:controller] = @scope[:controller], controller + else + controller_set = false + end + constraints = options.delete(:constraints) || {} unless constraints.is_a?(Hash) block, constraints = constraints, {} @@ -225,19 +246,6 @@ module ActionDispatch options, @scope[:options] = @scope[:options], (@scope[:options] || {}).merge(options) - path_set = controller_set = false - - case args.first - when String - path_set = true - path = args.first - path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}" - when Symbol - controller_set = true - controller = args.first - controller, @scope[:controller] = @scope[:controller], controller - end - yield self -- cgit v1.2.3 From e8489b43e2746d9b3605eef86731232fa823ce69 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:24:33 -0600 Subject: Allow name_prefix to be pass into scope --- actionpack/lib/action_dispatch/routing/mapper.rb | 60 ++++++++---------------- 1 file changed, 20 insertions(+), 40 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index fab8a227bf..abcaa529ae 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -4,16 +4,12 @@ module ActionDispatch module Resources class Resource #:nodoc: attr_reader :plural, :singular - attr_reader :path_prefix, :name_prefix def initialize(entities, options = {}) entities = entities.to_s @plural = entities.pluralize @singular = entities.singularize - - @path_prefix = options[:path_prefix] - @name_prefix = options[:name_prefix] end def name @@ -25,41 +21,17 @@ module ActionDispatch end def member_name - if name_prefix - "#{name_prefix}_#{singular}" - else - singular - end + singular end def collection_name - if name_prefix - "#{name_prefix}_#{plural}" - else - plural - end - end - - def new_name - if name_prefix - "new_#{name_prefix}_#{singular}" - else - "new_#{singular}" - end - end - - def edit_name - if name_prefix - "edit_#{name_prefix}_#{singular}" - else - "edit_#{singular}" - end + plural end end class SingletonResource < Resource #:nodoc: def initialize(entity, options = {}) - super(entity) + super end def name @@ -76,8 +48,7 @@ module ActionDispatch return self end - name_prefix = @scope[:options][:name_prefix] if @scope[:options] - resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix) + resource = SingletonResource.new(resources.pop) if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] @@ -99,8 +70,8 @@ module ActionDispatch post "", :to => :create put "", :to => :update delete "", :to => :destroy - get "new", :to => :new, :as => resource.new_name - get "edit", :to => :edit, :as => resource.edit_name + get "new", :to => :new, :as => "new_#{resource.singular}" + get "edit", :to => :edit, :as => "edit_#{resource.singular}" end end end @@ -117,8 +88,7 @@ module ActionDispatch return self end - name_prefix = @scope[:options][:name_prefix] if @scope[:options] - resource = Resource.new(resources.pop, :name_prefix => name_prefix) + resource = Resource.new(resources.pop) if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] @@ -139,14 +109,14 @@ module ActionDispatch collection do get "", :to => :index, :as => resource.collection_name post "", :to => :create - get "new", :to => :new, :as => resource.new_name + get "new", :to => :new, :as => "new_#{resource.singular}" end member do get "", :to => :show, :as => resource.member_name put "", :to => :update delete "", :to => :destroy - get "edit", :to => :edit, :as => resource.edit_name + get "edit", :to => :edit, :as => "edit_#{resource.singular}" end end end @@ -230,6 +200,13 @@ module ActionDispatch path_set = false end + if name_prefix = options.delete(:name_prefix) + name_prefix_set = true + name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix) + else + name_prefix_set = false + end + if controller = options.delete(:controller) controller_set = true controller, @scope[:controller] = @scope[:controller], controller @@ -251,6 +228,7 @@ module ActionDispatch self ensure @scope[:path] = path if path_set + @scope[:name_prefix] = name_prefix if name_prefix_set @scope[:controller] = controller if controller_set @scope[:options] = options @scope[:blocks] = blocks @@ -404,7 +382,9 @@ module ActionDispatch validate_defaults!(app, defaults, segment_keys) app = Constraints.new(app, blocks) - @set.add_route(app, conditions, requirements, defaults, options[:as]) + name = @scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{options[:as]}" : options[:as] + + @set.add_route(app, conditions, requirements, defaults, name) self end -- cgit v1.2.3 From 5835447b6fbc956f22011fc33bcc882db144c7c1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:31:29 -0600 Subject: named_prefix doesn't join with "_" --- actionpack/lib/action_dispatch/routing/mapper.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index abcaa529ae..7eb648cedf 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -63,7 +63,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resource, :name => resource.singular, :name_prefix => resource.member_name) do + with_scope_level(:resource, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do yield if block_given? get "", :to => :show, :as => resource.member_name @@ -103,7 +103,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resources, :name => resource.singular, :name_prefix => resource.member_name) do + with_scope_level(:resources, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do yield if block_given? collection do @@ -202,7 +202,7 @@ module ActionDispatch if name_prefix = options.delete(:name_prefix) name_prefix_set = true - name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix) + name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}#{name_prefix}" : name_prefix) else name_prefix_set = false end @@ -382,7 +382,7 @@ module ActionDispatch validate_defaults!(app, defaults, segment_keys) app = Constraints.new(app, blocks) - name = @scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{options[:as]}" : options[:as] + name = @scope[:name_prefix] ? "#{@scope[:name_prefix]}#{options[:as]}" : options[:as] @set.add_route(app, conditions, requirements, defaults, name) -- cgit v1.2.3 From e600b41c7f2029b1fb4b75b90acc3379acf95d2b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:47:47 -0600 Subject: Cleanup resource scoping by passing down the parent resource object in the scope --- actionpack/lib/action_dispatch/routing/mapper.rb | 31 +++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7eb648cedf..9ca4e16802 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -27,6 +27,14 @@ module ActionDispatch def collection_name plural end + + def id_segment + ":#{singular}_id" + end + + def member_name_prefix + "#{member_name}_" + end end class SingletonResource < Resource #:nodoc: @@ -51,10 +59,8 @@ module ActionDispatch resource = SingletonResource.new(resources.pop) if @scope[:scope_level] == :resources - parent_resource = @scope[:scope_level_options][:name] - parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do resource(resource.name, options, &block) end end @@ -63,7 +69,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resource, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do + with_scope_level(:resource, resource) do yield if block_given? get "", :to => :show, :as => resource.member_name @@ -91,10 +97,8 @@ module ActionDispatch resource = Resource.new(resources.pop) if @scope[:scope_level] == :resources - parent_resource = @scope[:scope_level_options][:name] - parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do resources(resource.name, options, &block) end end @@ -103,7 +107,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resources, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do + with_scope_level(:resources, resource) do yield if block_given? collection do @@ -165,14 +169,19 @@ module ActionDispatch super end + protected + def parent_resource + @scope[:scope_level_resource] + end + private - def with_scope_level(kind, options = {}) + def with_scope_level(kind, resource = parent_resource) old, @scope[:scope_level] = @scope[:scope_level], kind - old_options, @scope[:scope_level_options] = @scope[:scope_level_options], options + old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource yield ensure @scope[:scope_level] = old - @scope[:scope_level_options] = old_options + @scope[:scope_level_resource] = old_resource end end -- cgit v1.2.3 From e86a82c52c2c5edb8c95f9fd882b491dd1f550f4 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:50:13 -0600 Subject: Move name_prefix merging into Scoping concern --- actionpack/lib/action_dispatch/routing/mapper.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9ca4e16802..8dbf33d5f9 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -258,7 +258,13 @@ module ActionDispatch def match(*args) options = args.extract_options! + options = (@scope[:options] || {}).merge(options) + + if @scope[:name_prefix] + options[:as] = "#{@scope[:name_prefix]}#{options[:as]}" + end + args.push(options) super(*args) end @@ -391,9 +397,7 @@ module ActionDispatch validate_defaults!(app, defaults, segment_keys) app = Constraints.new(app, blocks) - name = @scope[:name_prefix] ? "#{@scope[:name_prefix]}#{options[:as]}" : options[:as] - - @set.add_route(app, conditions, requirements, defaults, name) + @set.add_route(app, conditions, requirements, defaults, options[:as]) self end -- cgit v1.2.3 From 81d7227c9b8f4a74a74cf3b2cb183ce130b3f679 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:59:23 -0600 Subject: Move base mapper methods into Base module so plugins can easily extend the mapper --- actionpack/lib/action_dispatch/routing/mapper.rb | 492 ++++++++++++----------- 1 file changed, 247 insertions(+), 245 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8dbf33d5f9..1a742f61e6 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1,6 +1,249 @@ module ActionDispatch module Routing class Mapper + class Constraints + def new(app, constraints = []) + if constraints.any? + super(app, constraints) + else + app + end + end + + def initialize(app, constraints = []) + @app, @constraints = app, constraints + end + + def call(env) + req = Rack::Request.new(env) + + @constraints.each { |constraint| + if constraint.respond_to?(:matches?) && !constraint.matches?(req) + return [417, {}, []] + elsif constraint.respond_to?(:call) && !constraint.call(req) + return [417, {}, []] + end + } + + @app.call(env) + end + end + + module Base + def initialize(set) + @set = set + end + + def root(options = {}) + match '/', options.merge(:as => :root) + end + + def match(*args) + options = args.extract_options! + + if args.length > 1 + args.each { |path| match(path, options.reverse_merge(:as => path.to_sym)) } + return self + end + + if args.first.is_a?(Symbol) + return match(args.first.to_s, options.merge(:to => args.first.to_sym)) + end + + path = args.first + + conditions, defaults = {}, {} + + path = nil if path == "" + path = Rack::Mount::Utils.normalize_path(path) if path + path = "#{@scope[:path]}#{path}" if @scope[:path] + + raise ArgumentError, "path is required" unless path + + constraints = options[:constraints] || {} + unless constraints.is_a?(Hash) + block, constraints = constraints, {} + end + blocks = ((@scope[:blocks] || []) + [block]).compact + constraints = (@scope[:constraints] || {}).merge(constraints) + options.each { |k, v| constraints[k] = v if v.is_a?(Regexp) } + + conditions[:path_info] = path + requirements = constraints.dup + + path_regexp = Rack::Mount::Strexp.compile(path, constraints, SEPARATORS) + segment_keys = Rack::Mount::RegexpWithNamedGroups.new(path_regexp).names + constraints.reject! { |k, v| segment_keys.include?(k.to_s) } + conditions.merge!(constraints) + + requirements[:controller] ||= @set.controller_constraints + + if via = options[:via] + via = Array(via).map { |m| m.to_s.upcase } + conditions[:request_method] = Regexp.union(*via) + end + + defaults[:controller] ||= @scope[:controller].to_s if @scope[:controller] + + app = initialize_app_endpoint(options, defaults) + validate_defaults!(app, defaults, segment_keys) + app = Constraints.new(app, blocks) + + @set.add_route(app, conditions, requirements, defaults, options[:as]) + + self + end + + private + def initialize_app_endpoint(options, defaults) + app = nil + + if options[:to].respond_to?(:call) + app = options[:to] + defaults.delete(:controller) + defaults.delete(:action) + elsif options[:to].is_a?(String) + defaults[:controller], defaults[:action] = options[:to].split('#') + elsif options[:to].is_a?(Symbol) + defaults[:action] = options[:to].to_s + end + + app || Routing::RouteSet::Dispatcher.new(:defaults => defaults) + end + + def validate_defaults!(app, defaults, segment_keys) + return unless app.is_a?(Routing::RouteSet::Dispatcher) + + unless defaults.include?(:controller) || segment_keys.include?("controller") + raise ArgumentError, "missing :controller" + end + + unless defaults.include?(:action) || segment_keys.include?("action") + raise ArgumentError, "missing :action" + end + end + end + + module HttpHelpers + def get(*args, &block) + map_method(:get, *args, &block) + end + + def post(*args, &block) + map_method(:post, *args, &block) + end + + def put(*args, &block) + map_method(:put, *args, &block) + end + + def delete(*args, &block) + map_method(:delete, *args, &block) + end + + def redirect(path, options = {}) + status = options[:status] || 301 + lambda { |env| + req = Rack::Request.new(env) + url = req.scheme + '://' + req.host + path + [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] + } + end + + private + def map_method(method, *args, &block) + options = args.extract_options! + options[:via] = method + args.push(options) + match(*args, &block) + self + end + end + + module Scoping + def initialize(*args) + @scope = {} + super + end + + def scope(*args) + options = args.extract_options! + + case args.first + when String + options[:path] = args.first + when Symbol + options[:controller] = args.first + end + + if path = options.delete(:path) + path_set = true + path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}" + else + path_set = false + end + + if name_prefix = options.delete(:name_prefix) + name_prefix_set = true + name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}#{name_prefix}" : name_prefix) + else + name_prefix_set = false + end + + if controller = options.delete(:controller) + controller_set = true + controller, @scope[:controller] = @scope[:controller], controller + else + controller_set = false + end + + constraints = options.delete(:constraints) || {} + unless constraints.is_a?(Hash) + block, constraints = constraints, {} + end + constraints, @scope[:constraints] = @scope[:constraints], (@scope[:constraints] || {}).merge(constraints) + blocks, @scope[:blocks] = @scope[:blocks], (@scope[:blocks] || []) + [block] + + options, @scope[:options] = @scope[:options], (@scope[:options] || {}).merge(options) + + yield + + self + ensure + @scope[:path] = path if path_set + @scope[:name_prefix] = name_prefix if name_prefix_set + @scope[:controller] = controller if controller_set + @scope[:options] = options + @scope[:blocks] = blocks + @scope[:constraints] = constraints + end + + def controller(controller) + scope(controller.to_sym) { yield } + end + + def namespace(path) + scope(path.to_s) { yield } + end + + def constraints(constraints = {}) + scope(:constraints => constraints) { yield } + end + + def match(*args) + options = args.extract_options! + + options = (@scope[:options] || {}).merge(options) + + if @scope[:name_prefix] + options[:as] = "#{@scope[:name_prefix]}#{options[:as]}" + end + + args.push(options) + super(*args) + end + end + module Resources class Resource #:nodoc: attr_reader :plural, :singular @@ -185,251 +428,10 @@ module ActionDispatch end end - module Scoping - def self.extended(object) - object.instance_eval do - @scope = {} - end - end - - def scope(*args) - options = args.extract_options! - - case args.first - when String - options[:path] = args.first - when Symbol - options[:controller] = args.first - end - - if path = options.delete(:path) - path_set = true - path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}" - else - path_set = false - end - - if name_prefix = options.delete(:name_prefix) - name_prefix_set = true - name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}#{name_prefix}" : name_prefix) - else - name_prefix_set = false - end - - if controller = options.delete(:controller) - controller_set = true - controller, @scope[:controller] = @scope[:controller], controller - else - controller_set = false - end - - constraints = options.delete(:constraints) || {} - unless constraints.is_a?(Hash) - block, constraints = constraints, {} - end - constraints, @scope[:constraints] = @scope[:constraints], (@scope[:constraints] || {}).merge(constraints) - blocks, @scope[:blocks] = @scope[:blocks], (@scope[:blocks] || []) + [block] - - options, @scope[:options] = @scope[:options], (@scope[:options] || {}).merge(options) - - yield - - self - ensure - @scope[:path] = path if path_set - @scope[:name_prefix] = name_prefix if name_prefix_set - @scope[:controller] = controller if controller_set - @scope[:options] = options - @scope[:blocks] = blocks - @scope[:constraints] = constraints - end - - def controller(controller) - scope(controller.to_sym) { yield } - end - - def namespace(path) - scope(path.to_s) { yield } - end - - def constraints(constraints = {}) - scope(:constraints => constraints) { yield } - end - - def match(*args) - options = args.extract_options! - - options = (@scope[:options] || {}).merge(options) - - if @scope[:name_prefix] - options[:as] = "#{@scope[:name_prefix]}#{options[:as]}" - end - - args.push(options) - super(*args) - end - end - - module HttpHelpers - def get(*args, &block) - map_method(:get, *args, &block) - end - - def post(*args, &block) - map_method(:post, *args, &block) - end - - def put(*args, &block) - map_method(:put, *args, &block) - end - - def delete(*args, &block) - map_method(:delete, *args, &block) - end - - def redirect(path, options = {}) - status = options[:status] || 301 - lambda { |env| - req = Rack::Request.new(env) - url = req.scheme + '://' + req.host + path - [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] - } - end - - private - def map_method(method, *args, &block) - options = args.extract_options! - options[:via] = method - args.push(options) - match(*args, &block) - self - end - end - - class Constraints - def new(app, constraints = []) - if constraints.any? - super(app, constraints) - else - app - end - end - - def initialize(app, constraints = []) - @app, @constraints = app, constraints - end - - def call(env) - req = Rack::Request.new(env) - - @constraints.each { |constraint| - if constraint.respond_to?(:matches?) && !constraint.matches?(req) - return [417, {}, []] - elsif constraint.respond_to?(:call) && !constraint.call(req) - return [417, {}, []] - end - } - - @app.call(env) - end - end - - def initialize(set) - @set = set - - extend HttpHelpers - extend Scoping - extend Resources - end - - def root(options = {}) - match '/', options.merge(:as => :root) - end - - def match(*args) - options = args.extract_options! - - if args.length > 1 - args.each { |path| match(path, options.reverse_merge(:as => path.to_sym)) } - return self - end - - if args.first.is_a?(Symbol) - return match(args.first.to_s, options.merge(:to => args.first.to_sym)) - end - - path = args.first - - conditions, defaults = {}, {} - - path = nil if path == "" - path = Rack::Mount::Utils.normalize_path(path) if path - path = "#{@scope[:path]}#{path}" if @scope[:path] - - raise ArgumentError, "path is required" unless path - - constraints = options[:constraints] || {} - unless constraints.is_a?(Hash) - block, constraints = constraints, {} - end - blocks = ((@scope[:blocks] || []) + [block]).compact - constraints = (@scope[:constraints] || {}).merge(constraints) - options.each { |k, v| constraints[k] = v if v.is_a?(Regexp) } - - conditions[:path_info] = path - requirements = constraints.dup - - path_regexp = Rack::Mount::Strexp.compile(path, constraints, SEPARATORS) - segment_keys = Rack::Mount::RegexpWithNamedGroups.new(path_regexp).names - constraints.reject! { |k, v| segment_keys.include?(k.to_s) } - conditions.merge!(constraints) - - requirements[:controller] ||= @set.controller_constraints - - if via = options[:via] - via = Array(via).map { |m| m.to_s.upcase } - conditions[:request_method] = Regexp.union(*via) - end - - defaults[:controller] ||= @scope[:controller].to_s if @scope[:controller] - - app = initialize_app_endpoint(options, defaults) - validate_defaults!(app, defaults, segment_keys) - app = Constraints.new(app, blocks) - - @set.add_route(app, conditions, requirements, defaults, options[:as]) - - self - end - - private - def initialize_app_endpoint(options, defaults) - app = nil - - if options[:to].respond_to?(:call) - app = options[:to] - defaults.delete(:controller) - defaults.delete(:action) - elsif options[:to].is_a?(String) - defaults[:controller], defaults[:action] = options[:to].split('#') - elsif options[:to].is_a?(Symbol) - defaults[:action] = options[:to].to_s - end - - app || Routing::RouteSet::Dispatcher.new(:defaults => defaults) - end - - def validate_defaults!(app, defaults, segment_keys) - return unless app.is_a?(Routing::RouteSet::Dispatcher) - - unless defaults.include?(:controller) || segment_keys.include?("controller") - raise ArgumentError, "missing :controller" - end - - unless defaults.include?(:action) || segment_keys.include?("action") - raise ArgumentError, "missing :action" - end - end + include Base + include HttpHelpers + include Scoping + include Resources end end end -- cgit v1.2.3 From 0c34e3f41ae79bb675c74b697ef92bad59b25f7f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 20:11:57 -0600 Subject: Ignore name_prefix unless there is an explicit name --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 1a742f61e6..86470c2017 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -235,7 +235,7 @@ module ActionDispatch options = (@scope[:options] || {}).merge(options) - if @scope[:name_prefix] + if @scope[:name_prefix] && options[:as] options[:as] = "#{@scope[:name_prefix]}#{options[:as]}" end -- cgit v1.2.3 From 1fc58a889d72e9a36167b41fc3cd055c1f58774e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 20:57:01 -0600 Subject: Fixed named prefix scope in resource member and collection actions --- actionpack/lib/action_dispatch/routing/mapper.rb | 60 ++++++++++++++---------- 1 file changed, 34 insertions(+), 26 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 86470c2017..7647bdeb5d 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -41,15 +41,6 @@ module ActionDispatch def match(*args) options = args.extract_options! - if args.length > 1 - args.each { |path| match(path, options.reverse_merge(:as => path.to_sym)) } - return self - end - - if args.first.is_a?(Symbol) - return match(args.first.to_s, options.merge(:to => args.first.to_sym)) - end - path = args.first conditions, defaults = {}, {} @@ -185,7 +176,7 @@ module ActionDispatch if name_prefix = options.delete(:name_prefix) name_prefix_set = true - name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}#{name_prefix}" : name_prefix) + name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix) else name_prefix_set = false end @@ -235,8 +226,10 @@ module ActionDispatch options = (@scope[:options] || {}).merge(options) - if @scope[:name_prefix] && options[:as] - options[:as] = "#{@scope[:name_prefix]}#{options[:as]}" + if @scope[:name_prefix] && !options[:as].blank? + options[:as] = "#{@scope[:name_prefix]}_#{options[:as]}" + elsif @scope[:name_prefix] && options[:as].blank? + options[:as] = @scope[:name_prefix].to_s end args.push(options) @@ -274,10 +267,6 @@ module ActionDispatch def id_segment ":#{singular}_id" end - - def member_name_prefix - "#{member_name}_" - end end class SingletonResource < Resource #:nodoc: @@ -303,7 +292,7 @@ module ActionDispatch if @scope[:scope_level] == :resources with_scope_level(:member) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do resource(resource.name, options, &block) end end @@ -341,7 +330,7 @@ module ActionDispatch if @scope[:scope_level] == :resources with_scope_level(:member) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do resources(resource.name, options, &block) end end @@ -353,17 +342,19 @@ module ActionDispatch with_scope_level(:resources, resource) do yield if block_given? - collection do + with_scope_level(:collection) do get "", :to => :index, :as => resource.collection_name post "", :to => :create get "new", :to => :new, :as => "new_#{resource.singular}" end - member do - get "", :to => :show, :as => resource.member_name - put "", :to => :update - delete "", :to => :destroy - get "edit", :to => :edit, :as => "edit_#{resource.singular}" + with_scope_level(:member) do + scope(":id") do + get "", :to => :show, :as => resource.member_name + put "", :to => :update + delete "", :to => :destroy + get "edit", :to => :edit, :as => "edit_#{resource.singular}" + end end end end @@ -378,7 +369,9 @@ module ActionDispatch end with_scope_level(:collection) do - yield + scope(:name_prefix => parent_resource.member_name, :as => "") do + yield + end end end @@ -388,7 +381,7 @@ module ActionDispatch end with_scope_level(:member) do - scope(":id") do + scope(":id", :name_prefix => parent_resource.member_name, :as => "") do yield end end @@ -396,6 +389,21 @@ module ActionDispatch def match(*args) options = args.extract_options! + + if args.length > 1 + args.each { |path| match(path, options) } + return self + end + + if args.first.is_a?(Symbol) + begin + old_name_prefix, @scope[:name_prefix] = @scope[:name_prefix], "#{args.first}_#{@scope[:name_prefix]}" + return match(args.first.to_s, options.merge(:to => args.first.to_sym)) + ensure + @scope[:name_prefix] = old_name_prefix + end + end + args.push(options) case options.delete(:on) -- cgit v1.2.3 From 3d91d7f0a2bd4b1e104dd8847a2fe9f206c916ca Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 8 Dec 2009 15:31:56 -0600 Subject: Routes added under resource collection should be prefixed with resource collection name --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7647bdeb5d..513c6a5c5f 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -369,7 +369,7 @@ module ActionDispatch end with_scope_level(:collection) do - scope(:name_prefix => parent_resource.member_name, :as => "") do + scope(:name_prefix => parent_resource.collection_name, :as => "") do yield end end -- cgit v1.2.3 From 33658ea1ae4170f4b0b5123e240d79bb292719e7 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 8 Dec 2009 15:50:44 -0600 Subject: Don't use name prefix by itself unless as is an empty string --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 513c6a5c5f..e05845a04f 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -228,7 +228,7 @@ module ActionDispatch if @scope[:name_prefix] && !options[:as].blank? options[:as] = "#{@scope[:name_prefix]}_#{options[:as]}" - elsif @scope[:name_prefix] && options[:as].blank? + elsif @scope[:name_prefix] && options[:as] == "" options[:as] = @scope[:name_prefix].to_s end -- cgit v1.2.3 From c4df6332a4d8292dd7d6bd6a1badc896a2323d11 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 8 Dec 2009 16:06:46 -0600 Subject: Seperate scope level for nesting resources --- actionpack/lib/action_dispatch/routing/mapper.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index e05845a04f..24b04088e6 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -291,10 +291,8 @@ module ActionDispatch resource = SingletonResource.new(resources.pop) if @scope[:scope_level] == :resources - with_scope_level(:member) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do - resource(resource.name, options, &block) - end + nested do + resource(resource.name, options, &block) end return self end @@ -329,10 +327,8 @@ module ActionDispatch resource = Resource.new(resources.pop) if @scope[:scope_level] == :resources - with_scope_level(:member) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do - resources(resource.name, options, &block) - end + nested do + resources(resource.name, options, &block) end return self end @@ -387,6 +383,18 @@ module ActionDispatch end end + def nested + unless @scope[:scope_level] == :resources + raise ArgumentError, "can't use nested outside resources scope" + end + + with_scope_level(:nested) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do + yield + end + end + end + def match(*args) options = args.extract_options! -- cgit v1.2.3 From ce5f27b04b1ff25eed520a3d06b3b9c150536e21 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 8 Dec 2009 16:13:00 -0600 Subject: Remove double scoping blocks and just use one --- actionpack/lib/action_dispatch/routing/mapper.rb | 52 +++++++++++------------- 1 file changed, 24 insertions(+), 28 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 24b04088e6..8cb7745aff 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -297,18 +297,16 @@ module ActionDispatch return self end - controller(resource.controller) do - namespace(resource.name) do - with_scope_level(:resource, resource) do - yield if block_given? - - get "", :to => :show, :as => resource.member_name - post "", :to => :create - put "", :to => :update - delete "", :to => :destroy - get "new", :to => :new, :as => "new_#{resource.singular}" - get "edit", :to => :edit, :as => "edit_#{resource.singular}" - end + scope(:path => resource.name, :controller => resource.controller) do + with_scope_level(:resource, resource) do + yield if block_given? + + get "", :to => :show, :as => resource.member_name + post "", :to => :create + put "", :to => :update + delete "", :to => :destroy + get "new", :to => :new, :as => "new_#{resource.singular}" + get "edit", :to => :edit, :as => "edit_#{resource.singular}" end end @@ -333,24 +331,22 @@ module ActionDispatch return self end - controller(resource.controller) do - namespace(resource.name) do - with_scope_level(:resources, resource) do - yield if block_given? + scope(:path => resource.name, :controller => resource.controller) do + with_scope_level(:resources, resource) do + yield if block_given? - with_scope_level(:collection) do - get "", :to => :index, :as => resource.collection_name - post "", :to => :create - get "new", :to => :new, :as => "new_#{resource.singular}" - end + with_scope_level(:collection) do + get "", :to => :index, :as => resource.collection_name + post "", :to => :create + get "new", :to => :new, :as => "new_#{resource.singular}" + end - with_scope_level(:member) do - scope(":id") do - get "", :to => :show, :as => resource.member_name - put "", :to => :update - delete "", :to => :destroy - get "edit", :to => :edit, :as => "edit_#{resource.singular}" - end + with_scope_level(:member) do + scope(":id") do + get "", :to => :show, :as => resource.member_name + put "", :to => :update + delete "", :to => :destroy + get "edit", :to => :edit, :as => "edit_#{resource.singular}" end end end -- cgit v1.2.3 From 511cef296bd07fa43794e029e12e4cd1053aa8d1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 8 Dec 2009 17:19:49 -0600 Subject: Tack format onto resource routes --- actionpack/lib/action_dispatch/routing/mapper.rb | 44 ++++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8cb7745aff..3b185c2576 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -46,8 +46,8 @@ module ActionDispatch conditions, defaults = {}, {} path = nil if path == "" - path = Rack::Mount::Utils.normalize_path(path) if path path = "#{@scope[:path]}#{path}" if @scope[:path] + path = Rack::Mount::Utils.normalize_path(path) if path raise ArgumentError, "path is required" unless path @@ -169,7 +169,7 @@ module ActionDispatch if path = options.delete(:path) path_set = true - path, @scope[:path] = @scope[:path], "#{@scope[:path]}#{Rack::Mount::Utils.normalize_path(path)}" + path, @scope[:path] = @scope[:path], Rack::Mount::Utils.normalize_path(@scope[:path].to_s + path.to_s) else path_set = false end @@ -214,7 +214,7 @@ module ActionDispatch end def namespace(path) - scope(path.to_s) { yield } + scope("/#{path}") { yield } end def constraints(constraints = {}) @@ -297,16 +297,16 @@ module ActionDispatch return self end - scope(:path => resource.name, :controller => resource.controller) do + scope(:path => "/#{resource.name}", :controller => resource.controller) do with_scope_level(:resource, resource) do yield if block_given? - get "", :to => :show, :as => resource.member_name - post "", :to => :create - put "", :to => :update - delete "", :to => :destroy - get "new", :to => :new, :as => "new_#{resource.singular}" - get "edit", :to => :edit, :as => "edit_#{resource.singular}" + get "(.:format)", :to => :show, :as => resource.member_name + post "(.:format)", :to => :create + put "(.:format)", :to => :update + delete "(.:format)", :to => :destroy + get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" + get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" end end @@ -331,22 +331,22 @@ module ActionDispatch return self end - scope(:path => resource.name, :controller => resource.controller) do + scope(:path => "/#{resource.name}", :controller => resource.controller) do with_scope_level(:resources, resource) do yield if block_given? with_scope_level(:collection) do - get "", :to => :index, :as => resource.collection_name - post "", :to => :create - get "new", :to => :new, :as => "new_#{resource.singular}" + get "(.:format)", :to => :index, :as => resource.collection_name + post "(.:format)", :to => :create + get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" end with_scope_level(:member) do - scope(":id") do - get "", :to => :show, :as => resource.member_name - put "", :to => :update - delete "", :to => :destroy - get "edit", :to => :edit, :as => "edit_#{resource.singular}" + scope("/:id") do + get "(.:format)", :to => :show, :as => resource.member_name + put "(.:format)", :to => :update + delete "(.:format)", :to => :destroy + get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" end end end @@ -373,7 +373,7 @@ module ActionDispatch end with_scope_level(:member) do - scope(":id", :name_prefix => parent_resource.member_name, :as => "") do + scope("/:id", :name_prefix => parent_resource.member_name, :as => "") do yield end end @@ -385,7 +385,7 @@ module ActionDispatch end with_scope_level(:nested) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do + scope("/#{parent_resource.id_segment}", :name_prefix => parent_resource.member_name) do yield end end @@ -402,7 +402,7 @@ module ActionDispatch if args.first.is_a?(Symbol) begin old_name_prefix, @scope[:name_prefix] = @scope[:name_prefix], "#{args.first}_#{@scope[:name_prefix]}" - return match(args.first.to_s, options.merge(:to => args.first.to_sym)) + return match("/#{args.first}(.:format)", options.merge(:to => args.first.to_sym)) ensure @scope[:name_prefix] = old_name_prefix end -- cgit v1.2.3 From ec5434c3c2631bb47b568eede397c3bd596eeb88 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 9 Dec 2009 20:46:32 -0600 Subject: Check block arity passed to routes draw so you don't need to use |map| --- actionpack/lib/action_dispatch/routing/route_set.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index a8073c2105..0e83ea3b7e 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -216,7 +216,14 @@ module ActionDispatch def draw(&block) clear! - Mapper.new(self).instance_exec(DeprecatedMapper.new(self), &block) + + mapper = Mapper.new(self) + if block.arity == 1 + mapper.instance_exec(DeprecatedMapper.new(self), &block) + else + mapper.instance_exec(&block) + end + @set.add_route(NotFound) install_helpers @set.freeze -- cgit v1.2.3 From d1191507bc8ffa4f7fe59b1cfb03dfbf6b73a798 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 10 Dec 2009 22:57:07 -0600 Subject: Cleanup generate_extras build/parse mess --- actionpack/lib/action_dispatch/routing/route_set.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 0e83ea3b7e..664babd12e 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -433,7 +433,7 @@ module ActionDispatch raise ActionController::RoutingError, "No route matches #{options.inspect}" end - uri, params = result + path, params = result params.each do |k, v| if v params[k] = v @@ -442,16 +442,10 @@ module ActionDispatch end end - uri << "?#{params.to_query}" if uri && params.any? - path = uri - if path && method == :generate_extras - uri = URI(path) - extras = uri.query ? - Rack::Utils.parse_nested_query(uri.query).keys.map { |k| k.to_sym } : - [] - [uri.path, extras] + [path, params.keys] elsif path + path << "?#{params.to_query}" if params.any? path else raise ActionController::RoutingError, "No route matches #{options.inspect}" -- cgit v1.2.3 From 2f90d700498294a9a4da0baa5317c0e6feaaf176 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 10 Dec 2009 23:45:04 -0600 Subject: Kill RouteSet#recognize --- actionpack/lib/action_dispatch/routing/route_set.rb | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 664babd12e..8d56c4d087 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -479,12 +479,6 @@ module ActionDispatch end end - def recognize(request) - params = recognize_path(request.path, extract_request_environment(request)) - request.path_parameters = params.with_indifferent_access - "#{params[:controller].to_s.camelize}Controller".constantize - end - def recognize_path(path, environment = {}, rescue_error = true) method = (environment[:method] || "GET").to_s.upcase @@ -499,12 +493,6 @@ module ActionDispatch status, headers, body = call(env) body end - - # Subclasses and plugins may override this method to extract further attributes - # from the request, for use by route conditions and such. - def extract_request_environment(request) - { :method => request.method } - end end end end -- cgit v1.2.3 From 588225f8852c4b60bfba38f16d8797a41e175400 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 11 Dec 2009 00:01:22 -0600 Subject: Remove fancy method not allowed resource exceptions since they are too much of a hack --- .../lib/action_dispatch/routing/route_set.rb | 24 +--------------------- 1 file changed, 1 insertion(+), 23 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 8d56c4d087..d71ed1d1db 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -456,30 +456,9 @@ module ActionDispatch def call(env) @set.call(env) - rescue ActionController::RoutingError => e - raise e if env['action_controller.rescue_error'] == false - - method, path = env['REQUEST_METHOD'].downcase.to_sym, env['PATH_INFO'] - - # Route was not recognized. Try to find out why (maybe wrong verb). - allows = HTTP_METHODS.select { |verb| - begin - recognize_path(path, {:method => verb}, false) - rescue ActionController::RoutingError - nil - end - } - - if !HTTP_METHODS.include?(method) - raise ActionController::NotImplemented.new(*allows) - elsif !allows.empty? - raise ActionController::MethodNotAllowed.new(*allows) - else - raise e - end end - def recognize_path(path, environment = {}, rescue_error = true) + def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase begin @@ -489,7 +468,6 @@ module ActionDispatch end env['action_controller.recognize'] = true - env['action_controller.rescue_error'] = rescue_error status, headers, body = call(env) body end -- cgit v1.2.3 From 61e9f2023baead046c7f8ba7c89a7496bf49d1be Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 11 Dec 2009 00:11:16 -0600 Subject: Use rackmounts recognize api and don't piggyback recognize_path on top of rack call --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- .../lib/action_dispatch/routing/route_set.rb | 49 +++++++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 3b185c2576..2fa0aab5df 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -2,7 +2,7 @@ module ActionDispatch module Routing class Mapper class Constraints - def new(app, constraints = []) + def self.new(app, constraints = []) if constraints.any? super(app, constraints) else diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index d71ed1d1db..8afd42a293 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -18,36 +18,37 @@ module ActionDispatch def call(env) params = env[PARAMETERS_KEY] + prepare_params!(params) + + unless controller = controller(params) + return [417, {}, []] + end + + controller.action(params[:action]).call(env) + end + + def prepare_params!(params) merge_default_action!(params) split_glob_param!(params) if @glob_param + params.each do |key, value| if value.is_a?(String) value = value.dup.force_encoding(Encoding::BINARY) if value.respond_to?(:force_encoding) params[key] = URI.unescape(value) end end + end - unless controller = controller(params) - return [417, {}, []] - end - - if env['action_controller.recognize'] - [200, {}, params] - else - controller.action(params[:action]).call(env) + def controller(params) + if params && params.has_key?(:controller) + controller = "#{params[:controller].camelize}Controller" + ActiveSupport::Inflector.constantize(controller) end + rescue NameError + nil end private - def controller(params) - if params && params.has_key?(:controller) - controller = "#{params[:controller].camelize}Controller" - ActiveSupport::Inflector.constantize(controller) - end - rescue NameError - nil - end - def merge_default_action!(params) params[:action] ||= 'index' end @@ -460,6 +461,7 @@ module ActionDispatch def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase + path = Rack::Mount::Utils.normalize_path(path) begin env = Rack::MockRequest.env_for(path, {:method => method}) @@ -467,9 +469,16 @@ module ActionDispatch raise ActionController::RoutingError, e.message end - env['action_controller.recognize'] = true - status, headers, body = call(env) - body + req = Rack::Request.new(env) + @set.recognize(req) do |route, params| + dispatcher = route.app + if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params) + dispatcher.prepare_params!(params) + return params + end + end + + raise ActionController::RoutingError, "No route matches #{path.inspect} with #{environment.inspect}" end end end -- cgit v1.2.3 From 2297eaed5b195ea42b99d062ad45f87dde9d3c60 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 11 Dec 2009 12:46:50 -0600 Subject: "new" and "edit" name routes always need to be prepend to the named_route [#3561 state:resolved] --- actionpack/lib/action_dispatch/routing/mapper.rb | 29 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 2fa0aab5df..d480af876d 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -338,7 +338,9 @@ module ActionDispatch with_scope_level(:collection) do get "(.:format)", :to => :index, :as => resource.collection_name post "(.:format)", :to => :create - get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" + with_exclusive_name_prefix :new do + get "/new(.:format)", :to => :new, :as => resource.singular + end end with_scope_level(:member) do @@ -346,7 +348,9 @@ module ActionDispatch get "(.:format)", :to => :show, :as => resource.member_name put "(.:format)", :to => :update delete "(.:format)", :to => :destroy - get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" + with_exclusive_name_prefix :edit do + get "/edit(.:format)", :to => :edit, :as => resource.singular + end end end end @@ -400,11 +404,8 @@ module ActionDispatch end if args.first.is_a?(Symbol) - begin - old_name_prefix, @scope[:name_prefix] = @scope[:name_prefix], "#{args.first}_#{@scope[:name_prefix]}" + with_exclusive_name_prefix(args.first) do return match("/#{args.first}(.:format)", options.merge(:to => args.first.to_sym)) - ensure - @scope[:name_prefix] = old_name_prefix end end @@ -430,6 +431,22 @@ module ActionDispatch end private + def with_exclusive_name_prefix(prefix) + begin + old_name_prefix = @scope[:name_prefix] + + if !old_name_prefix.blank? + @scope[:name_prefix] = "#{prefix}_#{@scope[:name_prefix]}" + else + @scope[:name_prefix] = prefix.to_s + end + + yield + ensure + @scope[:name_prefix] = old_name_prefix + end + end + def with_scope_level(kind, resource = parent_resource) old, @scope[:scope_level] = @scope[:scope_level], kind old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource -- cgit v1.2.3 From ec99eca013ce96fa1fa628510038a9eafa46d3c5 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 14 Dec 2009 16:51:13 -0600 Subject: Fix loading plugin and engine route sets --- .../lib/action_dispatch/routing/route_set.rb | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 8afd42a293..6f35e9b4e3 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -212,11 +212,14 @@ module ActionDispatch self.routes = [] self.named_routes = NamedRouteCollection.new + @clear_before_draw = true + @finalize_set_on_draw = true + clear! end def draw(&block) - clear! + clear! if @clear_before_draw mapper = Mapper.new(self) if block.arity == 1 @@ -225,9 +228,13 @@ module ActionDispatch mapper.instance_exec(&block) end - @set.add_route(NotFound) - install_helpers - @set.freeze + if @finalize_set_on_draw + @set.add_route(NotFound) + install_helpers + @set.freeze + end + + nil end def clear! @@ -283,7 +290,15 @@ module ActionDispatch def load_routes! if configuration_files.any? - configuration_files.each { |config| load(config) } + @finalize_set_on_draw = false + configuration_files.each_with_index do |config, index| + @finalize_set_on_draw = true if index == (configuration_files.length - 1) + load(config) + @clear_before_draw = false if index == 0 + end + @clear_before_draw = true + @finalize_set_on_draw = true + @routes_last_modified = routes_changed_at else draw do |map| -- cgit v1.2.3 From 5f8e48cbd297aca4add4b48efa2136ba6ac851b1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 14 Dec 2009 17:54:41 -0600 Subject: Move route reloading into railties --- .../lib/action_dispatch/routing/route_set.rb | 109 ++++----------------- 1 file changed, 21 insertions(+), 88 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 6f35e9b4e3..bf2443c1be 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -203,23 +203,18 @@ module ActionDispatch end end - attr_accessor :routes, :named_routes, :configuration_files, :controller_paths + attr_accessor :routes, :named_routes + attr_accessor :disable_clear_and_finalize def initialize - self.configuration_files = [] - self.controller_paths = [] - self.routes = [] self.named_routes = NamedRouteCollection.new - @clear_before_draw = true - @finalize_set_on_draw = true - - clear! + @disable_clear_and_finalize = false end def draw(&block) - clear! if @clear_before_draw + clear! unless @disable_clear_and_finalize mapper = Mapper.new(self) if block.arity == 1 @@ -228,16 +223,20 @@ module ActionDispatch mapper.instance_exec(&block) end - if @finalize_set_on_draw - @set.add_route(NotFound) - install_helpers - @set.freeze - end + finalize! unless @disable_clear_and_finalize nil end + def finalize! + @set.add_route(NotFound) + install_helpers + @set.freeze + end + def clear! + # Clear the controller cache so we may discover new ones + @controller_constraints = nil routes.clear named_routes.clear @set = ::Rack::Mount::RouteSet.new(:parameters_key => PARAMETERS_KEY) @@ -252,75 +251,6 @@ module ActionDispatch routes.empty? end - def add_configuration_file(path) - self.configuration_files << path - end - - # Deprecated accessor - def configuration_file=(path) - add_configuration_file(path) - end - - # Deprecated accessor - def configuration_file - configuration_files - end - - def load! - # Clear the controller cache so we may discover new ones - @controller_constraints = nil - - load_routes! - end - - # reload! will always force a reload whereas load checks the timestamp first - alias reload! load! - - def reload - if configuration_files.any? && @routes_last_modified - if routes_changed_at == @routes_last_modified - return # routes didn't change, don't reload - else - @routes_last_modified = routes_changed_at - end - end - - load! - end - - def load_routes! - if configuration_files.any? - @finalize_set_on_draw = false - configuration_files.each_with_index do |config, index| - @finalize_set_on_draw = true if index == (configuration_files.length - 1) - load(config) - @clear_before_draw = false if index == 0 - end - @clear_before_draw = true - @finalize_set_on_draw = true - - @routes_last_modified = routes_changed_at - else - draw do |map| - map.connect ":controller/:action/:id" - end - end - end - - def routes_changed_at - routes_changed_at = nil - - configuration_files.each do |config| - config_changed_at = File.stat(config).mtime - - if routes_changed_at.nil? || config_changed_at > routes_changed_at - routes_changed_at = config_changed_at - end - end - - routes_changed_at - end - CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/ def controller_constraints @@ -340,11 +270,14 @@ module ActionDispatch namespaces << controller_name.split('/')[0...-1].join('/') end - # Find namespaces in controllers/ directory - controller_paths.each do |load_path| - load_path = File.expand_path(load_path) - Dir["#{load_path}/**/*_controller.rb"].collect do |path| - namespaces << File.dirname(path).sub(/#{load_path}\/?/, '') + # TODO: Move this into Railties + if defined?(Rails.application) + # Find namespaces in controllers/ directory + Rails.application.configuration.controller_paths.each do |load_path| + load_path = File.expand_path(load_path) + Dir["#{load_path}/**/*_controller.rb"].collect do |path| + namespaces << File.dirname(path).sub(/#{load_path}\/?/, '') + end end end -- cgit v1.2.3 From e48b4c2dd01877ace901e1c186d04605b53b40d0 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 20 Dec 2009 14:07:19 -0800 Subject: :to => redirect() can take a String using 1.9-style interpolation or proc that takes the path parameters as a Hash --- actionpack/lib/action_dispatch/routing/mapper.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index d480af876d..57e992d7dc 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -132,13 +132,19 @@ module ActionDispatch map_method(:delete, *args, &block) end - def redirect(path, options = {}) + def redirect(*args, &block) + options = args.last.is_a?(Hash) ? args.pop : {} + + path = args.shift || block + path_proc = path.is_a?(Proc) ? path : proc {|params| path % params } status = options[:status] || 301 - lambda { |env| + + lambda do |env| req = Rack::Request.new(env) - url = req.scheme + '://' + req.host + path + params = path_proc.call(env["action_dispatch.request.path_parameters"]) + url = req.scheme + '://' + req.host + params [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] - } + end end private -- cgit v1.2.3 From 3ff9e9ee147b682cb13aed4c057e750228892f42 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 20 Dec 2009 20:37:36 -0800 Subject: Its now possible to use match 'stuff' => 'what#stuff' instead of using the :to for simple routes --- actionpack/lib/action_dispatch/routing/mapper.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 57e992d7dc..46163706c3 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -39,9 +39,13 @@ module ActionDispatch end def match(*args) - options = args.extract_options! - - path = args.first + if args.one? && args.first.is_a?(Hash) + path = args.first.keys.first + options = { :to => args.first.values.first } + else + path = args.first + options = args.extract_options! + end conditions, defaults = {}, {} -- cgit v1.2.3 From 715dd10961cefd1d8039cb81f47788d0a5b97d0d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 17:34:53 -0600 Subject: Less annoying RoutingError message --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index bf2443c1be..a4dc5e0956 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -5,7 +5,7 @@ module ActionDispatch module Routing class RouteSet #:nodoc: NotFound = lambda { |env| - raise ActionController::RoutingError, "No route matches #{env['PATH_INFO'].inspect} with #{env.inspect}" + raise ActionController::RoutingError, "No route matches #{env['PATH_INFO'].inspect}" } PARAMETERS_KEY = 'action_dispatch.request.path_parameters' @@ -426,7 +426,7 @@ module ActionDispatch end end - raise ActionController::RoutingError, "No route matches #{path.inspect} with #{environment.inspect}" + raise ActionController::RoutingError, "No route matches #{path.inspect}" end end end -- cgit v1.2.3 From 94bb3316353ace661a83563f44a9c47baf438f26 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 23 Dec 2009 17:11:17 -0800 Subject: Shift more responsibility from application class to its singleton instance. Treat instantiation and boot as separate steps. Use app.config rather than app.configuration. --- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index a4dc5e0956..498ad3268c 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -273,7 +273,7 @@ module ActionDispatch # TODO: Move this into Railties if defined?(Rails.application) # Find namespaces in controllers/ directory - Rails.application.configuration.controller_paths.each do |load_path| + Rails.application.config.controller_paths.each do |load_path| load_path = File.expand_path(load_path) Dir["#{load_path}/**/*_controller.rb"].collect do |path| namespaces << File.dirname(path).sub(/#{load_path}\/?/, '') -- cgit v1.2.3 From 4d3602a8c4b38052c70655cd7d9dea42ae10ea8d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 23 Dec 2009 17:42:30 -0800 Subject: Routing: fix that route shorthand shouldn't ignore other options. Raise if :as option is given to root method since its name is always 'root' --- actionpack/lib/action_dispatch/routing/mapper.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 46163706c3..40e30bca6f 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -35,13 +35,15 @@ module ActionDispatch end def root(options = {}) + raise "Can't rename root to #{options[:as].inspect}: root is always named 'root'" if options.include?(:as) match '/', options.merge(:as => :root) end def match(*args) if args.one? && args.first.is_a?(Hash) - path = args.first.keys.first - options = { :to => args.first.values.first } + options = args.first + path = options.keys.first + options[:to] = options.delete(path) else path = args.first options = args.extract_options! -- cgit v1.2.3 From aa3565f3a6327c947ded314525ba1d0674d5a71e Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Wed, 23 Dec 2009 23:33:14 -0500 Subject: Allow named_routes to be used with root, and with new DSL short-form. The real use case it to make all of the following act the same: root 'store#index', :as => 'store' match '/' => 'store#index', :as => 'store' match '/', :to => 'store#index', :as => 'store' The test case provided deviates from this in order to demonstrate all three forms in a single set of test routes. Signed-off-by: Jeremy Kemper --- actionpack/lib/action_dispatch/routing/mapper.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 40e30bca6f..3eadb0e9fe 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -35,18 +35,17 @@ module ActionDispatch end def root(options = {}) - raise "Can't rename root to #{options[:as].inspect}: root is always named 'root'" if options.include?(:as) - match '/', options.merge(:as => :root) + match '/', options.reverse_merge(:as => :root) end def match(*args) - if args.one? && args.first.is_a?(Hash) - options = args.first - path = options.keys.first - options[:to] = options.delete(path) + options = args.extract_options! + + if args.empty? + path, to = options.find {|name,value| name.is_a?(String)} + options.merge!(:to => to).delete(path) if path else - path = args.first - options = args.extract_options! + path = args.first end conditions, defaults = {}, {} -- cgit v1.2.3 From 2b7256a42e63640d6e94fe80ee67093ed0f06e4c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 24 Dec 2009 15:23:39 -0800 Subject: Extract Mapping class from monster match method --- actionpack/lib/action_dispatch/routing/mapper.rb | 227 ++++++++++++++--------- 1 file changed, 137 insertions(+), 90 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 46163706c3..a6b32e0152 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -19,9 +19,9 @@ module ActionDispatch @constraints.each { |constraint| if constraint.respond_to?(:matches?) && !constraint.matches?(req) - return [417, {}, []] + return [ 417, {}, [] ] elsif constraint.respond_to?(:call) && !constraint.call(req) - return [417, {}, []] + return [ 417, {}, [] ] end } @@ -29,94 +29,138 @@ module ActionDispatch end end - module Base - def initialize(set) - @set = set + class Mapping + def initialize(set, scope, args) + @set, @scope = set, scope + @path, @options = extract_path_and_options(args) end - - def root(options = {}) - match '/', options.merge(:as => :root) + + def to_route + [ app, conditions, requirements, defaults, @options[:as] ] end - - def match(*args) - if args.one? && args.first.is_a?(Hash) - path = args.first.keys.first - options = { :to => args.first.values.first } - else - path = args.first + + private + def extract_path_and_options(args) options = args.extract_options! - end - - conditions, defaults = {}, {} - - path = nil if path == "" - path = "#{@scope[:path]}#{path}" if @scope[:path] - path = Rack::Mount::Utils.normalize_path(path) if path - - raise ArgumentError, "path is required" unless path - constraints = options[:constraints] || {} - unless constraints.is_a?(Hash) - block, constraints = constraints, {} + if args.empty? + path, to = options.find { |name, value| name.is_a?(String) } + options.merge!(:to => to).delete(path) if path + else + path = args.first + end + + [ normalize_path(path), options ] end - blocks = ((@scope[:blocks] || []) + [block]).compact - constraints = (@scope[:constraints] || {}).merge(constraints) - options.each { |k, v| constraints[k] = v if v.is_a?(Regexp) } - conditions[:path_info] = path - requirements = constraints.dup + def normalize_path(path) + path = nil if path == "" + path = "#{@scope[:path]}#{path}" if @scope[:path] + path = Rack::Mount::Utils.normalize_path(path) if path - path_regexp = Rack::Mount::Strexp.compile(path, constraints, SEPARATORS) - segment_keys = Rack::Mount::RegexpWithNamedGroups.new(path_regexp).names - constraints.reject! { |k, v| segment_keys.include?(k.to_s) } - conditions.merge!(constraints) + raise ArgumentError, "path is required" unless path + + path + end - requirements[:controller] ||= @set.controller_constraints - if via = options[:via] - via = Array(via).map { |m| m.to_s.upcase } - conditions[:request_method] = Regexp.union(*via) + def app + Constraints.new( + to.respond_to?(:call) ? to : Routing::RouteSet::Dispatcher.new(:defaults => defaults), + blocks + ) end - defaults[:controller] ||= @scope[:controller].to_s if @scope[:controller] - - app = initialize_app_endpoint(options, defaults) - validate_defaults!(app, defaults, segment_keys) - app = Constraints.new(app, blocks) + def conditions + { :path_info => @path }.merge(constraints).merge(request_method_condition) + end + + def requirements + @requirements ||= returning(@options[:constraints] || {}) do |requirements| + requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints] + @options.each { |k, v| requirements[k] = v if v.is_a?(Regexp) } + requirements[:controller] ||= @set.controller_constraints + end + end - @set.add_route(app, conditions, requirements, defaults, options[:as]) + def defaults + @defaults ||= if to.respond_to?(:call) + { } + else + defaults = case to + when String + controller, action = to.split('#') + { :controller => controller, :action => action } + when Symbol + { :controller => default_controller, :action => to.to_s } + else + { :controller => default_controller } + end + + if defaults[:controller].blank? && segment_keys.exclude?("controller") + raise ArgumentError, "missing :controller" + end + + if defaults[:action].blank? && segment_keys.exclude?("action") + raise ArgumentError, "missing :action" + end + + defaults + end + end - self - end + + def blocks + if @options[:constraints].present? && !@options[:constraints].is_a?(Hash) + block = @options[:constraints] + else + block = nil + end + + ((@scope[:blocks] || []) + [ block ]).compact + end + + def constraints + @constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller } + end - private - def initialize_app_endpoint(options, defaults) - app = nil - - if options[:to].respond_to?(:call) - app = options[:to] - defaults.delete(:controller) - defaults.delete(:action) - elsif options[:to].is_a?(String) - defaults[:controller], defaults[:action] = options[:to].split('#') - elsif options[:to].is_a?(Symbol) - defaults[:action] = options[:to].to_s + def request_method_condition + if via = @options[:via] + via = Array(via).map { |m| m.to_s.upcase } + { :request_method => Regexp.union(*via) } + else + { } end + end + + def segment_keys + @segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new( + Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS) + ).names + end - app || Routing::RouteSet::Dispatcher.new(:defaults => defaults) + def to + @options[:to] + end + + def default_controller + @scope[:controller].to_s if @scope[:controller] end + end - def validate_defaults!(app, defaults, segment_keys) - return unless app.is_a?(Routing::RouteSet::Dispatcher) + module Base + def initialize(set) + @set = set + end - unless defaults.include?(:controller) || segment_keys.include?("controller") - raise ArgumentError, "missing :controller" - end + def root(options = {}) + match '/', options.reverse_merge(:as => :root) + end - unless defaults.include?(:action) || segment_keys.include?("action") - raise ArgumentError, "missing :action" - end - end + def match(*args) + @set.add_route(*Mapping.new(@set, @scope, args).to_route) + self + end end module HttpHelpers @@ -139,15 +183,16 @@ module ActionDispatch def redirect(*args, &block) options = args.last.is_a?(Hash) ? args.pop : {} - path = args.shift || block - path_proc = path.is_a?(Proc) ? path : proc {|params| path % params } - status = options[:status] || 301 + path = args.shift || block + path_proc = path.is_a?(Proc) ? path : proc { |params| path % params } + status = options[:status] || 301 lambda do |env| - req = Rack::Request.new(env) + req = Rack::Request.new(env) params = path_proc.call(env["action_dispatch.request.path_parameters"]) - url = req.scheme + '://' + req.host + params - [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] + url = req.scheme + '://' + req.host + params + + [ status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently'] ] end end @@ -211,11 +256,11 @@ module ActionDispatch self ensure - @scope[:path] = path if path_set + @scope[:path] = path if path_set @scope[:name_prefix] = name_prefix if name_prefix_set - @scope[:controller] = controller if controller_set - @scope[:options] = options - @scope[:blocks] = blocks + @scope[:controller] = controller if controller_set + @scope[:options] = options + @scope[:blocks] = blocks @scope[:constraints] = constraints end @@ -311,12 +356,12 @@ module ActionDispatch with_scope_level(:resource, resource) do yield if block_given? - get "(.:format)", :to => :show, :as => resource.member_name - post "(.:format)", :to => :create - put "(.:format)", :to => :update - delete "(.:format)", :to => :destroy - get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" - get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" + get "(.:format)", :to => :show, :as => resource.member_name + post "(.:format)", :to => :create + put "(.:format)", :to => :update + delete "(.:format)", :to => :destroy + get "/new(.:format)", :to => :new, :as => "new_#{resource.singular}" + get "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" end end @@ -346,8 +391,9 @@ module ActionDispatch yield if block_given? with_scope_level(:collection) do - get "(.:format)", :to => :index, :as => resource.collection_name + get "(.:format)", :to => :index, :as => resource.collection_name post "(.:format)", :to => :create + with_exclusive_name_prefix :new do get "/new(.:format)", :to => :new, :as => resource.singular end @@ -355,9 +401,10 @@ module ActionDispatch with_scope_level(:member) do scope("/:id") do - get "(.:format)", :to => :show, :as => resource.member_name - put "(.:format)", :to => :update + get "(.:format)", :to => :show, :as => resource.member_name + put "(.:format)", :to => :update delete "(.:format)", :to => :destroy + with_exclusive_name_prefix :edit do get "/edit(.:format)", :to => :edit, :as => resource.singular end @@ -473,4 +520,4 @@ module ActionDispatch include Resources end end -end +end \ No newline at end of file -- cgit v1.2.3 From 7f5d44bac548c9f80e2ea88e191356dbb099593e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 25 Dec 2009 10:14:44 -0800 Subject: The controller key shouldnt be part of the mapping if its not used --- actionpack/lib/action_dispatch/routing/mapper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index a6b32e0152..b48fc6edc5 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -92,9 +92,9 @@ module ActionDispatch controller, action = to.split('#') { :controller => controller, :action => action } when Symbol - { :controller => default_controller, :action => to.to_s } + { :action => to.to_s }.merge(default_controller ? { :controller => default_controller } : {}) else - { :controller => default_controller } + default_controller ? { :controller => default_controller } : {} end if defaults[:controller].blank? && segment_keys.exclude?("controller") -- cgit v1.2.3 From 673fa7f06608151aa4a5e6e754894978b4000b63 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 26 Dec 2009 12:43:50 -0600 Subject: rack-mount 0.4 --- actionpack/lib/action_dispatch/routing/mapper.rb | 38 +++++++++++----------- .../lib/action_dispatch/routing/route_set.rb | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index b48fc6edc5..e655d6a708 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -19,9 +19,9 @@ module ActionDispatch @constraints.each { |constraint| if constraint.respond_to?(:matches?) && !constraint.matches?(req) - return [ 417, {}, [] ] + return [ 404, {'X-Cascade' => 'pass'}, [] ] elsif constraint.respond_to?(:call) && !constraint.call(req) - return [ 417, {}, [] ] + return [ 404, {'X-Cascade' => 'pass'}, [] ] end } @@ -34,11 +34,11 @@ module ActionDispatch @set, @scope = set, scope @path, @options = extract_path_and_options(args) end - + def to_route [ app, conditions, requirements, defaults, @options[:as] ] end - + private def extract_path_and_options(args) options = args.extract_options! @@ -49,18 +49,18 @@ module ActionDispatch else path = args.first end - + [ normalize_path(path), options ] end def normalize_path(path) path = nil if path == "" path = "#{@scope[:path]}#{path}" if @scope[:path] - path = Rack::Mount::Utils.normalize_path(path) if path + path = Rack::Mount::Utils.normalize_path(path) if path raise ArgumentError, "path is required" unless path - - path + + path end @@ -74,7 +74,7 @@ module ActionDispatch def conditions { :path_info => @path }.merge(constraints).merge(request_method_condition) end - + def requirements @requirements ||= returning(@options[:constraints] || {}) do |requirements| requirements.reverse_merge!(@scope[:constraints]) if @scope[:constraints] @@ -96,30 +96,30 @@ module ActionDispatch else default_controller ? { :controller => default_controller } : {} end - + if defaults[:controller].blank? && segment_keys.exclude?("controller") raise ArgumentError, "missing :controller" end - + if defaults[:action].blank? && segment_keys.exclude?("action") raise ArgumentError, "missing :action" end - + defaults end end - + def blocks if @options[:constraints].present? && !@options[:constraints].is_a?(Hash) block = @options[:constraints] else block = nil end - - ((@scope[:blocks] || []) + [ block ]).compact + + ((@scope[:blocks] || []) + [ block ]).compact end - + def constraints @constraints ||= requirements.reject { |k, v| segment_keys.include?(k.to_s) || k == :controller } end @@ -132,7 +132,7 @@ module ActionDispatch { } end end - + def segment_keys @segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new( Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS) @@ -142,7 +142,7 @@ module ActionDispatch def to @options[:to] end - + def default_controller @scope[:controller].to_s if @scope[:controller] end @@ -520,4 +520,4 @@ module ActionDispatch include Resources end end -end \ No newline at end of file +end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 498ad3268c..bd397432ce 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -21,7 +21,7 @@ module ActionDispatch prepare_params!(params) unless controller = controller(params) - return [417, {}, []] + return [404, {'X-Cascade' => 'pass'}, []] end controller.action(params[:action]).call(env) -- cgit v1.2.3 From 95762cbbb3528bcb6a1a8119795df9647daaaab6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 27 Dec 2009 14:13:03 -0800 Subject: Added shorthand for match 'products/overview' that expands to match 'products/overview', :to => 'products#overview', :as => 'products_overview' --- actionpack/lib/action_dispatch/routing/mapper.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index e655d6a708..3d604158f4 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -43,15 +43,29 @@ module ActionDispatch def extract_path_and_options(args) options = args.extract_options! - if args.empty? + case + when using_to_shorthand?(args, options) path, to = options.find { |name, value| name.is_a?(String) } options.merge!(:to => to).delete(path) if path + when using_match_shorthand?(args, options) + path = args.first + options = { :to => path.gsub("/", "#"), :as => path.gsub("/", "_") } else path = args.first end [ normalize_path(path), options ] end + + # match "account" => "account#index" + def using_to_shorthand?(args, options) + args.empty? && options.present? + end + + # match "account/overview" + def using_match_shorthand?(args, options) + args.present? && options.except(:via).empty? && args.first.exclude?(":") + end def normalize_path(path) path = nil if path == "" -- cgit v1.2.3 From 438a8c3ec798e5c1b983db8a8421f634021d4420 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 27 Dec 2009 15:23:30 -0800 Subject: Require the enumberable extension from active support because we use #exclude? --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 3d604158f4..f02c681cf6 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/enumberable' + module ActionDispatch module Routing class Mapper -- cgit v1.2.3 From 3a79117c77e5c5ac44a6a561af968ecd23e3024f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 27 Dec 2009 15:27:18 -0800 Subject: Typo --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index f02c681cf6..4943ac3fa9 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/enumberable' +require 'active_support/core_ext/enumerable' module ActionDispatch module Routing -- cgit v1.2.3 From 7c4fb93ac30b03c512ba163c5444eced18f12171 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 27 Dec 2009 15:38:00 -0800 Subject: Ruby 1.9: string is not enumerable, so #exclude? is not available --- actionpack/lib/action_dispatch/routing/mapper.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 4943ac3fa9..8f33346a4f 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1,5 +1,3 @@ -require 'active_support/core_ext/enumerable' - module ActionDispatch module Routing class Mapper @@ -58,15 +56,15 @@ module ActionDispatch [ normalize_path(path), options ] end - + # match "account" => "account#index" def using_to_shorthand?(args, options) args.empty? && options.present? end - + # match "account/overview" def using_match_shorthand?(args, options) - args.present? && options.except(:via).empty? && args.first.exclude?(":") + args.present? && options.except(:via).empty? && !args.first.include?(':') end def normalize_path(path) -- cgit v1.2.3