From 99888487b63b313ea4495edd24b666fc5a3c8fe0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 6 Oct 2011 11:06:38 -0700 Subject: fix require --- 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 e921269331..bc956ef216 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,4 +1,4 @@ -require 'journey/router' +require 'journey' require 'forwardable' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/to_query' -- cgit v1.2.3 From 8f863742e34908ed1a9549bb9f984edb58f2b068 Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Mon, 10 Oct 2011 19:53:42 -0300 Subject: allow shorthand routes with nested optional parameters --- actionpack/lib/action_dispatch/routing/mapper.rb | 6 +++--- 1 file changed, 3 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 cd59b13c42..ef31d1e004 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -49,7 +49,7 @@ module ActionDispatch class Mapping #:nodoc: IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix] ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} - SHORTHAND_REGEX = %r{^/[\w/]+$} + SHORTHAND_REGEX = %r{/[\w/]+$} WILDCARD_PATH = %r{\*([^/\)]+)\)?$} def initialize(set, scope, path, options) @@ -70,7 +70,7 @@ module ActionDispatch if using_match_shorthand?(path_without_format, @options) to_shorthand = @options[:to].blank? - @options[:to] ||= path_without_format[1..-1].sub(%r{/([^/]*)$}, '#\1') + @options[:to] ||= path_without_format.gsub(/\(.*\)/, "")[1..-1].sub(%r{/([^/]*)$}, '#\1') end @options.merge!(default_controller_and_action(to_shorthand)) @@ -90,7 +90,7 @@ module ActionDispatch # match "account/overview" def using_match_shorthand?(path, options) - path && options.except(:via, :anchor, :to, :as).empty? && path =~ SHORTHAND_REGEX + path && (options[:to] || options[:action]).nil? && path =~ SHORTHAND_REGEX end def normalize_path(path) -- cgit v1.2.3 From ec371606640f87289f4821f5f197709dd0ebe6f2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 13 Oct 2011 21:40:42 -0700 Subject: Leave escaping up to Journey --- actionpack/lib/action_dispatch/routing/route_set.rb | 7 +++---- 1 file changed, 3 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 bc956ef216..e7bc431783 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -394,10 +394,9 @@ module ActionDispatch if name == :controller value elsif value.is_a?(Array) - value.map { |v| Journey::Router::Utils.escape_uri(v.to_param) }.join('/') - else - return nil unless param = value.to_param - param.split('/').map { |v| Journey::Router::Utils.escape_uri(v) }.join("/") + value.map { |v| v.to_param }.join('/') + elsif param = value.to_param + param end end -- cgit v1.2.3 From e368583ba716f90120e7e6ccfc5ee76de015521c Mon Sep 17 00:00:00 2001 From: mjy Date: Tue, 25 Oct 2011 15:08:28 -0400 Subject: Adds missing closing regex slashes. --- 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 ef31d1e004..09a8c10043 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -696,7 +696,7 @@ module ActionDispatch # Allows you to constrain the nested routes based on a set of rules. # For instance, in order to change the routes to allow for a dot character in the +id+ parameter: # - # constraints(:id => /\d+\.\d+) do + # constraints(:id => /\d+\.\d+/) do # resources :posts # end # @@ -706,7 +706,7 @@ module ActionDispatch # You may use this to also restrict other parameters: # # resources :posts do - # constraints(:post_id => /\d+\.\d+) do + # constraints(:post_id => /\d+\.\d+/) do # resources :comments # end # end -- cgit v1.2.3 From 5f4550889dcab7def4122d37a3379d57627f68e2 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Tue, 1 Nov 2011 08:55:03 +0400 Subject: Fix typo in constraints method documentation --- 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 09a8c10043..e8bfe9bbd0 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -735,7 +735,7 @@ module ActionDispatch # if the user should be given access to that route, or +false+ if the user should not. # # class Iphone - # def self.matches(request) + # def self.matches?(request) # request.env["HTTP_USER_AGENT"] =~ /iPhone/ # end # end -- cgit v1.2.3 From 702aecb126712de9f996da74357cafe14f449d24 Mon Sep 17 00:00:00 2001 From: Aviv Ben-Yosef Date: Tue, 1 Nov 2011 08:59:20 +0200 Subject: Fix typo in Dispatcher#controller documentation --- 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 e7bc431783..2bcde16110 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -37,7 +37,7 @@ module ActionDispatch # If this is a default_controller (i.e. a controller specified by the user) # we should raise an error in case it's not found, because it usually means - # an user error. However, if the controller was retrieved through a dynamic + # a user error. However, if the controller was retrieved through a dynamic # segment, as in :controller(/:action), we should simply return nil and # delegate the control back to Rack cascade. Besides, if this is not a default # controller, it means we should respect the @scope[:module] parameter. -- cgit v1.2.3 From 746331711585cfcb158dafcaf3ea5d60d9825ed2 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Thu, 3 Nov 2011 08:43:28 +0400 Subject: Fix small typos in routing docs --- actionpack/lib/action_dispatch/routing/mapper.rb | 4 +++- 1 file changed, 3 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 e8bfe9bbd0..970236a05a 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -285,7 +285,7 @@ module ActionDispatch # A pattern can also point to a +Rack+ endpoint i.e. anything that # responds to +call+: # - # match 'photos/:id' => lambda {|hash| [200, {}, "Coming soon" } + # match 'photos/:id' => lambda {|hash| [200, {}, "Coming soon"] } # match 'photos/:id' => PhotoRackApp # # Yes, controller actions are just rack endpoints # match 'photos/:id' => PhotosController.action(:show) @@ -1023,6 +1023,7 @@ module ActionDispatch # creates seven different routes in your application, all mapping to # the +Photos+ controller: # + # GET /photos # GET /photos/new # POST /photos # GET /photos/:id @@ -1038,6 +1039,7 @@ module ActionDispatch # # This generates the following comments routes: # + # GET /photos/:photo_id/comments # GET /photos/:photo_id/comments/new # POST /photos/:photo_id/comments # GET /photos/:photo_id/comments/:id -- cgit v1.2.3 From 76780c34f5e0f0e821e408482172454751514241 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 10:25:47 -0700 Subject: some refactoring of the match method --- actionpack/lib/action_dispatch/routing/mapper.rb | 42 ++++++++++-------------- 1 file changed, 17 insertions(+), 25 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 970236a05a..ff96b43a9d 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1245,32 +1245,38 @@ module ActionDispatch parent_resource.instance_of?(Resource) && @scope[:shallow] end - def match(*args) - options = args.extract_options!.dup + def match(path, *rest) + if rest.empty? && Hash === path + options = path + path, to = options.find { |name, value| name.is_a?(String) } + options.merge!(:to => to).delete(path) + paths = [path] + else + options = rest.pop || {} + paths = [path] + rest + end + options[:anchor] = true unless options.key?(:anchor) - if args.length > 1 - args.each { |path| match(path, options.dup) } + if paths.length > 1 + paths.each { |path| match(path, options.dup) } return self end on = options.delete(:on) if VALID_ON_OPTIONS.include?(on) - args.push(options) - return send(on){ match(*args) } + return send(on){ match(path, options) } elsif on raise ArgumentError, "Unknown scope #{on.inspect} given to :on" end if @scope[:scope_level] == :resources - args.push(options) - return nested { match(*args) } + return nested { match(path, options) } elsif @scope[:scope_level] == :resource - args.push(options) - return member { match(*args) } + return member { match(path, options) } end - action = args.first + action = path path = path_for_action(action, options.delete(:path)) if action.to_s =~ /^[\w\/]+$/ @@ -1466,19 +1472,6 @@ module ActionDispatch end end - module Shorthand #:nodoc: - def match(path, *rest) - if rest.empty? && Hash === path - options = path - path, to = options.find { |name, value| name.is_a?(String) } - options.merge!(:to => to).delete(path) - super(path, options) - else - super - end - end - end - def initialize(set) #:nodoc: @set = set @scope = { :path_names => @set.resources_path_names } @@ -1489,7 +1482,6 @@ module ActionDispatch include Redirection include Scoping include Resources - include Shorthand end end end -- cgit v1.2.3 From 494ab25772ba5eca741fe98f6beb48954949993f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 11:15:41 -0700 Subject: breaking match down to smaller methods --- actionpack/lib/action_dispatch/routing/mapper.rb | 41 +++++++++++++----------- 1 file changed, 22 insertions(+), 19 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 ff96b43a9d..4c4b99c03e 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -374,10 +374,6 @@ module ActionDispatch # # Matches any request starting with 'path' # match 'path' => 'c#a', :anchor => false def match(path, options=nil) - mapping = Mapping.new(@set, @scope, path, options || {}) - app, conditions, requirements, defaults, as, anchor = mapping.to_route - @set.add_route(app, conditions, requirements, defaults, as, anchor) - self end # Mount a Rack-based application to be used within the application. @@ -1249,7 +1245,8 @@ module ActionDispatch if rest.empty? && Hash === path options = path path, to = options.find { |name, value| name.is_a?(String) } - options.merge!(:to => to).delete(path) + options[:to] = to + options.delete(path) paths = [path] else options = rest.pop || {} @@ -1258,25 +1255,29 @@ module ActionDispatch options[:anchor] = true unless options.key?(:anchor) - if paths.length > 1 - paths.each { |path| match(path, options.dup) } - return self - end + paths.each { |path| decomposed_match(path, options.dup) } + self + end + def decomposed_match(path, options) # :nodoc: on = options.delete(:on) if VALID_ON_OPTIONS.include?(on) - return send(on){ match(path, options) } + return send(on){ decomposed_match(path, options) } elsif on raise ArgumentError, "Unknown scope #{on.inspect} given to :on" end - if @scope[:scope_level] == :resources - return nested { match(path, options) } - elsif @scope[:scope_level] == :resource - return member { match(path, options) } + case @scope[:scope_level] + when :resources + nested { decomposed_match(path, options) } + when :resource + member { decomposed_match(path, options) } + else + add_route(path, options) end + end - action = path + def add_route(action, options) # :nodoc: path = path_for_action(action, options.delete(:path)) if action.to_s =~ /^[\w\/]+$/ @@ -1285,13 +1286,15 @@ module ActionDispatch action = nil end - if options.key?(:as) && !options[:as] + if !options.fetch(:as) { true } options.delete(:as) else options[:as] = name_for_action(options[:as], action) end - super(path, options) + mapping = Mapping.new(@set, @scope, path, options) + app, conditions, requirements, defaults, as, anchor = mapping.to_route + @set.add_route(app, conditions, requirements, defaults, as, anchor) end def root(options={}) @@ -1355,11 +1358,11 @@ module ActionDispatch end def resource_scope? #:nodoc: - @scope[:scope_level].in?([:resource, :resources]) + [:resource, :resources].include? @scope[:scope_level] end def resource_method_scope? #:nodoc: - @scope[:scope_level].in?([:collection, :member, :new]) + [:collection, :member, :new].include? @scope[:scope_level] end def with_exclusive_scope -- cgit v1.2.3 From 7459ba4f6c7d7b98fc0985255ccfd93186b0950f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 11:42:59 -0700 Subject: pushing hash validation up --- actionpack/lib/action_dispatch/routing/mapper.rb | 27 ++++++++++++------------ 1 file changed, 14 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 4c4b99c03e..d196f16600 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1255,25 +1255,26 @@ module ActionDispatch options[:anchor] = true unless options.key?(:anchor) + if options[:on] && !VALID_ON_OPTIONS.include?(options[:on]) + raise ArgumentError, "Unknown scope #{on.inspect} given to :on" + end + paths.each { |path| decomposed_match(path, options.dup) } self end def decomposed_match(path, options) # :nodoc: - on = options.delete(:on) - if VALID_ON_OPTIONS.include?(on) - return send(on){ decomposed_match(path, options) } - elsif on - raise ArgumentError, "Unknown scope #{on.inspect} given to :on" - end - - case @scope[:scope_level] - when :resources - nested { decomposed_match(path, options) } - when :resource - member { decomposed_match(path, options) } + if on = options.delete(:on) + send(on) { decomposed_match(path, options) } else - add_route(path, options) + case @scope[:scope_level] + when :resources + nested { decomposed_match(path, options) } + when :resource + member { decomposed_match(path, options) } + else + add_route(path, options) + end end end -- cgit v1.2.3 From 648f6113d1f0c6f9cdb2352a24e84c2c204d564b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 15:04:48 -0700 Subject: move constants to methods since nothing else is using them --- actionpack/lib/action_dispatch/routing/mapper.rb | 11 +++++------ 1 file changed, 5 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 d196f16600..f49155349c 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -863,8 +863,6 @@ module ActionDispatch CANONICAL_ACTIONS = %w(index create new show update destroy) class Resource #:nodoc: - DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit] - attr_reader :controller, :path, :options def initialize(entities, options = {}) @@ -876,7 +874,7 @@ module ActionDispatch end def default_actions - self.class::DEFAULT_ACTIONS + [:index, :create, :new, :show, :update, :destroy, :edit] end def actions @@ -930,16 +928,17 @@ module ActionDispatch end class SingletonResource < Resource #:nodoc: - DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit] - def initialize(entities, options) super - @as = nil @controller = (options[:controller] || plural).to_s @as = options[:as] end + def default_actions + [:show, :create, :update, :destroy, :new, :edit] + end + def plural @plural ||= name.to_s.pluralize end -- cgit v1.2.3 From 33543ac87148cfdd5b1917a0698bccaf55690e28 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 15:14:25 -0700 Subject: stop doing is_a? checks on the resource type --- 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 f49155349c..91220e1cf7 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -986,7 +986,7 @@ module ActionDispatch return self end - resource_scope(SingletonResource.new(resources.pop, options)) do + resource_scope(:resource, SingletonResource.new(resources.pop, options)) do yield if block_given? collection do @@ -1117,7 +1117,7 @@ module ActionDispatch return self end - resource_scope(Resource.new(resources.pop, options)) do + resource_scope(:resources, Resource.new(resources.pop, options)) do yield if block_given? collection do @@ -1387,8 +1387,8 @@ module ActionDispatch @scope[:scope_level_resource] = old_resource end - def resource_scope(resource) #:nodoc: - with_scope_level(resource.is_a?(SingletonResource) ? :resource : :resources, resource) do + def resource_scope(level, resource) #:nodoc: + with_scope_level(level, resource) do scope(parent_resource.resource_scope) do yield end -- cgit v1.2.3 From 636405d2a6187cf1fe90f35b5e2945758afde160 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 15:18:36 -0700 Subject: cleaning up variable names to match method parameter names --- actionpack/lib/action_dispatch/routing/mapper.rb | 14 ++++++++------ 1 file changed, 8 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 91220e1cf7..4fc0d78267 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1387,8 +1387,8 @@ module ActionDispatch @scope[:scope_level_resource] = old_resource end - def resource_scope(level, resource) #:nodoc: - with_scope_level(level, resource) do + def resource_scope(kind, resource) #:nodoc: + with_scope_level(kind, resource) do scope(parent_resource.resource_scope) do yield end @@ -1396,10 +1396,12 @@ module ActionDispatch end def nested_options #:nodoc: - {}.tap do |options| - options[:as] = parent_resource.member_name - options[:constraints] = { "#{parent_resource.singular}_id".to_sym => id_constraint } if id_constraint? - end + options = { :as => parent_resource.member_name } + options[:constraints] = { + :"#{parent_resource.singular}_id" => id_constraint + } if id_constraint? + + options end def id_constraint? #:nodoc: -- cgit v1.2.3 From ecbae9947830bb3ec42db097140bf1b2154dee31 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 15:40:10 -0700 Subject: no need for type checking --- 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 4fc0d78267..f2a2e92011 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1350,7 +1350,7 @@ module ActionDispatch end def scope_action_options? #:nodoc: - @scope[:options].is_a?(Hash) && (@scope[:options][:only] || @scope[:options][:except]) + @scope[:options] && (@scope[:options][:only] || @scope[:options][:except]) end def scope_action_options #:nodoc: -- cgit v1.2.3 From 3178cc9a80262d3bf7754f3507ef60243b46634f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 21 Oct 2011 16:47:49 -0700 Subject: copy options keys to the right place so that undo will work correctly --- actionpack/lib/action_dispatch/routing/mapper.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 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 f2a2e92011..21624bcfc2 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -603,9 +603,12 @@ module ActionDispatch options[:constraints] ||= {} unless options[:constraints].is_a?(Hash) - block, options[:constraints] = options[:constraints], {} + options[:blocks] = options[:constraints] + options[:constraints] = {} end + options[:options] = options + scope_options.each do |option| if value = options.delete(option) recover[option] = @scope[option] @@ -613,21 +616,12 @@ module ActionDispatch end end - recover[:block] = @scope[:blocks] - @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block) - - recover[:options] = @scope[:options] - @scope[:options] = merge_options_scope(@scope[:options], options) - yield self ensure scope_options.each do |option| @scope[option] = recover[option] if recover.has_key?(option) end - - @scope[:options] = recover[:options] - @scope[:blocks] = recover[:block] end # Scopes routes to a specific controller -- cgit v1.2.3 From 4589b2419b6c2f6d8b1ea0873999a4d0fa21bdb3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 16:26:11 -0400 Subject: require that all blocks have arity of 2 --- actionpack/lib/action_dispatch/routing/redirection.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index 804991ad5f..27dbf72519 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -43,13 +43,19 @@ module ActionDispatch path = args.shift path_proc = if path.is_a?(String) - proc { |params| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) } + proc { |params, request| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) } elsif options.any? options_proc(options) elsif path.respond_to?(:call) proc { |params, request| path.call(params, request) } elsif block - block + if block.arity < 2 + msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" + ActiveSupport::Deprecation.warn msg + lambda { |params, _| block.call(params) } + else + block + end else raise ArgumentError, "redirection argument not supported" end @@ -85,8 +91,7 @@ module ActionDispatch lambda do |env| req = Request.new(env) - params = [req.symbolized_path_parameters] - params << req if path_proc.arity > 1 + params = [req.symbolized_path_parameters, req] uri = URI.parse(path_proc.call(*params)) uri.scheme ||= req.scheme @@ -107,4 +112,4 @@ module ActionDispatch end end -end \ No newline at end of file +end -- cgit v1.2.3 From 163b645472b863231558c93abcdc1454db00b287 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 16:28:38 -0400 Subject: arity check has been pushed up, so no need for proc wrapping --- actionpack/lib/action_dispatch/routing/redirection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index 27dbf72519..d61320980d 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -47,7 +47,7 @@ module ActionDispatch elsif options.any? options_proc(options) elsif path.respond_to?(:call) - proc { |params, request| path.call(params, request) } + path elsif block if block.arity < 2 msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" -- cgit v1.2.3 From a8a4264858d5eac4f11ce6545f63e854bbc35759 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 16:36:48 -0400 Subject: make sure to require the right deprecation warning file --- actionpack/lib/action_dispatch/routing/redirection.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index d61320980d..a234e26151 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -1,4 +1,5 @@ require 'action_dispatch/http/request' +require 'active_support/deprecation/reporting' module ActionDispatch module Routing -- cgit v1.2.3 From 0809c675ef5831852b7c1aa8497402b2beff5185 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 17:49:20 -0400 Subject: remove the :path feature to redirects, since it cannot work --- .../lib/action_dispatch/routing/redirection.rb | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index a234e26151..cd9ba5a4db 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -68,23 +68,15 @@ module ActionDispatch def options_proc(options) proc do |params, request| - path = if options[:path].nil? - request.path - elsif params.empty? || !options[:path].match(/%\{\w*\}/) - options.delete(:path) - else - (options.delete(:path) % params) - end - - default_options = { + url_options = { :protocol => request.protocol, - :host => request.host, - :port => request.optional_port, - :path => path, - :params => request.query_parameters - } + :host => request.host, + :port => request.optional_port, + :path => request.path, + :params => request.query_parameters + }.merge options - ActionDispatch::Http::URL.url_for(options.reverse_merge(default_options)) + ActionDispatch::Http::URL.url_for url_options end end -- cgit v1.2.3 From d34efdd260d7a894537267a6186f16abe1b9335c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 18:18:04 -0400 Subject: moving redirection to objects --- .../lib/action_dispatch/routing/redirection.rb | 103 ++++++++++++--------- 1 file changed, 58 insertions(+), 45 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index cd9ba5a4db..35dabae1f2 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -3,6 +3,54 @@ require 'active_support/deprecation/reporting' module ActionDispatch module Routing + class Redirect + attr_reader :status, :block + + def initialize(status, block) + @status = status + @block = block + end + + def call(env) + req = Request.new(env) + + uri = URI.parse(path(req.symbolized_path_parameters, req)) + uri.scheme ||= req.scheme + uri.host ||= req.host + uri.port ||= req.port unless req.standard_port? + + body = %(You are being redirected.) + + headers = { + 'Location' => uri.to_s, + 'Content-Type' => 'text/html', + 'Content-Length' => body.length.to_s + } + + [ status, headers, [body] ] + end + + def path(params, request) + block.call params, request + end + end + + class OptionRedirect < Redirect + alias :options :block + + def path(params, request) + url_options = { + :protocol => request.protocol, + :host => request.host, + :port => request.optional_port, + :path => request.path, + :params => request.query_parameters + }.merge options + + ActionDispatch::Http::URL.url_for url_options + end + end + module Redirection # Redirect any path to another path: @@ -43,66 +91,31 @@ module ActionDispatch path = args.shift - path_proc = if path.is_a?(String) - proc { |params, request| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) } + if path.is_a?(String) + block_redirect status, lambda { |params, request| + (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) + } elsif options.any? - options_proc(options) + OptionRedirect.new(status, options) elsif path.respond_to?(:call) - path + block_redirect status, path elsif block if block.arity < 2 msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" ActiveSupport::Deprecation.warn msg - lambda { |params, _| block.call(params) } + block_redirect status, lambda { |params, _| block.call(params) } else - block + block_redirect status, block end else raise ArgumentError, "redirection argument not supported" end - - redirection_proc(status, path_proc) end private - - def options_proc(options) - proc do |params, request| - url_options = { - :protocol => request.protocol, - :host => request.host, - :port => request.optional_port, - :path => request.path, - :params => request.query_parameters - }.merge options - - ActionDispatch::Http::URL.url_for url_options - end - end - - def redirection_proc(status, path_proc) - lambda do |env| - req = Request.new(env) - - params = [req.symbolized_path_parameters, req] - - uri = URI.parse(path_proc.call(*params)) - uri.scheme ||= req.scheme - uri.host ||= req.host - uri.port ||= req.port unless req.standard_port? - - body = %(You are being redirected.) - - headers = { - 'Location' => uri.to_s, - 'Content-Type' => 'text/html', - 'Content-Length' => body.length.to_s - } - - [ status, headers, [body] ] - end + def block_redirect(status, path_proc) + Redirect.new status, path_proc end - end end end -- cgit v1.2.3 From 99d94f126d05398ec0917d75253ab1548bc54ba3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 1 Nov 2011 15:53:02 -0200 Subject: Refactoring the redirect method for the router api. --- .../lib/action_dispatch/routing/redirection.rb | 44 ++++++++++------------ 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index 35dabae1f2..b7df456e91 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -3,7 +3,7 @@ require 'active_support/deprecation/reporting' module ActionDispatch module Routing - class Redirect + class Redirect # :nodoc: attr_reader :status, :block def initialize(status, block) @@ -35,7 +35,7 @@ module ActionDispatch end end - class OptionRedirect < Redirect + class OptionRedirect < Redirect # :nodoc: alias :options :block def path(params, request) @@ -89,33 +89,27 @@ module ActionDispatch options = args.last.is_a?(Hash) ? args.pop : {} status = options.delete(:status) || 301 + return OptionRedirect.new(status, options) if options.any? + path = args.shift - if path.is_a?(String) - block_redirect status, lambda { |params, request| - (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) - } - elsif options.any? - OptionRedirect.new(status, options) - elsif path.respond_to?(:call) - block_redirect status, path - elsif block - if block.arity < 2 - msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" - ActiveSupport::Deprecation.warn msg - block_redirect status, lambda { |params, _| block.call(params) } - else - block_redirect status, block - end - else - raise ArgumentError, "redirection argument not supported" - end - end + block = lambda { |params, request| + (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) + } if String === path + + block = path if path.respond_to? :call - private - def block_redirect(status, path_proc) - Redirect.new status, path_proc + # :FIXME: remove in Rails 4.0 + if block && block.respond_to?(:arity) && block.arity < 2 + msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" + ActiveSupport::Deprecation.warn msg + block = lambda { |params, _| block.call(params) } end + + raise ArgumentError, "redirection argument not supported" unless block + + Redirect.new status, block + end end end end -- cgit v1.2.3 From 396ef44be48dbcbc75d313980d0ba272a6200099 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 18 Nov 2011 10:36:02 -0800 Subject: Revert "make sure to require the right deprecation warning file" This reverts commit 9d725e3df502a07222f35576108eb2df2bd88259. --- actionpack/lib/action_dispatch/routing/redirection.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index b7df456e91..330400e139 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -1,5 +1,4 @@ require 'action_dispatch/http/request' -require 'active_support/deprecation/reporting' module ActionDispatch module Routing -- cgit v1.2.3 From d806ea20506f824efbe2c8b69928929977cc3a59 Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Sat, 19 Nov 2011 11:29:29 +0530 Subject: Warning removed for shadowing variable --- 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 21624bcfc2..e3ad3f9ba7 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1252,7 +1252,7 @@ module ActionDispatch raise ArgumentError, "Unknown scope #{on.inspect} given to :on" end - paths.each { |path| decomposed_match(path, options.dup) } + paths.each { |_path| decomposed_match(_path, options.dup) } self end -- cgit v1.2.3 From 3d2bd6938586086fbce4d6c087e4f9cd528e0212 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 19 Nov 2011 20:35:54 -0800 Subject: Revert "copy options keys to the right place so that undo will work correctly" This reverts commit 3178cc9a80262d3bf7754f3507ef60243b46634f. --- 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 e3ad3f9ba7..7947e9d393 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -603,12 +603,9 @@ module ActionDispatch options[:constraints] ||= {} unless options[:constraints].is_a?(Hash) - options[:blocks] = options[:constraints] - options[:constraints] = {} + block, options[:constraints] = options[:constraints], {} end - options[:options] = options - scope_options.each do |option| if value = options.delete(option) recover[option] = @scope[option] @@ -616,12 +613,21 @@ module ActionDispatch end end + recover[:block] = @scope[:blocks] + @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block) + + recover[:options] = @scope[:options] + @scope[:options] = merge_options_scope(@scope[:options], options) + yield self ensure scope_options.each do |option| @scope[option] = recover[option] if recover.has_key?(option) end + + @scope[:options] = recover[:options] + @scope[:blocks] = recover[:block] end # Scopes routes to a specific controller -- cgit v1.2.3 From 9a45867b094c0a41303e24b68414bc69d2263bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=A3is=20Ozols?= Date: Fri, 25 Nov 2011 14:22:36 +0200 Subject: Remove unnecessary comment. --- actionpack/lib/action_dispatch/routing/url_for.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 8fc8dc191b..334ddd5c2f 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -42,7 +42,7 @@ module ActionDispatch # url_for(:controller => 'users', # :action => 'new', # :message => 'Welcome!', - # :host => 'www.example.com') # Changed this. + # :host => 'www.example.com') # # => "http://www.example.com/users/new?message=Welcome%21" # # By default, all controllers and views have access to a special version of url_for, -- cgit v1.2.3 From a607a9d978c4a84935fa23556de1dde5aea274d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=A3is=20Ozols?= Date: Fri, 25 Nov 2011 14:24:14 +0200 Subject: what's -> that's --- actionpack/lib/action_dispatch/routing/url_for.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 334ddd5c2f..39ba83fb9a 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -52,7 +52,7 @@ module ActionDispatch # # For convenience reasons, mailers provide a shortcut for ActionController::UrlFor#url_for. # So within mailers, you only have to type 'url_for' instead of 'ActionController::UrlFor#url_for' - # in full. However, mailers don't have hostname information, and what's why you'll still + # in full. However, mailers don't have hostname information, and that's why you'll still # have to specify the :host argument when generating URLs in mailers. # # -- cgit v1.2.3 From e3bc1385f1e8d2816deb215880d99aca136c0cc3 Mon Sep 17 00:00:00 2001 From: Aviv Ben-Yosef Date: Wed, 30 Nov 2011 06:55:34 +0200 Subject: Fixing incorrect documentation `path_names` can only be used for affecting `new` and `edit` --- 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 7947e9d393..88e422c05d 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1046,8 +1046,8 @@ module ActionDispatch # Takes same options as Base#match as well as: # # [:path_names] - # Allows you to change the paths of the seven default actions. - # Paths not specified are not changed. + # Allows you to change the segment component of the +edit+ and +new+ actions. + # Actions not specified are not changed. # # resources :posts, :path_names => { :new => "brand_new" } # -- cgit v1.2.3 From 71d769e3b58cb56b4b1d5143936c65be8b27c490 Mon Sep 17 00:00:00 2001 From: Andy Jeffries Date: Mon, 5 Dec 2011 15:41:38 +0000 Subject: Named Routes shouldn't override existing ones (currently route recognition goes with the earliest match, named routes use the latest match) --- 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 2bcde16110..c64214431a 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -356,7 +356,7 @@ module ActionDispatch conditions = build_conditions(conditions, valid_conditions, path.names.map { |x| x.to_sym }) route = @set.add_route(app, path, conditions, defaults, name) - named_routes[name] = route if name + named_routes[name] = route if name && !named_routes[name] route end -- cgit v1.2.3 From 7280787a53436046e992305a235e66e4fb458e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 8 Dec 2011 19:52:26 +0100 Subject: Improve cache on route_key lookup. --- actionpack/lib/action_dispatch/routing/polymorphic_routes.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index e989a38d8b..013cf93dbc 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -165,7 +165,7 @@ module ActionDispatch if parent.is_a?(Symbol) || parent.is_a?(String) parent else - ActiveModel::Naming.route_key(parent).singularize + ActiveModel::Naming.singular_route_key(parent) end end else @@ -176,9 +176,11 @@ module ActionDispatch if record.is_a?(Symbol) || record.is_a?(String) route << record elsif record - route << ActiveModel::Naming.route_key(record) - route = [route.join("_").singularize] if inflection == :singular - route << "index" if ActiveModel::Naming.uncountable?(record) && inflection == :plural + if inflection == :singular + route << ActiveModel::Naming.singular_route_key(record) + else + route << ActiveModel::Naming.route_key(record) + end else raise ArgumentError, "Nil location provided. Can't build URI." end -- cgit v1.2.3 From c41f08cefe6fa3747ee79001d9c88dc988e8064d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 8 Dec 2011 21:16:12 +0100 Subject: Move symbolize keys to the inner options as we can assume url_options will be properly symbolized. --- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- actionpack/lib/action_dispatch/routing/url_for.rb | 2 +- 2 files 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 c64214431a..6d6de36a08 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -557,7 +557,7 @@ module ActionDispatch path_addition, params = generate(path_options, path_segments || {}) path << path_addition - ActionDispatch::Http::URL.url_for(options.merge({ + ActionDispatch::Http::URL.url_for(options.merge!({ :path => path, :params => params, :user => user, diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 39ba83fb9a..22e41c9c16 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -145,7 +145,7 @@ module ActionDispatch when String options when nil, Hash - _routes.url_for((options || {}).reverse_merge(url_options).symbolize_keys) + _routes.url_for((options || {}).symbolize_keys.reverse_merge!(url_options)) else polymorphic_url(options) end -- cgit v1.2.3 From 192e55c38ed9b48672b9e216c9805b782b835d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 16 Dec 2011 10:29:45 +0100 Subject: Do not raise an exception if an invalid route was generated automatically. --- actionpack/lib/action_dispatch/routing/mapper.rb | 14 +++++++++++--- 1 file changed, 11 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 88e422c05d..4d83c6dee1 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1286,7 +1286,7 @@ module ActionDispatch action = nil end - if !options.fetch(:as) { true } + if !options.fetch(:as, true) options.delete(:as) else options[:as] = name_for_action(options[:as], action) @@ -1472,8 +1472,16 @@ module ActionDispatch [name_prefix, member_name, prefix] end - candidate = name.select(&:present?).join("_").presence - candidate unless as.nil? && @set.routes.find { |r| r.name == candidate } + if candidate = name.select(&:present?).join("_").presence + # If a name was not explicitly given, we check if it is valid + # and return nil in case it isn't. Otherwise, we pass the invalid name + # forward so the underlying router engine treats it and raises an exception. + if as.nil? + candidate unless @set.routes.find { |r| r.name == candidate } || candidate !~ /\A[_a-z]/i + else + candidate + end + end end end -- cgit v1.2.3 From 6c57177f2c7f4f934716d588545902d5fc00fa99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 20 Dec 2011 15:12:38 +0100 Subject: Remove deprecation warnings from Action Pack. --- actionpack/lib/action_dispatch/routing/redirection.rb | 9 --------- 1 file changed, 9 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index 330400e139..617b24b46a 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -97,16 +97,7 @@ module ActionDispatch } if String === path block = path if path.respond_to? :call - - # :FIXME: remove in Rails 4.0 - if block && block.respond_to?(:arity) && block.arity < 2 - msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" - ActiveSupport::Deprecation.warn msg - block = lambda { |params, _| block.call(params) } - end - raise ArgumentError, "redirection argument not supported" unless block - Redirect.new status, block end end -- cgit v1.2.3 From 77ed289609ff02b23b9d3c89aa0aeace8d8b280d Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 24 Dec 2011 01:01:25 +0300 Subject: remove checking for non-empty string before calling to_sym --- actionpack/lib/action_dispatch/routing/mapper.rb | 3 +-- 1 file changed, 1 insertion(+), 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 4d83c6dee1..2117cb76b5 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1432,8 +1432,7 @@ module ActionDispatch end def action_path(name, path = nil) #:nodoc: - # Ruby 1.8 can't transform empty strings to symbols - name = name.to_sym if name.is_a?(String) && !name.empty? + name = name.to_sym if name.is_a?(String) path || @scope[:path_names][name] || name.to_s end -- cgit v1.2.3 From 5ca86ac8f924b333a5a01a47cc07cbcf39c16e80 Mon Sep 17 00:00:00 2001 From: Sergey Nartimov Date: Sat, 24 Dec 2011 15:57:54 +0300 Subject: deprecate String#encoding_aware? and remove its usage --- 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 6d6de36a08..d065d9f9d8 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -584,7 +584,7 @@ module ActionDispatch @router.recognize(req) do |route, matches, params| params.each do |key, value| if value.is_a?(String) - value = value.dup.force_encoding(Encoding::BINARY) if value.encoding_aware? + value = value.dup.force_encoding(Encoding::BINARY) params[key] = URI.parser.unescape(value) end end -- cgit v1.2.3 From 28cd098d99c52486aecb72aab39105d8abcd52ad Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 26 Dec 2011 11:31:22 +0100 Subject: Correctly display rack apps with dynamic constraints in RoutesInspector If you used dynamic constraint like that: scope :constraint => MyConstraint.new do mount RackApp => "/foo" end routes were not displayed correctly when using `rake routes`. This commit fixes it. If you want nice display of dynamic constraints in `rake routes` output, please just override to_s method in your constraint's class. --- 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 2117cb76b5..ce4d407217 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -16,7 +16,7 @@ module ActionDispatch end end - attr_reader :app + attr_reader :app, :constraints def initialize(app, constraints, request) @app, @constraints, @request = app, constraints, request -- cgit v1.2.3 From a7d3851bdbc030e1eb14b66fd72ad602b6d4348c Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Wed, 28 Dec 2011 10:59:28 -0800 Subject: Documented about using :path option for resources --- actionpack/lib/action_dispatch/routing/mapper.rb | 7 +++++++ 1 file changed, 7 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 2117cb76b5..84f544c546 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1053,6 +1053,13 @@ module ActionDispatch # # The above example will now change /posts/new to /posts/brand_new # + # [:path] + # Allows you to change the path prefix for the resource. + # + # resources :posts, :path => 'postings' + # + # The resource and all segments will now route to /postings instead of /posts + # # [:only] # Only generate routes for the given actions. # -- cgit v1.2.3 From 25b10f4c1c7db7dea631d6877fdd6eae5947fe21 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 09:02:54 -0800 Subject: modules don't have any instance methods --- 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 d065d9f9d8..9de8849a90 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -94,9 +94,7 @@ module ActionDispatch @routes = {} @helpers = [] - @module ||= Module.new do - instance_methods.each { |selector| remove_method(selector) } - end + @module ||= Module.new end def add(name, route) -- cgit v1.2.3 From c0904e47f2982c2cd323c44060e99cfbe8e9869a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 09:06:34 -0800 Subject: decouple initialize from clear!. Initialize ivars in initialize, clear ivars in clear! --- actionpack/lib/action_dispatch/routing/route_set.rb | 10 +++++----- 1 file changed, 5 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 9de8849a90..e691124193 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -83,7 +83,9 @@ module ActionDispatch attr_reader :routes, :helpers, :module def initialize - clear! + @routes = {} + @helpers = [] + @module = Module.new end def helper_names @@ -91,10 +93,8 @@ module ActionDispatch end def clear! - @routes = {} - @helpers = [] - - @module ||= Module.new + @routes.clear + @helpers.clear end def add(name, route) -- cgit v1.2.3 From 88aeeee288641a3009f05574079d4c50277e77a5 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 09:52:18 -0800 Subject: removing dead code. --- actionpack/lib/action_dispatch/routing/route_set.rb | 15 +++------------ 1 file changed, 3 insertions(+), 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 e691124193..3b9d8dc57b 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -123,16 +123,7 @@ module ActionDispatch routes.length end - def reset! - old_routes = routes.dup - clear! - old_routes.each do |name, route| - add(name, route) - end - end - - def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) - reset! if regenerate + def install(destinations = [ActionController::Base, ActionView::Base]) Array(destinations).each do |dest| dest.__send__(:include, @module) end @@ -285,9 +276,9 @@ module ActionDispatch @prepend.each { |blk| eval_block(blk) } end - def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) + def install_helpers(destinations = [ActionController::Base, ActionView::Base]) Array(destinations).each { |d| d.module_eval { include Helpers } } - named_routes.install(destinations, regenerate_code) + named_routes.install(destinations) end module MountedHelpers -- cgit v1.2.3 From 0035c54e2f5b0d5b4d27d50d16fb91bad4060c5a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 10:25:58 -0800 Subject: don't use instance eval, just reference variables so we don't have to worry about "inspect" marshalling --- actionpack/lib/action_dispatch/routing/route_set.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 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 3b9d8dc57b..3ffa4b4bea 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -149,22 +149,23 @@ module ActionDispatch def define_hash_access(route, name, kind, options) selector = hash_access_name(name, kind) - # We use module_eval to avoid leaks - @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 - remove_possible_method :#{selector} - def #{selector}(*args) - options = args.extract_options! - result = #{options.inspect} + @module.module_eval do + remove_possible_method selector + + define_method(selector) do |*args| + inner_options = args.extract_options! + result = options.dup if args.any? result[:_positional_args] = args - result[:_positional_keys] = #{route.segment_keys.inspect} + result[:_positional_keys] = route.segment_keys end - result.merge(options) + result.merge(inner_options) end - protected :#{selector} - END_EVAL + + protected selector + end helpers << selector end -- cgit v1.2.3 From 72c290ca1597f2ecf3252aaa51d3baef0cac0440 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 11:47:21 -0800 Subject: avoid extra method calls by just defining the delegate --- actionpack/lib/action_dispatch/routing/route_set.rb | 5 +++-- 1 file changed, 3 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 3ffa4b4bea..fb49bc153c 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -315,9 +315,10 @@ module ActionDispatch include UrlFor @_routes = routes - class << self - delegate :url_for, :to => '@_routes' + def self.url_for(options) + @_routes.url_for options end + extend routes.named_routes.module # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that -- cgit v1.2.3 From fb3e09a877f6b4649dff8c9fc4a991a3ac7fa3cf Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 11:50:35 -0800 Subject: don't need the begin / end --- .../lib/action_dispatch/routing/route_set.rb | 38 ++++++++++------------ 1 file changed, 17 insertions(+), 21 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 fb49bc153c..22d4ccf623 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -307,33 +307,29 @@ module ActionDispatch end def url_helpers - @url_helpers ||= begin - routes = self + routes = self - helpers = Module.new do - extend ActiveSupport::Concern - include UrlFor + @url_helpers ||= Module.new { + extend ActiveSupport::Concern + include UrlFor - @_routes = routes - def self.url_for(options) - @_routes.url_for options - end + @_routes = routes + def self.url_for(options) + @_routes.url_for options + end - extend routes.named_routes.module + extend routes.named_routes.module - # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that - # we can include? - # Yes plz - JP - included do - routes.install_helpers(self) - singleton_class.send(:redefine_method, :_routes) { routes } - end - - define_method(:_routes) { @_routes || routes } + # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that + # we can include? + # Yes plz - JP + included do + routes.install_helpers(self) + singleton_class.send(:redefine_method, :_routes) { routes } end - helpers - end + define_method(:_routes) { @_routes || routes } + } end def empty? -- cgit v1.2.3 From cd97d0b5b73d12a8a6c4d202782d8542a3738e3b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 11:59:39 -0800 Subject: we know the classes will be a list, so *tell* it to respond to each rather than casting --- actionpack/lib/action_dispatch/routing/route_set.rb | 6 +++--- 1 file changed, 3 insertions(+), 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 22d4ccf623..ee61d421b0 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -124,7 +124,7 @@ module ActionDispatch end def install(destinations = [ActionController::Base, ActionView::Base]) - Array(destinations).each do |dest| + destinations.each do |dest| dest.__send__(:include, @module) end end @@ -278,7 +278,7 @@ module ActionDispatch end def install_helpers(destinations = [ActionController::Base, ActionView::Base]) - Array(destinations).each { |d| d.module_eval { include Helpers } } + destinations.each { |d| d.module_eval { include Helpers } } named_routes.install(destinations) end @@ -324,7 +324,7 @@ module ActionDispatch # we can include? # Yes plz - JP included do - routes.install_helpers(self) + routes.install_helpers([self]) singleton_class.send(:redefine_method, :_routes) { routes } end -- cgit v1.2.3 From 87dd62ab2e4a426011392f41090006428fd01e3c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 14:37:52 -0800 Subject: stop using __send__ and just module eval in the extensions --- actionpack/lib/action_dispatch/routing/route_set.rb | 5 ++--- 1 file changed, 2 insertions(+), 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 ee61d421b0..c60e26983d 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -124,9 +124,8 @@ module ActionDispatch end def install(destinations = [ActionController::Base, ActionView::Base]) - destinations.each do |dest| - dest.__send__(:include, @module) - end + helper = @module + destinations.each { |d| d.module_eval { include helper } } end private -- cgit v1.2.3 From e43b2b35c7042c87fd18e3ecd55c14eabd5746ba Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 16:07:07 -0800 Subject: just add the writer rather than adding both and removing one --- actionpack/lib/action_dispatch/routing/url_for.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing') diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 22e41c9c16..6c2a98ab15 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -90,8 +90,7 @@ module ActionDispatch if respond_to?(:class_attribute) class_attribute :default_url_options else - mattr_accessor :default_url_options - remove_method :default_url_options + mattr_writer :default_url_options end self.default_url_options = {} -- cgit v1.2.3 From 5681f79f642c57397bf18d239a9aa1dbf71b0f24 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Dec 2011 17:27:10 -0800 Subject: be explicit about where helpers are installed --- 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 c60e26983d..10b3e212e6 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -276,7 +276,7 @@ module ActionDispatch @prepend.each { |blk| eval_block(blk) } end - def install_helpers(destinations = [ActionController::Base, ActionView::Base]) + def install_helpers(destinations) destinations.each { |d| d.module_eval { include Helpers } } named_routes.install(destinations) end -- cgit v1.2.3