Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote branch 'rails/master'

  • Loading branch information...
commit c63cf7bf0db708fe46a929cf57649ab5a92034af 2 parents 52c56f9 + b07e6fd
@fxn fxn authored
Showing with 5,690 additions and 970 deletions.
  1. +1 −1  Gemfile
  2. +9 −1 actionpack/CHANGELOG
  3. +2 −2 actionpack/actionpack.gemspec
  4. +2 −2 actionpack/lib/action_controller/metal/helpers.rb
  5. +1 −0  actionpack/lib/action_controller/vendor/html-scanner/html/node.rb
  6. +1 −0  actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb
  7. +2 −2 actionpack/lib/action_dispatch/http/cache.rb
  8. +30 −1 actionpack/lib/action_dispatch/http/parameters.rb
  9. +2 −0  actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
  10. +96 −109 actionpack/lib/action_dispatch/routing/mapper.rb
  11. +11 −2 actionpack/lib/action_dispatch/routing/route.rb
  12. +9 −3 actionpack/lib/action_dispatch/routing/route_set.rb
  13. +32 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount.rb
  14. +60 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/frequency.rb
  15. +74 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
  16. +159 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/splitting.rb
  17. +113 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/code_generation.rb
  18. +210 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/generatable_regexp.rb
  19. +53 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/multimap.rb
  20. +36 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/prefix.rb
  21. +69 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
  22. +130 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/route.rb
  23. +409 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/route_set.rb
  24. +68 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp.rb
  25. +160 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp/parser.rb
  26. +34 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp/parser.y
  27. +83 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp/tokenizer.rb
  28. +12 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp/tokenizer.rex
  29. +148 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/utils.rb
  30. +569 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/multimap/multimap.rb
  31. +185 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/multimap/multiset.rb
  32. +158 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/multimap/nested_multimap.rb
  33. +45 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin.rb
  34. +40 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/alternation.rb
  35. +4 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/anchor.rb
  36. +59 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/atom.rb
  37. +56 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/character.rb
  38. +55 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/character_class.rb
  39. +83 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/collection.rb
  40. +126 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/expression.rb
  41. +90 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/group.rb
  42. +55 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/options.rb
  43. +415 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/parser.rb
  44. +213 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/tokenizer.rb
  45. +3 −0  actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/vendor/regin/regin/version.rb
  46. +5 −0 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/version.rb
  47. +1 −3 actionpack/lib/action_view/helpers/capture_helper.rb
  48. +14 −3 actionpack/lib/action_view/helpers/form_tag_helper.rb
  49. +5 −6 actionpack/lib/action_view/helpers/prototype_helper.rb
  50. +1 −1  actionpack/lib/action_view/helpers/tag_helper.rb
  51. +21 −29 actionpack/lib/action_view/helpers/url_helper.rb
  52. +3 −2 actionpack/lib/action_view/log_subscriber.rb
  53. +11 −8 actionpack/lib/action_view/template.rb
  54. +1 −1  actionpack/lib/action_view/test_case.rb
  55. +11 −0 actionpack/test/abstract_unit.rb
  56. +15 −15 actionpack/test/activerecord/polymorphic_routes_test.rb
  57. +0 −3  actionpack/test/controller/base_test.rb
  58. +14 −8 actionpack/test/dispatch/request/multipart_params_parsing_test.rb
  59. +23 −0 actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb
  60. +82 −0 actionpack/test/dispatch/routing_test.rb
  61. +1 −1  actionpack/test/template/erb/form_for_test.rb
  62. +2 −2 actionpack/test/template/erb/tag_helper_test.rb
  63. +204 −181 actionpack/test/template/form_helper_test.rb
  64. +46 −10 actionpack/test/template/form_tag_helper_test.rb
  65. +4 −0 actionpack/test/template/html-scanner/sanitizer_test.rb
  66. +2 −2 actionpack/test/template/tag_helper_test.rb
  67. +8 −25 actionpack/test/template/url_helper_test.rb
  68. +2 −0  activemodel/test/cases/validations/numericality_validation_test.rb
  69. +7 −1 activerecord/CHANGELOG
  70. +3 −1 activerecord/Rakefile
  71. +11 −0 activerecord/lib/active_record/associations/association_collection.rb
  72. +5 −1 activerecord/lib/active_record/associations/has_many_association.rb
  73. +57 −18 activerecord/lib/active_record/base.rb
  74. +1 −1  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
  75. +1 −0  activerecord/lib/active_record/migration.rb
  76. +22 −11 activerecord/lib/active_record/named_scope.rb
  77. +1 −1  activerecord/lib/active_record/persistence.rb
  78. +37 −7 activerecord/lib/active_record/railties/databases.rake
  79. +24 −21 activerecord/lib/active_record/relation.rb
  80. +1 −3 activerecord/lib/active_record/relation/predicate_builder.rb
  81. +4 −10 activerecord/lib/active_record/relation/query_methods.rb
  82. +1 −1  activerecord/lib/active_record/relation/spawn_methods.rb
  83. 0  activerecord/test/cases/adapters/{sqlite → sqlite3}/copy_table_test.rb
  84. 0  activerecord/test/cases/adapters/{sqlite → sqlite3}/sqlite3_adapter_test.rb
  85. +62 −0 activerecord/test/cases/associations/has_many_associations_test.rb
  86. +0 −19 activerecord/test/cases/base_test.rb
  87. +1 −1  activerecord/test/cases/inheritance_test.rb
  88. +11 −202 activerecord/test/cases/method_scoping_test.rb
  89. +17 −0 activerecord/test/cases/migration_test.rb
  90. +8 −2 activerecord/test/cases/named_scope_test.rb
  91. +396 −0 activerecord/test/cases/relation_scoping_test.rb
  92. +1 −1  activerecord/test/cases/relations_test.rb
  93. +7 −1 activerecord/test/models/developer.rb
  94. +3 −0  activerecord/test/models/without_table.rb
  95. +6 −0 activesupport/CHANGELOG
  96. +2 −3 activesupport/lib/active_support/core_ext/string/conversions.rb
  97. +12 −9 activesupport/lib/active_support/deprecation/behaviors.rb
  98. +5 −4 activesupport/lib/active_support/multibyte/chars.rb
  99. +1 −1  activesupport/lib/active_support/multibyte/unicode.rb
  100. +31 −1 activesupport/lib/active_support/railtie.rb
  101. +18 −36 activesupport/lib/active_support/testing/performance.rb
  102. +5 −1 activesupport/lib/active_support/values/time_zone.rb
  103. BIN  activesupport/lib/active_support/values/unicode_tables.dat
  104. +1 −3 activesupport/test/core_ext/time_with_zone_test.rb
  105. +9 −6 activesupport/test/multibyte_chars_test.rb
  106. +6 −1 activesupport/test/time_zone_test.rb
  107. +4 −0 railties/CHANGELOG
  108. +1 −6 railties/lib/rails/application.rb
  109. +2 −2 railties/lib/rails/application/configuration.rb
  110. +10 −11 railties/lib/rails/engine.rb
  111. +2 −2 railties/lib/rails/engine/configuration.rb
  112. +1 −1  railties/lib/rails/generators/actions.rb
  113. +3 −0  railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
  114. +3 −0  railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
  115. +3 −0  railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
  116. +1 −1  railties/lib/rails/generators/rails/app/templates/config/routes.rb
  117. +114 −57 railties/lib/rails/generators/rails/app/templates/public/javascripts/rails.js
  118. +31 −20 railties/lib/rails/paths.rb
  119. +10 −6 railties/lib/rails/plugin.rb
  120. +14 −1 railties/test/application/loading_test.rb
  121. +1 −0  railties/test/application/url_generation_test.rb
  122. +2 −1  railties/test/isolation/abstract_unit.rb
  123. +28 −28 railties/test/paths_test.rb
  124. +0 −4 railties/test/railties/engine_test.rb
  125. +26 −4 railties/test/railties/plugin_test.rb
  126. +9 −36 railties/test/railties/shared_tests.rb
