From 7c49b1adbbe1ffd42c8cd6fc0439d53895c861cf Mon Sep 17 00:00:00 2001 From: wycats Date: Tue, 16 Mar 2010 17:28:44 -0700 Subject: Make sure options[:anchor] is correct in shorthand cases --- actionpack/lib/action_dispatch/routing/mapper.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index f9b27a5a03..668abb5fdf 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -51,7 +51,9 @@ module ActionDispatch options.merge!(:to => to).delete(path) if path when using_match_shorthand?(args, options) path = args.first - options = { :to => path.gsub("/", "#"), :as => path.gsub("/", "_") } + options = { :to => path.gsub("/", "#"), + :as => path.gsub("/", "_") + }.merge(options || {}) else path = args.first end @@ -301,7 +303,6 @@ module ActionDispatch options = args.extract_options! options = (@scope[:options] || {}).merge(options) - options[:anchor] = true unless options.key?(:anchor) if @scope[:name_prefix] && !options[:as].blank? options[:as] = "#{@scope[:name_prefix]}_#{options[:as]}" @@ -563,6 +564,8 @@ module ActionDispatch def match(*args) options = args.extract_options! + options[:anchor] = true unless options.key?(:anchor) + if args.length > 1 args.each { |path| match(path, options) } return self -- cgit v1.2.3 From 4998e097cc597f26cbe292552bcf5608b87cb1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 22 Mar 2010 21:03:43 +0100 Subject: Make router shortcuts more polite to URLs starting with a leading slash. --- actionpack/lib/action_dispatch/routing/mapper.rb | 32 +++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 668abb5fdf..ddee742021 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -45,20 +45,23 @@ module ActionDispatch def extract_path_and_options(args) options = args.extract_options! - case - when using_to_shorthand?(args, options) + if 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("/", "_") - }.merge(options || {}) else path = args.first end - [ normalize_path(path), options ] + path = normalize_path(path) + + if using_match_shorthand?(path, options) + options = { + :to => path[1..-1].sub(%r{/([^/]*)$}, '#\1'), + :as => path[1..-1].gsub("/", "_") + }.merge!(options) + end + + [ path, options ] end # match "account" => "account#index" @@ -67,14 +70,13 @@ module ActionDispatch end # match "account/overview" - def using_match_shorthand?(args, options) - args.present? && options.except(:via, :anchor).empty? && !args.first.include?(':') + def using_match_shorthand?(path, options) + path && options.except(:via, :anchor).empty? && !path.include?(':') end def normalize_path(path) - path = "#{@scope[:path]}/#{path}" - raise ArgumentError, "path is required" if path.empty? - Mapper.normalize_path(path) + raise ArgumentError, "path is required" if @scope[:path].blank? && path.blank? + Mapper.normalize_path("#{@scope[:path]}/#{path}") end def app @@ -146,8 +148,8 @@ module ActionDispatch def segment_keys @segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new( - Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS) - ).names + Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS) + ).names end def to -- cgit v1.2.3 From b2c2b0ce459a215d389f3ab8bb9e33718460cf51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 26 Mar 2010 15:41:05 +0100 Subject: Rails router automatically calculated for you the controller and named routes in the following scenarios: match "home/about" #=> maps to home#about with named route home_about_path match "about" #=> does not work because it cannot guess the controller match "about" => "home#about" #=> maps to home#about with named route home_about_path match "home/about", :as => "about" #=> maps to home#about with named route about_path --- actionpack/lib/action_dispatch/routing/mapper.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index ddee742021..278cf383ee 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -55,10 +55,8 @@ module ActionDispatch path = normalize_path(path) if using_match_shorthand?(path, options) - options = { - :to => path[1..-1].sub(%r{/([^/]*)$}, '#\1'), - :as => path[1..-1].gsub("/", "_") - }.merge!(options) + options[:to] ||= path[1..-1].sub(%r{/([^/]*)$}, '#\1') + options[:as] ||= path[1..-1].gsub("/", "_") end [ path, options ] @@ -71,7 +69,7 @@ module ActionDispatch # match "account/overview" def using_match_shorthand?(path, options) - path && options.except(:via, :anchor).empty? && !path.include?(':') + path && options.except(:via, :anchor, :to, :as).empty? && path =~ %r{^/[\w\/]+$} end def normalize_path(path) -- cgit v1.2.3 From 3d746fcdb584767c476408f395320e934fd5383e Mon Sep 17 00:00:00 2001 From: Andrew White Date: Fri, 26 Mar 2010 12:16:25 +0000 Subject: Add parameter defaults support to new routing DSL [#4265 state:resolved] Signed-off-by: wycats --- actionpack/lib/action_dispatch/routing/mapper.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 278cf383ee..cc3ed169d8 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -32,6 +32,8 @@ module ActionDispatch end class Mapping + IGNORE_OPTIONS = [:to, :as, :controller, :action, :via, :on, :constraints, :defaults, :only, :except, :anchor] + def initialize(set, scope, args) @set, @scope = set, scope @path, @options = extract_path_and_options(args) @@ -96,7 +98,15 @@ module ActionDispatch end def defaults - @defaults ||= if to.respond_to?(:call) + @defaults ||= (@options[:defaults] || {}).tap do |defaults| + defaults.merge!(default_controller_and_action) + defaults.reverse_merge!(@scope[:defaults]) if @scope[:defaults] + @options.each { |k, v| defaults[k] = v unless v.is_a?(Regexp) || IGNORE_OPTIONS.include?(k.to_sym) } + end + end + + def default_controller_and_action + if to.respond_to?(:call) { } else defaults = case to @@ -299,6 +309,10 @@ module ActionDispatch scope(:constraints => constraints) { yield } end + def defaults(defaults = {}) + scope(:defaults => defaults) { yield } + end + def match(*args) options = args.extract_options! @@ -343,6 +357,10 @@ module ActionDispatch merge_options_scope(parent, child) end + def merge_defaults_scope(parent, child) + merge_options_scope(parent, child) + end + def merge_blocks_scope(parent, child) (parent || []) + [child] end -- cgit v1.2.3 From 39c35ff04b4478968b8994bc5fad74f5840eb64c Mon Sep 17 00:00:00 2001 From: Andrew White Date: Thu, 25 Mar 2010 13:25:56 +0000 Subject: Fix named routes for member actions of singleton resources [#4266 state:resolved] Signed-off-by: wycats --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index cc3ed169d8..5a3868e1d4 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -479,7 +479,7 @@ module ActionDispatch scope(:path => resource.name.to_s, :controller => resource.controller) do with_scope_level(:resource, resource) do - scope(:name_prefix => resource.name.to_s) do + scope(:name_prefix => resource.name.to_s, :as => "") do yield if block_given? end -- cgit v1.2.3