Skip to content

Commit

Permalink
add ability to set a prefix name for routes which have hyphen
Browse files Browse the repository at this point in the history
  • Loading branch information
amrnt committed Jan 1, 2014
1 parent 0d4614c commit df13989
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
* Add ability to set a prefix name for routes which have hyphen(s).

get '/contact-us' => 'pages#contact'
get '/about-us' => 'pages#about_us'

The above routes will inspected to

Prefix Verb URI Pattern Controller#Action
contact_us GET /contact-us(.:format) pages#contact
about_us GET /about-us(.:format) pages#about_us

*Amr Tamimi*

* Fix `Encoding::CompatibilityError` when public path is UTF-8

In #5337 we forced the path encoding to ASCII-8BIT to prevent static file handling
Expand Down
3 changes: 2 additions & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ def add_route(action, options) # :nodoc:
path = path_for_action(action, options.delete(:path))
action = action.to_s.dup

if action =~ /^[\w\/]+$/
if action =~ /^[\w\-\/]+$/
options[:action] ||= action unless action.include?("/")
else
action = nil
Expand Down Expand Up @@ -1632,6 +1632,7 @@ def name_for_action(as, action) #:nodoc:
when :root
[name_prefix, collection_name, prefix]
else
prefix.gsub!(/\-/, '_') if prefix.is_a?(String)
[name_prefix, member_name, prefix]
end

Expand Down
4 changes: 3 additions & 1 deletion actionpack/test/dispatch/routing/inspector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def self.inspect
end
engine.routes.draw do
get '/cart', :to => 'cart#show'
get '/view-cart', :to => 'cart#show'
end

output = draw do
Expand All @@ -50,7 +51,8 @@ def self.inspect
" blog /blog Blog::Engine",
"",
"Routes for Blog::Engine:",
" cart GET /cart(.:format) cart#show"
" cart GET /cart(.:format) cart#show",
"view_cart GET /view-cart(.:format) cart#show"
], output
end

Expand Down
2 changes: 2 additions & 0 deletions actionpack/test/dispatch/routing/route_set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def call(env)
draw do
get 'foo', to: SimpleApp.new('foo#index')
get 'bar', to: SimpleApp.new('bar#index')
get 'foo-bar', to: SimpleApp.new('bar#index')
end

assert_equal '/foo', url_helpers.foo_path
assert_equal '/bar', url_helpers.bar_path
assert_equal '/foo-bar', url_helpers.foo_bar_path
end

test "url helpers are updated when route is updated" do
Expand Down

0 comments on commit df13989

Please sign in to comment.