View
2  Gemfile
@@ -1,7 +1,7 @@
source 'http://rubygems.org'
gem "arel", :git => "git://github.com/rails/arel.git"
-gem "rack-mount", :git => "git://github.com/rails/rack-mount.git"
+#gem "rack-mount", :git => "git://github.com/rails/rack-mount.git"
gem "rails", :path => File.dirname(__FILE__)
gem "rake", ">= 0.8.7"
View
10 actionpack/CHANGELOG
@@ -1,5 +1,14 @@
*Rails 3.0.0 [Release Candidate] (unreleased)*
+* link_to, button_to, and tag/tag_options now rely on html_escape instead of escape_once. [fxn]
+
+* url_for returns always unescaped strings, and the :escape option is gone. [fxn]
+
+* Added accept-charset parameter and _snowman hidden field to force the contents
+ of Rails POSTed forms to be in UTF-8 [Yehuda Katz]
+
+* Upgrade to Rack 1.2.1 [Jeremy Kemper]
+
* Allow :path to be given to match/get/post/put/delete instead of :path_names in the new router [Carlos Antônio da Silva]
* Added resources_path_names to the new router DSL [José Valim]
@@ -22,7 +31,6 @@
* Removed textilize, textilize_without_paragraph and markdown helpers. [Santiago Pastorino]
-
*Rails 3.0.0 [beta 4] (June 8th, 2010)*
* Remove middleware laziness [José Valim]
View
4 actionpack/actionpack.gemspec
@@ -23,9 +23,9 @@ Gem::Specification.new do |s|
s.add_dependency('activemodel', version)
s.add_dependency('builder', '~> 2.1.2')
s.add_dependency('i18n', '~> 0.4.1')
- s.add_dependency('rack', '~> 1.1.0')
+ s.add_dependency('rack', '~> 1.2.1')
s.add_dependency('rack-test', '~> 0.5.4')
- s.add_dependency('rack-mount', '~> 0.6.5')
+ #s.add_dependency('rack-mount', '~> 0.6.6')
s.add_dependency('tzinfo', '~> 0.3.16')
s.add_dependency('erubis', '~> 2.6.5')
end
View
4 actionpack/lib/action_controller/metal/helpers.rb
@@ -58,12 +58,12 @@ module Helpers
module ClassMethods
def helpers_dir
- ActiveSupport::Deprecation.warn "helpers_dir is deprecated, use helpers_path instead"
+ ActiveSupport::Deprecation.warn "helpers_dir is deprecated, use helpers_path instead", caller
self.helpers_path
end
def helpers_dir=(value)
- ActiveSupport::Deprecation.warn "helpers_dir= is deprecated, use helpers_path= instead"
+ ActiveSupport::Deprecation.warn "helpers_dir= is deprecated, use helpers_path= instead", caller
self.helpers_path = Array(value)
end
View
1  actionpack/lib/action_controller/vendor/html-scanner/html/node.rb
@@ -177,6 +177,7 @@ def parse(parent, line, pos, content, strict=true)
case text
when "\\" then
value << text
+ break if scanner.eos?
value << scanner.getch
when delim
break
View
1  actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb
@@ -96,6 +96,7 @@ def consume_quoted_regions
while match = @scanner.scan_until(/[\\#{delim}]/)
text << match
break if @scanner.matched == delim
+ break if @scanner.eos?
text << @scanner.getch # skip the escaped character
end
end
View
4 actionpack/lib/action_dispatch/http/cache.rb
@@ -89,7 +89,7 @@ def handle_conditional_get!
if etag? || last_modified? || !@cache_control.empty?
set_conditional_cache_control!
elsif nonempty_ok_response?
- self.etag = @body
+ self.etag = body
if request && request.etag_matches?(etag)
self.status = 304
@@ -137,4 +137,4 @@ def set_conditional_cache_control!
end
end
end
-end
+end
View
31 actionpack/lib/action_dispatch/http/parameters.rb
@@ -6,7 +6,11 @@ module Http
module Parameters
# Returns both GET and POST \parameters in a single hash.
def parameters
- @env["action_dispatch.request.parameters"] ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
+ @env["action_dispatch.request.parameters"] ||= begin
+ params = request_parameters.merge(query_parameters)
+ params.merge!(path_parameters)
+ encode_params(params).with_indifferent_access
+ end
end
alias :params :parameters
@@ -32,6 +36,31 @@ def path_parameters
end
private
+
+ # TODO: Validate that the characters are UTF-8. If they aren't,
+ # you'll get a weird error down the road, but our form handling
+ # should really prevent that from happening
+ def encode_params(params)
+ return params unless "ruby".encoding_aware?
+
+ if params.is_a?(String)
+ return params.force_encoding("UTF-8").encode!
+ elsif !params.is_a?(Hash)
+ return params
+ end
+
+ params.each do |k, v|
+ case v
+ when Hash
+ encode_params(v)
+ when Array
+ v.map! {|el| encode_params(el) }
+ else
+ encode_params(v)
+ end
+ end
+ end
+
# Convert nested Hash to HashWithIndifferentAccess
def normalize_parameters(value)
case value
View
2  actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
@@ -30,6 +30,8 @@ def in_memory_controller_namespaces
class DeprecatedMapper #:nodoc:
def initialize(set) #:nodoc:
+ ActiveSupport::Deprecation.warn "You are using the old router DSL which will be removed in Rails 3.1. " <<
+ "Please check how to update your router file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/"
@set = set
end
View
205 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -131,6 +131,7 @@ def default_controller_and_action
end
defaults[:controller] ||= default_controller
+ defaults[:action] ||= default_action
defaults.delete(:controller) if defaults[:controller].blank?
defaults.delete(:action) if defaults[:action].blank?
@@ -187,6 +188,12 @@ def default_controller
@scope[:controller].to_s
end
end
+
+ def default_action
+ if @options[:action]
+ @options[:action].to_s
+ end
+ end
end
# Invokes Rack::Mount::Utils.normalize path and ensure that
@@ -433,10 +440,14 @@ def merge_shallow_scope(parent, child)
end
module Resources
+ # CANONICAL_ACTIONS holds all actions that does not need a prefix or
+ # a path appended since they fit properly in their scope level.
+ VALID_ON_OPTIONS = [:new, :collection, :member]
+ CANONICAL_ACTIONS = [:index, :create, :new, :show, :update, :destroy]
+ MERGE_FROM_SCOPE_OPTIONS = [:shallow, :constraints]
+
class Resource #:nodoc:
- def self.default_actions
- [:index, :create, :new, :show, :update, :destroy, :edit]
- end
+ DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
attr_reader :controller, :path, :options
@@ -449,7 +460,7 @@ def initialize(entities, options = {})
end
def default_actions
- self.class.default_actions
+ self.class::DEFAULT_ACTIONS
end
def actions
@@ -533,8 +544,8 @@ def member_scope
["#{path}/:id", options]
end
- def new_scope
- [path]
+ def new_scope(new_path)
+ ["#{path}/#{new_path}"]
end
def nested_scope
@@ -543,9 +554,7 @@ def nested_scope
end
class SingletonResource < Resource #:nodoc:
- def self.default_actions
- [:show, :create, :update, :destroy, :new, :edit]
- end
+ DEFAULT_ACTIONS = [:show, :create, :update, :destroy, :new, :edit]
def initialize(entities, options)
@name = entities.to_s
@@ -591,8 +600,6 @@ def resources_path_names(options)
def resource(*resources, &block)
options = resources.extract_options!
- options = (@scope[:options] || {}).merge(options)
- options[:shallow] = true if @scope[:shallow] && !options.has_key?(:shallow)
if apply_common_behavior_for(:resource, resources, options, &block)
return self
@@ -602,9 +609,12 @@ def resource(*resources, &block)
yield if block_given?
collection_scope do
- post :create if parent_resource.actions.include?(:create)
- get :new if parent_resource.actions.include?(:new)
- end
+ post :create
+ end if parent_resource.actions.include?(:create)
+
+ new_scope do
+ get :new
+ end if parent_resource.actions.include?(:new)
member_scope do
get :show if parent_resource.actions.include?(:show)
@@ -619,8 +629,6 @@ def resource(*resources, &block)
def resources(*resources, &block)
options = resources.extract_options!
- options = (@scope[:options] || {}).merge(options)
- options[:shallow] = true if @scope[:shallow] && !options.has_key?(:shallow)
if apply_common_behavior_for(:resources, resources, options, &block)
return self
@@ -632,9 +640,12 @@ def resources(*resources, &block)
collection_scope do
get :index if parent_resource.actions.include?(:index)
post :create if parent_resource.actions.include?(:create)
- get :new if parent_resource.actions.include?(:new)
end
+ new_scope do
+ get :new
+ end if parent_resource.actions.include?(:new)
+
member_scope do
get :show if parent_resource.actions.include?(:show)
put :update if parent_resource.actions.include?(:update)
@@ -671,12 +682,8 @@ def new
raise ArgumentError, "can't use new outside resource(s) scope"
end
- with_scope_level(:new) do
- scope(*parent_resource.new_scope) do
- scope(action_path(:new)) do
- yield
- end
- end
+ new_scope do
+ yield
end
end
@@ -717,8 +724,7 @@ def shallow
end
def match(*args)
- options = args.extract_options!
-
+ options = args.extract_options!.dup
options[:anchor] = true unless options.key?(:anchor)
if args.length > 1
@@ -726,17 +732,12 @@ def match(*args)
return self
end
- if [:collection, :member, :new].include?(options[:on])
+ on = options.delete(:on)
+ if VALID_ON_OPTIONS.include?(on)
args.push(options)
-
- case options.delete(:on)
- when :collection
- return collection { match(*args) }
- when :member
- return member { match(*args) }
- when :new
- return new { match(*args) }
- end
+ return send(on){ match(*args) }
+ elsif on
+ raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
end
if @scope[:scope_level] == :resource
@@ -745,10 +746,12 @@ def match(*args)
end
path = options.delete(:path)
+ action = args.first
- if args.first.is_a?(Symbol)
- path = path_for_action(args.first, path)
- options = options_for_action(args.first, options)
+ if action.is_a?(Symbol)
+ path = path_for_action(action, path)
+ options[:to] ||= action
+ options[:as] = name_for_action(action, options[:as])
with_exclusive_scope do
return super(path, options)
@@ -786,11 +789,11 @@ def root(options={})
end
protected
+
def parent_resource #:nodoc:
@scope[:scope_level_resource]
end
- private
def apply_common_behavior_for(method, resources, options, &block)
if resources.length > 1
resources.each { |r| send(method, r, options, &block) }
@@ -804,6 +807,10 @@ def apply_common_behavior_for(method, resources, options, &block)
return true
end
+ scope_options = @scope.slice(*MERGE_FROM_SCOPE_OPTIONS).delete_if{ |k,v| v.blank? }
+ options.reverse_merge!(scope_options) unless scope_options.empty?
+ options.reverse_merge!(@scope[:options]) unless @scope[:options].blank?
+
if resource_scope?
nested do
send(method, resources.pop, options, &block)
@@ -852,6 +859,14 @@ def resource_scope(resource)
end
end
+ def new_scope
+ with_scope_level(:new) do
+ scope(*parent_resource.new_scope(action_path(:new))) do
+ yield
+ end
+ end
+ end
+
def collection_scope
with_scope_level(:collection) do
scope(*parent_resource.collection_scope) do
@@ -868,48 +883,30 @@ def member_scope
end
end
+ def canonical_action?(action, flag)
+ flag && CANONICAL_ACTIONS.include?(action)
+ end
+
+ def shallow_scoping?
+ parent_resource && parent_resource.shallow? && @scope[:scope_level] == :member
+ end
+
def path_for_action(action, path)
- case action
- when :index, :create
- "#{@scope[:path]}(.:format)"
- when :show, :update, :destroy
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id(.:format)"
- else
- "#{@scope[:path]}(.:format)"
- end
- when :new
- "#{@scope[:path]}/#{action_path(:new)}(.:format)"
- when :edit
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(:edit)}(.:format)"
- else
- "#{@scope[:path]}/#{action_path(:edit)}(.:format)"
- end
+ prefix = shallow_scoping? ?
+ "#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]
+
+ if canonical_action?(action, path.blank?)
+ "#{prefix}(.:format)"
else
- case @scope[:scope_level]
- when :collection, :new
- "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
- else
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action, path)}(.:format)"
- else
- "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
- end
- end
+ "#{prefix}/#{action_path(action, path)}(.:format)"
end
end
def path_for_custom_action
- case @scope[:scope_level]
- when :collection, :new
- @scope[:path]
+ if shallow_scoping?
+ "#{@scope[:shallow_path]}/#{parent_resource.path}/:id"
else
- if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id"
- else
- @scope[:path]
- end
+ @scope[:path]
end
end
@@ -917,50 +914,40 @@ def action_path(name, path = nil)
path || @scope[:path_names][name.to_sym] || name.to_s
end
- def options_for_action(action, options)
- options.reverse_merge(
- :to => action,
- :as => name_for_action(action)
- )
+ def prefix_name_for_action(action, as)
+ if as.present?
+ "#{as}_"
+ elsif as
+ ""
+ elsif !canonical_action?(action, @scope[:scope_level])
+ "#{action}_"
+ end
end
- def name_for_action(action)
- name_prefix = @scope[:as].blank? ? "" : "#{@scope[:as]}_"
- shallow_prefix = @scope[:shallow_prefix].blank? ? "" : "#{@scope[:shallow_prefix]}_"
+ def name_for_action(action, as=nil)
+ prefix = prefix_name_for_action(action, as)
+ name_prefix = @scope[:as]
- case action
- when :index, :create
- "#{name_prefix}#{parent_resource.collection_name}"
- when :show, :update, :destroy
- if parent_resource.shallow?
- "#{shallow_prefix}#{parent_resource.member_name}"
- else
- "#{name_prefix}#{parent_resource.member_name}"
- end
- when :edit
- if parent_resource.shallow?
- "edit_#{shallow_prefix}#{parent_resource.member_name}"
- else
- "edit_#{name_prefix}#{parent_resource.member_name}"
- end
+ if parent_resource
+ collection_name = parent_resource.collection_name
+ member_name = parent_resource.member_name
+ name_prefix = "#{name_prefix}_" if name_prefix.present?
+ end
+
+ case @scope[:scope_level]
+ when :collection
+ "#{prefix}#{name_prefix}#{collection_name}"
when :new
- "new_#{name_prefix}#{parent_resource.member_name}"
+ "#{prefix}new_#{name_prefix}#{member_name}"
else
- case @scope[:scope_level]
- when :collection
- "#{action}_#{name_prefix}#{parent_resource.collection_name}"
- when :new
- "#{action}_new_#{name_prefix}#{parent_resource.member_name}"
+ if shallow_scoping?
+ shallow_prefix = "#{@scope[:shallow_prefix]}_" if @scope[:shallow_prefix].present?
+ "#{prefix}#{shallow_prefix}#{member_name}"
else
- if parent_resource.shallow?
- "#{action}_#{shallow_prefix}#{parent_resource.member_name}"
- else
- "#{action}_#{name_prefix}#{parent_resource.member_name}"
- end
+ "#{prefix}#{name_prefix}#{member_name}"
end
end
end
-
end
include Base
View
13 actionpack/lib/action_dispatch/routing/route.rb
@@ -2,9 +2,10 @@ module ActionDispatch
module Routing
class Route #:nodoc:
attr_reader :app, :conditions, :defaults, :name
- attr_reader :path, :requirements
+ attr_reader :path, :requirements, :set
- def initialize(app, conditions, requirements, defaults, name, anchor)
+ def initialize(set, app, conditions, requirements, defaults, name, anchor)
+ @set = set
@app = app
@defaults = defaults
@name = name
@@ -24,6 +25,9 @@ def initialize(app, conditions, requirements, defaults, name, anchor)
h[k] = Rack::Mount::RegexpWithNamedGroups.new(v)
h
}
+
+ @conditions.delete_if{ |k,v| k != :path_info && !valid_condition?(k) }
+ @requirements.delete_if{ |k,v| !valid_condition?(k) }
end
def verb
@@ -50,6 +54,11 @@ def to_s
"%-6s %-40s %s" % [(verb || :any).to_s.upcase, path, requirements.inspect]
end
end
+
+ private
+ def valid_condition?(method)
+ segment_keys.include?(method) || set.valid_conditions.include?(method)
+ end
end
end
end
View
12 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -1,8 +1,10 @@
-require 'rack/mount'
require 'forwardable'
require 'active_support/core_ext/object/to_query'
require 'action_dispatch/routing/deprecated_mapper'
+$: << File.expand_path('../../vendor/rack-mount-0.6.6.pre', __FILE__)
+require 'rack/mount'
+
module ActionDispatch
module Routing
class RouteSet #:nodoc:
@@ -187,7 +189,7 @@ def #{selector}(*args)
attr_accessor :set, :routes, :named_routes
attr_accessor :disable_clear_and_finalize, :resources_path_names
- attr_accessor :default_url_options, :request_class
+ attr_accessor :default_url_options, :request_class, :valid_conditions
def self.default_resources_path_names
{ :new => 'new', :edit => 'edit' }
@@ -199,7 +201,11 @@ def initialize(request_class = ActionDispatch::Request)
self.resources_path_names = self.class.default_resources_path_names.dup
self.controller_namespaces = Set.new
self.default_url_options = {}
+
self.request_class = request_class
+ self.valid_conditions = request_class.public_instance_methods.map { |m| m.to_sym }
+ self.valid_conditions.delete(:id)
+ self.valid_conditions.push(:controller, :action)
@disable_clear_and_finalize = false
clear!
@@ -277,7 +283,7 @@ def empty?
end
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
- route = Route.new(app, conditions, requirements, defaults, name, anchor)
+ route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
@set.add_route(*route)
named_routes[name] = route if name
routes << route
View
32 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount.rb
@@ -0,0 +1,32 @@
+require 'rack'
+
+module Rack #:nodoc:
+ # A stackable dynamic tree based Rack router.
+ #
+ # Rack::Mount supports Rack's Cascade style of trying several routes until
+ # it finds one that is not a 404. This allows multiple routes to be nested
+ # or stacked on top of each other. Since the application endpoint can
+ # trigger the router to continue matching, middleware can be used to add
+ # arbitrary conditions to any route. This allows you to route based on
+ # other request attributes, session information, or even data dynamically
+ # pulled from a database.
+ module Mount
+ autoload :CodeGeneration, 'rack/mount/code_generation'
+ autoload :GeneratableRegexp, 'rack/mount/generatable_regexp'
+ autoload :Multimap, 'rack/mount/multimap'
+ autoload :Prefix, 'rack/mount/prefix'
+ autoload :RegexpWithNamedGroups, 'rack/mount/regexp_with_named_groups'
+ autoload :Route, 'rack/mount/route'
+ autoload :RouteSet, 'rack/mount/route_set'
+ autoload :RoutingError, 'rack/mount/route_set'
+ autoload :Strexp, 'rack/mount/strexp'
+ autoload :Utils, 'rack/mount/utils'
+ autoload :Version, 'rack/mount/version'
+
+ module Analysis #:nodoc:
+ autoload :Frequency, 'rack/mount/analysis/frequency'
+ autoload :Histogram, 'rack/mount/analysis/histogram'
+ autoload :Splitting, 'rack/mount/analysis/splitting'
+ end
+ end
+end
View
60 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/frequency.rb
@@ -0,0 +1,60 @@
+require 'rack/mount/utils'
+
+module Rack::Mount
+ module Analysis
+ class Frequency #:nodoc:
+ def initialize(*keys)
+ clear
+ keys.each { |key| self << key }
+ end
+
+ def clear
+ @raw_keys = []
+ @key_frequency = Analysis::Histogram.new
+ self
+ end
+
+ def <<(key)
+ raise ArgumentError unless key.is_a?(Hash)
+ @raw_keys << key
+ nil
+ end
+
+ def possible_keys
+ @possible_keys ||= begin
+ @raw_keys.map do |key|
+ key.inject({}) { |requirements, (method, requirement)|
+ process_key(requirements, method, requirement)
+ requirements
+ }
+ end
+ end
+ end
+
+ def process_key(requirements, method, requirement)
+ if requirement.is_a?(Regexp)
+ expression = Utils.parse_regexp(requirement)
+
+ if expression.is_a?(Regin::Expression) && expression.anchored_to_line?
+ expression = Regin::Expression.new(expression.reject { |e| e.is_a?(Regin::Anchor) })
+ return requirements[method] = expression.to_s if expression.literal?
+ end
+ end
+
+ requirements[method] = requirement
+ end
+
+ def report
+ @report ||= begin
+ possible_keys.each { |keys| keys.each_pair { |key, _| @key_frequency << key } }
+ return [] if @key_frequency.count <= 1
+ @key_frequency.keys_in_upper_quartile
+ end
+ end
+
+ def expire!
+ @possible_keys = @report = nil
+ end
+ end
+ end
+end
View
74 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/histogram.rb
@@ -0,0 +1,74 @@
+module Rack::Mount
+ module Analysis
+ class Histogram < Hash #:nodoc:
+ attr_reader :count
+
+ def initialize
+ @count = 0
+ super(0)
+ expire_caches!
+ end
+
+ def <<(value)
+ @count += 1
+ self[value] += 1 if value
+ expire_caches!
+ self
+ end
+
+ def sorted_by_frequency
+ sort_by { |_, value| value }.reverse!
+ end
+
+ def max
+ @max ||= values.max || 0
+ end
+
+ def min
+ @min ||= values.min || 0
+ end
+
+ def mean
+ @mean ||= calculate_mean
+ end
+
+ def standard_deviation
+ @standard_deviation ||= calculate_standard_deviation
+ end
+
+ def upper_quartile_limit
+ @upper_quartile_limit ||= calculate_upper_quartile_limit
+ end
+
+ def keys_in_upper_quartile
+ @keys_in_upper_quartile ||= compute_keys_in_upper_quartile
+ end
+
+ private
+ def calculate_mean
+ count / size
+ end
+
+ def calculate_variance
+ values.inject(0) { |sum, e| sum + (e - mean) ** 2 } / count.to_f
+ end
+
+ def calculate_standard_deviation
+ Math.sqrt(calculate_variance)
+ end
+
+ def calculate_upper_quartile_limit
+ mean + standard_deviation
+ end
+
+ def compute_keys_in_upper_quartile
+ sorted_by_frequency.select { |_, value| value >= upper_quartile_limit }.map! { |key, _| key }
+ end
+
+ def expire_caches!
+ @max = @min = @mean = @standard_deviation = nil
+ @keys_in_upper_quartile = nil
+ end
+ end
+ end
+end
View
159 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/analysis/splitting.rb
@@ -0,0 +1,159 @@
+require 'rack/mount/utils'
+
+module Rack::Mount
+ module Analysis
+ class Splitting < Frequency
+ NULL = "\0".freeze
+
+ class Key < Struct.new(:method, :index, :separators)
+ def self.split(value, separator_pattern)
+ keys = value.split(separator_pattern)
+ keys.shift if keys[0] == ''
+ keys << NULL
+ keys
+ end
+
+ def call(cache, obj)
+ (cache[method] ||= self.class.split(obj.send(method), separators))[index]
+ end
+
+ def call_source(cache, obj)
+ "(#{cache}[:#{method}] ||= Analysis::Splitting::Key.split(#{obj}.#{method}, #{separators.inspect}))[#{index}]"
+ end
+
+ def inspect
+ "#{method}[#{index}]"
+ end
+ end
+
+ def clear
+ @boundaries = {}
+ super
+ end
+
+ def <<(key)
+ super
+ key.each_pair do |k, v|
+ analyze_capture_boundaries(v, @boundaries[k] ||= Histogram.new)
+ end
+ end
+
+ def separators(key)
+ (@boundaries[key].keys_in_upper_quartile + ['/']).uniq
+ end
+
+ def process_key(requirements, method, requirement)
+ separators = separators(method)
+ if requirement.is_a?(Regexp) && separators.any?
+ generate_split_keys(requirement, separators).each_with_index do |value, index|
+ requirements[Key.new(method, index, Regexp.union(*separators))] = value
+ end
+ else
+ super
+ end
+ end
+
+ private
+ def analyze_capture_boundaries(regexp, boundaries) #:nodoc:
+ return boundaries unless regexp.is_a?(Regexp)
+
+ parts = Utils.parse_regexp(regexp)
+ parts.each_with_index do |part, index|
+ if part.is_a?(Regin::Group)
+ if index > 0
+ previous = parts[index-1]
+ if previous.is_a?(Regin::Character) && previous.literal?
+ boundaries << previous.to_s
+ end
+ end
+
+ if inside = part.expression[0]
+ if inside.is_a?(Regin::Character) && inside.literal?
+ boundaries << inside.to_s
+ end
+ end
+
+ if index < parts.length
+ following = parts[index+1]
+ if following.is_a?(Regin::Character) && following.literal?
+ boundaries << following.to_s
+ end
+ end
+ end
+ end
+
+ boundaries
+ end
+
+ def generate_split_keys(regexp, separators) #:nodoc:
+ segments = []
+ buf = nil
+ parts = Utils.parse_regexp(regexp)
+ parts.each_with_index do |part, index|
+ case part
+ when Regin::Anchor
+ if part.value == '$' || part.value == '\Z'
+ segments << join_buffer(buf, regexp) if buf
+ segments << NULL
+ buf = nil
+ break
+ end
+ when Regin::CharacterClass
+ break if separators.any? { |s| part.include?(s) }
+ buf = nil
+ segments << part.to_regexp(true)
+ when Regin::Character
+ if separators.any? { |s| part.include?(s) }
+ segments << join_buffer(buf, regexp) if buf
+ peek = parts[index+1]
+ if peek.is_a?(Regin::Character) && separators.include?(peek.value)
+ segments << ''
+ end
+ buf = nil
+ else
+ buf ||= Regin::Expression.new([])
+ buf += [part]
+ end
+ when Regin::Group
+ if part.quantifier == '?'
+ value = part.expression.first
+ if separators.any? { |s| value.include?(s) }
+ segments << join_buffer(buf, regexp) if buf
+ buf = nil
+ end
+ break
+ elsif part.quantifier == nil
+ break if separators.any? { |s| part.include?(s) }
+ buf = nil
+ segments << part.to_regexp(true)
+ else
+ break
+ end
+ else
+ break
+ end
+
+ if index + 1 == parts.size
+ segments << join_buffer(buf, regexp) if buf
+ buf = nil
+ break
+ end
+ end
+
+ while segments.length > 0 && (segments.last.nil? || segments.last == '')
+ segments.pop
+ end
+
+ segments
+ end
+
+ def join_buffer(parts, regexp)
+ if parts.literal?
+ parts.to_s
+ else
+ parts.to_regexp(true)
+ end
+ end
+ end
+ end
+end
View
113 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/code_generation.rb
@@ -0,0 +1,113 @@
+module Rack::Mount
+ module CodeGeneration #:nodoc:
+ def _expired_recognize(env) #:nodoc:
+ raise 'route set not finalized'
+ end
+
+ def rehash
+ super
+ optimize_recognize!
+ end
+
+ private
+ def expire!
+ if @optimized_recognize_defined
+ remove_metaclass_method :recognize
+
+ class << self
+ alias_method :recognize, :_expired_recognize
+ end
+
+ @optimized_recognize_defined = false
+ end
+
+ super
+ end
+
+ def optimize_container_iterator(container)
+ body = []
+
+ container.each_with_index { |route, i|
+ body << "route = self[#{i}]"
+ body << 'matches = {}'
+ body << 'params = route.defaults.dup'
+
+ conditions = []
+ route.conditions.each do |method, condition|
+ b = []
+ if condition.is_a?(Regexp)
+ b << "if m = obj.#{method}.match(#{condition.inspect})"
+ b << "matches[:#{method}] = m"
+ if (named_captures = route.named_captures[method]) && named_captures.any?
+ b << 'captures = m.captures'
+ b << 'p = nil'
+ b << named_captures.map { |k, j| "params[#{k.inspect}] = p if p = captures[#{j}]" }.join('; ')
+ end
+ else
+ b << "if m = obj.#{method} == route.conditions[:#{method}]"
+ end
+ b << 'true'
+ b << 'end'
+ conditions << "(#{b.join('; ')})"
+ end
+
+ body << <<-RUBY
+ if #{conditions.join(' && ')}
+ yield route, matches, params
+ end
+ RUBY
+ }
+
+ container.instance_eval(<<-RUBY, __FILE__, __LINE__)
+ def optimized_each(obj)
+ #{body.join("\n")}
+ nil
+ end
+ RUBY
+ end
+
+ def optimize_recognize!
+ keys = @recognition_keys.map { |key|
+ if key.respond_to?(:call_source)
+ key.call_source(:cache, :obj)
+ else
+ "obj.#{key}"
+ end
+ }.join(', ')
+
+ @optimized_recognize_defined = true
+
+ remove_metaclass_method :recognize
+
+ instance_eval(<<-RUBY, __FILE__, __LINE__)
+ def recognize(obj)
+ cache = {}
+ container = @recognition_graph[#{keys}]
+ optimize_container_iterator(container) unless container.respond_to?(:optimized_each)
+
+ if block_given?
+ container.optimized_each(obj) do |route, matches, params|
+ yield route, matches, params
+ end
+ else
+ container.optimized_each(obj) do |route, matches, params|
+ return route, matches, params
+ end
+ end
+
+ nil
+ end
+ RUBY
+ end
+
+ # method_defined? can't distinguish between instance
+ # and meta methods. So we have to rescue if the method
+ # has not been defined in the metaclass yet.
+ def remove_metaclass_method(symbol)
+ metaclass = class << self; self; end
+ metaclass.send(:remove_method, symbol)
+ rescue NameError => e
+ nil
+ end
+ end
+end
View
210 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/generatable_regexp.rb
@@ -0,0 +1,210 @@
+require 'rack/mount/utils'
+
+module Rack::Mount
+ class GeneratableRegexp < Regexp #:nodoc:
+ class DynamicSegment #:nodoc:
+ attr_reader :name, :requirement
+
+ def initialize(name, requirement)
+ @name, @requirement = name.to_sym, requirement
+ freeze
+ end
+
+ def ==(obj)
+ @name == obj.name && @requirement == obj.requirement
+ end
+
+ def =~(str)
+ @requirement =~ str
+ end
+
+ def to_hash
+ { @name => @requirement }
+ end
+
+ def inspect
+ "/(?<#{@name}>#{@requirement.source})/"
+ end
+ end
+
+ module InstanceMethods
+ def self.extended(obj)
+ obj.segments
+ end
+
+ def defaults=(defaults)
+ @required_captures = nil
+ @required_params = nil
+ @required_defaults = nil
+ @defaults = defaults
+ end
+
+ def defaults
+ @defaults ||= {}
+ end
+
+ def generatable?
+ segments.any?
+ end
+
+ def generate(params = {}, recall = {}, options = {})
+ return nil unless generatable?
+
+ merged = recall.merge(params)
+ return nil unless required_params.all? { |p| merged.include?(p) }
+ return nil unless required_defaults.all? { |k, v| merged[k] == v }
+
+ generate_from_segments(segments, params, merged, options)
+ end
+
+ def segments
+ @segments ||= begin
+ defaults
+ segments = []
+ catch(:halt) do
+ expression = Utils.parse_regexp(self)
+ segments = parse_segments(expression)
+ end
+ segments
+ end
+ end
+
+ def captures
+ segments.flatten.find_all { |s| s.is_a?(DynamicSegment) }
+ end
+
+ def required_captures
+ @required_captures ||= segments.find_all { |s|
+ s.is_a?(DynamicSegment) && !@defaults.include?(s.name)
+ }.freeze
+ end
+
+ def required_params
+ @required_params ||= required_captures.map { |s| s.name }.freeze
+ end
+
+ def required_defaults
+ @required_defaults ||= begin
+ required_defaults = @defaults.dup
+ captures.inject({}) { |h, s| h.merge!(s.to_hash) }.keys.each { |name|
+ required_defaults.delete(name)
+ }
+ required_defaults
+ end
+ end
+
+ def freeze
+ segments
+ captures
+ required_captures
+ required_params
+ required_defaults
+ super
+ end
+
+ private
+ def parse_segments(segments)
+ s = []
+ segments.each_with_index do |part, index|
+ case part
+ when Regin::Anchor
+ # ignore
+ when Regin::Character
+ throw :halt unless part.literal?
+
+ if s.last.is_a?(String)
+ s.last << part.value.dup
+ else
+ s << part.value.dup
+ end
+ when Regin::Group
+ if part.name
+ s << DynamicSegment.new(part.name, part.expression.to_regexp(true))
+ else
+ s << parse_segments(part.expression)
+ end
+ when Regin::Expression
+ return parse_segments(part)
+ else
+ throw :halt
+ end
+ end
+
+ s
+ end
+
+ EMPTY_STRING = ''.freeze
+
+ def generate_from_segments(segments, params, merged, options, optional = false)
+ if optional
+ return EMPTY_STRING if segments.all? { |s| s.is_a?(String) }
+ return EMPTY_STRING unless segments.flatten.any? { |s|
+ params.has_key?(s.name) if s.is_a?(DynamicSegment)
+ }
+ return EMPTY_STRING if segments.any? { |segment|
+ if segment.is_a?(DynamicSegment)
+ value = merged[segment.name] || @defaults[segment.name]
+ value = parameterize(segment.name, value, options)
+
+ merged_value = parameterize(segment.name, merged[segment.name], options)
+ default_value = parameterize(segment.name, @defaults[segment.name], options)
+
+ if value.nil? || segment !~ value
+ true
+ elsif merged_value == default_value
+ # Nasty control flow
+ return :clear_remaining_segments
+ else
+ false
+ end
+ end
+ }
+ end
+
+ generated = segments.map do |segment|
+ case segment
+ when String
+ segment
+ when DynamicSegment
+ value = params[segment.name] || merged[segment.name] || @defaults[segment.name]
+ value = parameterize(segment.name, value, options)
+ if value && segment =~ value.to_s
+ value
+ else
+ return
+ end
+ when Array
+ value = generate_from_segments(segment, params, merged, options, true)
+ if value == :clear_remaining_segments
+ segment.each { |s| params.delete(s.name) if s.is_a?(DynamicSegment) }
+ EMPTY_STRING
+ elsif value.nil?
+ EMPTY_STRING
+ else
+ value
+ end
+ end
+ end
+
+ # Delete any used items from the params
+ segments.each { |s| params.delete(s.name) if s.is_a?(DynamicSegment) }
+
+ generated.join
+ end
+
+ def parameterize(name, value, options)
+ if block = options[:parameterize]
+ block.call(name, value)
+ else
+ value
+ end
+ end
+ end
+ include InstanceMethods
+
+ def initialize(regexp)
+ super
+ segments
+ end
+ end
+end
View
53 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/multimap.rb
@@ -0,0 +1,53 @@
+begin
+ require 'nested_multimap'
+rescue LoadError
+ $: << File.expand_path(File.join(File.dirname(__FILE__), 'vendor/multimap'))
+ require 'nested_multimap'
+end
+
+module Rack::Mount
+ class Multimap < NestedMultimap #:nodoc:
+ def store(*args)
+ keys = args.dup
+ value = keys.pop
+ key = keys.shift
+
+ raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value
+
+ unless key.respond_to?(:=~)
+ raise ArgumentError, "unsupported key: #{args.first.inspect}"
+ end
+
+ if key.is_a?(Regexp)
+ if keys.empty?
+ @hash.each_pair { |k, l| l << value if k =~ key }
+ self.default << value
+ else
+ @hash.each_pair { |k, _|
+ if k =~ key
+ args[0] = k
+ super(*args)
+ end
+ }
+
+ self.default = self.class.new(default) unless default.is_a?(self.class)
+ default[*keys.dup] = value
+ end
+ else
+ super(*args)
+ end
+ end
+ alias_method :[]=, :store
+
+ undef :index, :invert
+
+ def height
+ containers_with_default.max { |a, b| a.length <=> b.length }.length
+ end
+
+ def average_height
+ lengths = containers_with_default.map { |e| e.length }
+ lengths.inject(0) { |sum, len| sum += len }.to_f / lengths.size
+ end
+ end
+end
View
36 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/prefix.rb
@@ -0,0 +1,36 @@
+require 'rack/mount/utils'
+
+module Rack::Mount
+ class Prefix #:nodoc:
+ EMPTY_STRING = ''.freeze
+ PATH_INFO = 'PATH_INFO'.freeze
+ SCRIPT_NAME = 'SCRIPT_NAME'.freeze
+ SLASH = '/'.freeze
+
+ KEY = 'rack.mount.prefix'.freeze
+
+ def initialize(app, prefix = nil)
+ @app, @prefix = app, prefix.freeze
+ freeze
+ end
+
+ def call(env)
+ if prefix = env[KEY] || @prefix
+ old_path_info = env[PATH_INFO].dup
+ old_script_name = env[SCRIPT_NAME].dup
+
+ begin
+ env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO].sub(prefix, EMPTY_STRING))
+ env[PATH_INFO] = EMPTY_STRING if env[PATH_INFO] == SLASH
+ env[SCRIPT_NAME] = Utils.normalize_path(env[SCRIPT_NAME].to_s + prefix)
+ @app.call(env)
+ ensure
+ env[PATH_INFO] = old_path_info
+ env[SCRIPT_NAME] = old_script_name
+ end
+ else
+ @app.call(env)
+ end
+ end
+ end
+end
View
69 ...ck/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
@@ -0,0 +1,69 @@
+module Rack::Mount
+ if Regin.regexp_supports_named_captures?
+ RegexpWithNamedGroups = Regexp
+ else
+ require 'strscan'
+
+ # A wrapper that adds shim named capture support to older
+ # versions of Ruby.
+ #
+ # Because the named capture syntax causes a parse error, an
+ # alternate syntax is used to indicate named captures.
+ #
+ # Ruby 1.9+ named capture syntax:
+ #
+ # /(?<foo>[a-z]+)/
+ #
+ # Ruby 1.8 shim syntax:
+ #
+ # /(?:<foo>[a-z]+)/
+ class RegexpWithNamedGroups < Regexp
+ def self.new(regexp) #:nodoc:
+ if regexp.is_a?(RegexpWithNamedGroups)
+ regexp
+ else
+ super
+ end
+ end
+
+ # Wraps Regexp with named capture support.
+ def initialize(regexp)
+ regexp = Regexp.compile(regexp) unless regexp.is_a?(Regexp)
+ source, options = regexp.source, regexp.options
+ @names, scanner = [], StringScanner.new(source)
+
+ while scanner.skip_until(/\(/)
+ if scanner.scan(/\?:<([^>]+)>/)
+ @names << scanner[1]
+ elsif scanner.scan(/\?(i?m?x?\-?i?m?x?)?:/)
+ # ignore noncapture
+ else
+ @names << nil
+ end
+ end
+ source.gsub!(/\?:<([^>]+)>/, '')
+
+ @names = [] unless @names.any?
+ @names.freeze
+
+ super(source, options)
+ end
+
+ def names
+ @names.dup
+ end
+
+ def named_captures
+ named_captures = {}
+ names.each_with_index { |n, i|
+ named_captures[n] = [i+1] if n
+ }
+ named_captures
+ end
+
+ def eql?(other)
+ super && @names.eql?(other.names)
+ end
+ end
+ end
+end
View
130 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/route.rb
@@ -0,0 +1,130 @@
+require 'rack/mount/generatable_regexp'
+require 'rack/mount/regexp_with_named_groups'
+require 'rack/mount/utils'
+
+module Rack::Mount
+ # Route is an internal class used to wrap a single route attributes.
+ #
+ # Plugins should not depend on any method on this class or instantiate
+ # new Route objects. Instead use the factory method, RouteSet#add_route
+ # to create new routes and add them to the set.
+ class Route
+ # Valid rack application to call if conditions are met
+ attr_reader :app
+
+ # A hash of conditions to match against. Conditions may be expressed
+ # as strings or regexps to match against.
+ attr_reader :conditions
+
+ # A hash of values that always gets merged into the parameters hash
+ attr_reader :defaults
+
+ # Symbol identifier for the route used with named route generations
+ attr_reader :name
+
+ attr_reader :named_captures
+
+ def initialize(app, conditions, defaults, name)
+ unless app.respond_to?(:call)
+ raise ArgumentError, 'app must be a valid rack application' \
+ ' and respond to call'
+ end
+ @app = app
+
+ @name = name ? name.to_sym : nil
+ @defaults = (defaults || {}).freeze
+
+ @conditions = {}
+
+ conditions.each do |method, pattern|
+ next unless method && pattern
+
+ pattern = Regexp.compile("\\A#{Regexp.escape(pattern)}\\Z") if pattern.is_a?(String)
+
+ if pattern.is_a?(Regexp)
+ pattern = Utils.normalize_extended_expression(pattern)
+ pattern = RegexpWithNamedGroups.new(pattern)
+ pattern.extend(GeneratableRegexp::InstanceMethods)
+ pattern.defaults = @defaults
+ end
+
+ @conditions[method] = pattern.freeze
+ end
+
+ @named_captures = {}
+ @conditions.map { |method, condition|
+ next unless condition.respond_to?(:named_captures)
+ @named_captures[method] = condition.named_captures.inject({}) { |named_captures, (k, v)|
+ named_captures[k.to_sym] = v.last - 1
+ named_captures
+ }.freeze
+ }
+ @named_captures.freeze
+
+ @has_significant_params = @conditions.any? { |method, condition|
+ (condition.respond_to?(:required_params) && condition.required_params.any?) ||
+ (condition.respond_to?(:required_defaults) && condition.required_defaults.any?)
+ }
+
+ if @conditions.has_key?(:path_info) &&
+ !Utils.regexp_anchored?(@conditions[:path_info])
+ @prefix = true
+ @app = Prefix.new(@app)
+ else
+ @prefix = false
+ end
+
+ @conditions.freeze
+ end
+
+ def prefix?
+ @prefix
+ end
+
+
+ def generation_keys
+ @conditions.inject({}) { |keys, (method, condition)|
+ if condition.respond_to?(:required_defaults)
+ keys.merge!(condition.required_defaults)
+ else
+ keys
+ end
+ }
+ end
+
+ def significant_params?
+ @has_significant_params
+ end
+
+ def generate(method, params = {}, recall = {}, options = {})
+ if method.nil?
+ result = @conditions.inject({}) { |h, (m, condition)|
+ if condition.respond_to?(:generate)
+ h[m] = condition.generate(params, recall, options)
+ end
+ h
+ }
+ return nil if result.values.compact.empty?
+ else
+ if condition = @conditions[method]
+ if condition.respond_to?(:generate)
+ result = condition.generate(params, recall, options)
+ end
+ end
+ end
+
+ if result
+ @defaults.each do |key, value|
+ params.delete(key) if params[key] == value
+ end
+ end
+
+ result
+ end
+
+
+ def inspect #:nodoc:
+ "#<#{self.class.name} @app=#{@app.inspect} @conditions=#{@conditions.inspect} @defaults=#{@defaults.inspect} @name=#{@name.inspect}>"
+ end
+ end
+end
View
409 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/route_set.rb
@@ -0,0 +1,409 @@
+require 'rack/mount/multimap'
+require 'rack/mount/route'
+require 'rack/mount/utils'
+
+module Rack::Mount
+ class RoutingError < StandardError; end
+
+ class RouteSet
+ # Initialize a new RouteSet without optimizations
+ def self.new_without_optimizations(options = {}, &block)
+ new(options.merge(:_optimize => false), &block)
+ end
+
+ # Basic RouteSet initializer.
+ #
+ # If a block is given, the set is yielded and finalized.
+ #
+ # See other aspects for other valid options:
+ # - <tt>Generation::RouteSet.new</tt>
+ # - <tt>Recognition::RouteSet.new</tt>
+ def initialize(options = {}, &block)
+ @parameters_key = options.delete(:parameters_key) || 'rack.routing_args'
+ @parameters_key.freeze
+
+ @named_routes = {}
+
+ @recognition_key_analyzer = Analysis::Splitting.new
+ @generation_key_analyzer = Analysis::Frequency.new
+
+ @request_class = options.delete(:request_class) || Rack::Request
+ @valid_conditions = @request_class.public_instance_methods.map! { |m| m.to_sym }
+
+ extend CodeGeneration unless options[:_optimize] == false
+ @optimized_recognize_defined = false
+
+ @routes = []
+ expire!
+
+ if block_given?
+ yield self
+ rehash
+ end
+ end
+
+ # Builder method to add a route to the set
+ #
+ # <tt>app</tt>:: A valid Rack app to call if the conditions are met.
+ # <tt>conditions</tt>:: A hash of conditions to match against.
+ # Conditions may be expressed as strings or
+ # regexps to match against.
+ # <tt>defaults</tt>:: A hash of values that always gets merged in
+ # <tt>name</tt>:: Symbol identifier for the route used with named
+ # route generations
+ def add_route(app, conditions = {}, defaults = {}, name = nil)
+ unless conditions.is_a?(Hash)
+ raise ArgumentError, 'conditions must be a Hash'
+ end
+
+ unless conditions.all? { |method, pattern|
+ @valid_conditions.include?(method)
+ }
+ raise ArgumentError, 'conditions may only include ' +
+ @valid_conditions.inspect
+ end
+
+ route = Route.new(app, conditions, defaults, name)
+ @routes << route
+
+ @recognition_key_analyzer << route.conditions
+
+ @named_routes[route.name] = route if route.name
+ @generation_key_analyzer << route.generation_keys
+
+ expire!
+ route
+ end
+
+ def recognize(obj)
+ raise 'route set not finalized' unless @recognition_graph
+
+ cache = {}
+ keys = @recognition_keys.map { |key|
+ if key.respond_to?(:call)
+ key.call(cache, obj)
+ else
+ obj.send(key)
+ end
+ }
+
+ @recognition_graph[*keys].each do |route|
+ matches = {}
+ params = route.defaults.dup
+
+ if route.conditions.all? { |method, condition|
+ value = obj.send(method)
+ if condition.is_a?(Regexp) && (m = value.match(condition))
+ matches[method] = m
+ captures = m.captures
+ route.named_captures[method].each do |k, i|
+ if v = captures[i]
+ params[k] = v
+ end
+ end
+ true
+ elsif value == condition
+ true
+ else
+ false
+ end
+ }
+ if block_given?
+ yield route, matches, params
+ else
+ return route, matches, params
+ end
+ end
+ end
+
+ nil
+ end
+
+ X_CASCADE = 'X-Cascade'.freeze
+ PASS = 'pass'.freeze
+ PATH_INFO = 'PATH_INFO'.freeze
+
+ # Rack compatible recognition and dispatching method. Routes are
+ # tried until one returns a non-catch status code. If no routes
+ # match, the catch status code is returned.
+ #
+ # This method can only be invoked after the RouteSet has been
+ # finalized.
+ def call(env)
+ raise 'route set not finalized' unless @recognition_graph
+
+ env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO])
+
+ request = nil
+ req = @request_class.new(env)
+ recognize(req) do |route, matches, params|
+ # TODO: We only want to unescape params from uri related methods
+ params.each { |k, v| params[k] = Utils.unescape_uri(v) if v.is_a?(String) }
+
+ if route.prefix?
+ env[Prefix::KEY] = matches[:path_info].to_s
+ end
+
+ env[@parameters_key] = params
+ result = route.app.call(env)
+ return result unless result[1][X_CASCADE] == PASS
+ end
+
+ request || [404, {'Content-Type' => 'text/html', 'X-Cascade' => 'pass'}, ['Not Found']]
+ end
+
+ # Generates a url from Rack env and identifiers or significant keys.
+ #
+ # To generate a url by named route, pass the name in as a +Symbol+.
+ # url(env, :dashboard) # => "/dashboard"
+ #
+ # Additional parameters can be passed in as a hash
+ # url(env, :people, :id => "1") # => "/people/1"
+ #
+ # If no name route is given, it will fall back to a slower
+ # generation search.
+ # url(env, :controller => "people", :action => "show", :id => "1")
+ # # => "/people/1"
+ def url(env, *args)
+ named_route, params = nil, {}
+
+ case args.length
+ when 2
+ named_route, params = args[0], args[1].dup
+ when 1
+ if args[0].is_a?(Hash)
+ params = args[0].dup
+ else
+ named_route = args[0]
+ end
+ else
+ raise ArgumentError
+ end
+
+ only_path = params.delete(:only_path)
+ recall = env[@parameters_key] || {}
+
+ unless result = generate(:all, named_route, params, recall,
+ :parameterize => lambda { |name, param| Utils.escape_uri(param) })
+ return
+ end
+
+ parts, params = result
+ return unless parts
+
+ params.each do |k, v|
+ if v
+ params[k] = v
+ else
+ params.delete(k)
+ end
+ end
+
+ req = stubbed_request_class.new(env)
+ req._stubbed_values = parts.merge(:query_string => Utils.build_nested_query(params))
+ only_path ? req.fullpath : req.url
+ end
+
+ def generate(method, *args) #:nodoc:
+ raise 'route set not finalized' unless @generation_graph
+
+ method = nil if method == :all
+ named_route, params, recall, options = extract_params!(*args)
+ merged = recall.merge(params)
+ route = nil
+
+ if named_route
+ if route = @named_routes[named_route.to_sym]
+ recall = route.defaults.merge(recall)
+ url = route.generate(method, params, recall, options)
+ [url, params]
+ else
+ raise RoutingError, "#{named_route} failed to generate from #{params.inspect}"
+ end
+ else
+ keys = @generation_keys.map { |key|
+ if k = merged[key]
+ k.to_s
+ else
+ nil
+ end
+ }
+ @generation_graph[*keys].each do |r|
+ next unless r.significant_params?
+ if url = r.generate(method, params, recall, options)
+ return [url, params]
+ end
+ end
+
+ raise RoutingError, "No route matches #{params.inspect}"
+ end
+ end
+
+ # Number of routes in the set
+ def length
+ @routes.length
+ end
+
+ def rehash #:nodoc:
+ @recognition_keys = build_recognition_keys
+ @recognition_graph = build_recognition_graph
+ @generation_keys = build_generation_keys
+ @generation_graph = build_generation_graph
+ end
+
+ # Finalizes the set and builds optimized data structures. You *must*
+ # freeze the set before you can use <tt>call</tt> and <tt>url</tt>.
+ # So remember to call freeze after you are done adding routes.
+ def freeze
+ unless frozen?
+ rehash
+
+ @recognition_key_analyzer = nil
+ @generation_key_analyzer = nil
+ @valid_conditions = nil
+
+ @routes.each { |route| route.freeze }
+ @routes.freeze
+ end
+
+ super
+ end
+
+ def marshal_dump #:nodoc:
+ hash = {}
+
+ instance_variables_to_serialize.each do |ivar|
+ hash[ivar] = instance_variable_get(ivar)
+ end
+
+ if graph = hash[:@recognition_graph]
+ hash[:@recognition_graph] = graph.dup
+ end
+
+ hash
+ end
+
+ def marshal_load(hash) #:nodoc:
+ hash.each do |ivar, value|
+ instance_variable_set(ivar, value)
+ end
+ end
+
+ protected
+ def recognition_stats
+ { :keys => @recognition_keys,
+ :keys_size => @recognition_keys.size,
+ :graph_size => @recognition_graph.size,
+ :graph_height => @recognition_graph.height,
+ :graph_average_height => @recognition_graph.average_height }
+ end
+
+ private
+ def expire! #:nodoc:
+ @recognition_keys = @recognition_graph = nil
+ @recognition_key_analyzer.expire!
+
+ @generation_keys = @generation_graph = nil
+ @generation_key_analyzer.expire!
+ end
+
+ def instance_variables_to_serialize
+ instance_variables.map { |ivar| ivar.to_sym } - [:@stubbed_request_class, :@optimized_recognize_defined]
+ end
+
+ # An internal helper method for constructing a nested set from
+ # the linear route set.
+ #
+ # build_nested_route_set([:request_method, :path_info]) { |route, method|
+ # route.send(method)
+ # }
+ def build_nested_route_set(keys, &block)
+ graph = Multimap.new
+ @routes.each_with_index do |route, index|
+ catch(:skip) do
+ k = keys.map { |key| block.call(key, index) }
+ Utils.pop_trailing_nils!(k)
+ k.map! { |key| key || /.+/ }
+ graph[*k] = route
+ end
+ end
+ graph
+ end
+
+ def build_recognition_graph
+ build_nested_route_set(@recognition_keys) { |k, i|
+ @recognition_key_analyzer.possible_keys[i][k]
+ }
+ end
+
+ def build_recognition_keys
+ @recognition_key_analyzer.report
+ end
+
+ def build_generation_graph
+ build_nested_route_set(@generation_keys) { |k, i|
+ throw :skip unless @routes[i].significant_params?
+
+ if k = @generation_key_analyzer.possible_keys[i][k]
+ k.to_s
+ else
+ nil
+ end
+ }
+ end
+
+ def build_generation_keys
+ @generation_key_analyzer.report
+ end
+
+ def extract_params!(*args)
+ case args.length
+ when 4
+ named_route, params, recall, options = args
+ when 3
+ if args[0].is_a?(Hash)
+ params, recall, options = args
+ else
+ named_route, params, recall = args
+ end
+ when 2
+ if args[0].is_a?(Hash)
+ params, recall = args
+ else
+ named_route, params = args
+ end
+ when 1
+ if args[0].is_a?(Hash)
+ params = args[0]
+ else
+ named_route = args[0]
+ end
+ else
+ raise ArgumentError
+ end
+
+ named_route ||= nil
+ params ||= {}
+ recall ||= {}
+ options ||= {}
+
+ [named_route, params.dup, recall.dup, options.dup]
+ end
+
+ def stubbed_request_class
+ @stubbed_request_class ||= begin
+ klass = Class.new(@request_class)
+ klass.public_instance_methods.each do |method|
+ next if method =~ /^__|object_id/
+ klass.class_eval <<-RUBY
+ def #{method}(*args, &block)
+ @_stubbed_values[:#{method}] || super
+ end
+ RUBY
+ end
+ klass.class_eval { attr_accessor :_stubbed_values }
+ klass
+ end
+ end
+ end
+end
View
68 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp.rb
@@ -0,0 +1,68 @@
+require 'rack/mount/strexp/parser'
+
+module Rack::Mount
+ class Strexp
+ class << self
+ # Parses segmented string expression and converts it into a Regexp
+ #
+ # Strexp.compile('foo')
+ # # => %r{\Afoo\Z}
+ #
+ # Strexp.compile('foo/:bar', {}, ['/'])
+ # # => %r{\Afoo/(?<bar>[^/]+)\Z}
+ #
+ # Strexp.compile(':foo.example.com')
+ # # => %r{\A(?<foo>.+)\.example\.com\Z}
+ #
+ # Strexp.compile('foo/:bar', {:bar => /[a-z]+/}, ['/'])
+ # # => %r{\Afoo/(?<bar>[a-z]+)\Z}
+ #
+ # Strexp.compile('foo(.:extension)')
+ # # => %r{\Afoo(\.(?<extension>.+))?\Z}
+ #
+ # Strexp.compile('src/*files')
+ # # => %r{\Asrc/(?<files>.+)\Z}
+ def compile(str, requirements = {}, separators = [], anchor = true)
+ return Regexp.compile(str) if str.is_a?(Regexp)
+
+ requirements = requirements ? requirements.dup : {}
+ normalize_requirements!(requirements, separators)
+
+ parser = StrexpParser.new
+ parser.anchor = anchor
+ parser.requirements = requirements
+
+ begin
+ re = parser.scan_str(str)
+ rescue Racc::ParseError => e
+ raise RegexpError, e.message
+ end
+
+ Regexp.compile(re)
+ end
+ alias_method :new, :compile
+
+ private
+ def normalize_requirements!(requirements, separators)
+ requirements.each do |key, value|
+ if value.is_a?(Regexp)
+ if regexp_has_modifiers?(value)
+ requirements[key] = value
+ else
+ requirements[key] = value.source
+ end
+ else
+ requirements[key] = Regexp.escape(value)
+ end
+ end
+ requirements.default ||= separators.any? ?
+ "[^#{separators.join}]+" : '.+'
+ requirements
+ end
+
+ def regexp_has_modifiers?(regexp)
+ regexp.options & (Regexp::IGNORECASE | Regexp::EXTENDED) != 0
+ end
+ end
+ end
+end
View
160 actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/strexp/parser.rb
@@ -0,0 +1,160 @@
+#
+# DO NOT MODIFY!!!!
+# This file is automatically generated by Racc 1.4.6
+# from Racc grammer file "".
+#
+
+require 'racc/parser.rb'
+
+require 'rack/mount/utils'
+require 'rack/mount/strexp/tokenizer'
+
+module Rack
+ module Mount
+ class StrexpParser < Racc::Parser
+
+
+if Regin.regexp_supports_named_captures?
+ REGEXP_NAMED_CAPTURE = '(?<%s>%s)'.freeze