Permalink
Browse files

Fixed incorrect behaviour with final optional parameters

  • Loading branch information...
1 parent 27735f8 commit 19803c5b0914e20d151a532b9b28ba000b444f08 @raul committed Nov 18, 2010
Showing with 27 additions and 17 deletions.
  1. +5 −15 lib/route_translator.rb
  2. +22 −2 test/translate_routes_test.rb
View
@@ -219,22 +219,12 @@ def add_prefix? locale
# Translates a path and adds the locale prefix.
def translate_path path, locale
- # Hack, please enlighten me with a better way.
- # without this, formatted root routes "/(.:format)" would translate to
- # "/de/(.:format)", which doesnt recognize "/de" (but "/de/")
- new_path = if path == "/(.:format)"
- ""
- else
- segments = path.split("/").map do |path_segment|
- translate_path_segment(path_segment, locale)
- end
-
- segments.join "/"
- end
-
+ final_optional_segments = path.match(/(\(.+\))$/)[1] rescue nil # i.e: (.:format)
+ path_segments = path.gsub(final_optional_segments,'').split("/")
+ new_path = path_segments.map{ |seg| translate_path_segment(seg, locale) }.join('/')
new_path = "/#{locale}#{new_path}" if add_prefix? locale
-
- new_path.blank? ? '/' : new_path
+ new_path = '/' if new_path.blank?
+ final_optional_segments ? new_path + final_optional_segments : new_path
end
# Tries to translate a single path segment. If the path segment
@@ -90,8 +90,7 @@ def test_named_translated_route_with_prefix_must_have_locale_as_static_segment
# we check the string representation of the route,
# if it stores locale as a dynamic segment it would be represented as: "/:locale/gente"
- people_es = @routes.routes.select{ |r| r.name == 'people_es' }.first
- assert_equal "/es/gente", people_es.to_s.split(' ')[1]
+ assert_equal "/es/gente(.:format)", path_string(named_route('people_es'))
end
def test_named_empty_route_without_prefix
@@ -148,6 +147,16 @@ def test_named_translated_route_on_non_default_locale_without_prefix
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_en, :people_es, :people
end
+
+ def test_formatted_root_route
+ @routes.draw{ root :to => 'people#index', :as => 'root' }
+ @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
+ assert_equal '/(.:format)', path_string(named_route('root'))
+ translate_routes
+ assert_equal '/(.:format)', path_string(named_route('root_en'))
+ assert_equal '/es(.:format)', path_string(named_route('root_es'))
+ end
+
def test_languages_load_from_file
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
@@ -212,6 +221,17 @@ def test_action_view_gets_locale_suffix_helper
private
+ # Given a route defined as a string like this:
+ # 'ANY /es(.:format) {:controller=>"people", :action=>"index"}'
+ # returns "/es(.:format)"
+ def path_string(route)
+ route.to_s.split(' ')[1]
+ end
+
+ def named_route(name)
+ @routes.routes.select{ |r| r.name == name }.first
+ end
+
def assert_helpers_include(*helpers)
helpers.each do |helper|
['url', 'path'].each do |suffix|

0 comments on commit 19803c5

Please sign in to comment.