Skip to content

Commit

Permalink
Support unicode character route in config/routes.rb.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Jun 15, 2012
1 parent c1b1956 commit c42a7a0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##

* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:

get Rack::Utils.escape('こんにちは') => 'home#index'

You just have to write the unicode route:

get 'こんにちは' => 'home#index'

*kennyj*

* Return proper format on exceptions. *Santiago Pastorino*

* Allow to use mounted_helpers (helpers for accessing mounted engines) in ActionView::TestCase. *Piotr Sarnacki*
Expand Down
7 changes: 7 additions & 0 deletions actionpack/lib/action_dispatch/routing.rb
@@ -1,3 +1,4 @@
# encoding: UTF-8
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/regexp'

Expand Down Expand Up @@ -218,6 +219,12 @@ module ActionDispatch
#
# match "/stories" => redirect("/posts")
#
# == Unicode character routes
#
# You can specify unicode character routes in your router:
#
# match "こんにちは" => "welcome#index"
#
# == Routing to Rack Applications
#
# Instead of a String, like <tt>posts#index</tt>, which corresponds to the
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -1387,7 +1387,7 @@ def add_route(action, options) # :nodoc:
options[:as] = name_for_action(options[:as], action)
end

mapping = Mapping.new(@set, @scope, path, options)
mapping = Mapping.new(@set, @scope, URI.parser.escape(path), options)
app, conditions, requirements, defaults, as, anchor = mapping.to_route
@set.add_route(app, conditions, requirements, defaults, as, anchor)
end
Expand Down
5 changes: 5 additions & 0 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -1760,6 +1760,7 @@ class RackMountIntegrationTests < ActiveSupport::TestCase
get 'account(/:action)' => "account#subscription"
get 'pages/:page_id/:controller(/:action(/:id))'
get ':controller/ping', :action => 'ping'
get 'こんにちは/世界', :controller => 'news', :action => 'index'
match ':controller(/:action(/:id))(.:format)', :via => :all
root :to => "news#index"
}
Expand Down Expand Up @@ -1876,6 +1877,10 @@ def test_extras
assert_equal({:controller => 'people', :action => 'create', :person => { :name => 'Josh'}}, params)
end

def test_unicode_path
assert_equal({:controller => 'news', :action => 'index'}, @routes.recognize_path(URI.parser.escape('こんにちは/世界'), :method => :get))
end

private
def sort_extras!(extras)
if extras.length == 2
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -2472,7 +2472,7 @@ def app; Routes end
class TestUnicodePaths < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/#{Rack::Utils.escape("ほげ")}" => lambda { |env|
get "/ほげ" => lambda { |env|
[200, { 'Content-Type' => 'text/plain' }, []]
}, :as => :unicode_path
end
Expand Down Expand Up @@ -2727,4 +2727,4 @@ def show
assert_response :bad_request
end
end
end
end
8 changes: 8 additions & 0 deletions guides/source/routing.textile
Expand Up @@ -644,6 +644,14 @@ You should put the +root+ route at the top of the file, because it is the most p

NOTE: The +root+ route only routes +GET+ requests to the action.

h4. Unicode character routes

You can specify unicode character routes directly. For example

<ruby>
match 'こんにちは' => 'welcome#index'
</ruby>

h3. Customizing Resourceful Routes

While the default routes and helpers generated by +resources :posts+ will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
Expand Down
2 changes: 2 additions & 0 deletions guides/source/upgrading_ruby_on_rails.textile
Expand Up @@ -50,6 +50,8 @@ h4(#action_pack4_0). Action Pack

Rails 4.0 changed how <tt>assert_generates</tt>, <tt>assert_recognizes</tt>, and <tt>assert_routing</tt> work. Now all these assertions raise <tt>Assertion</tt> instead of <tt>ActionController::RoutingError</tt>.

Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, e.g. <tt>get Rack::Utils.escape('こんにちは'), :controller => 'welcome', :action => 'index'</tt> to <tt>get 'こんにちは', :controller => 'welcome', :action => 'index'</tt>.

h4(#helpers_order). Helpers Loading Order

The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use <tt>helpers_path</tt> parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use <tt>config.railties_order=</tt> method.
Expand Down

0 comments on commit c42a7a0

Please sign in to comment.