Skip to content

Commit

Permalink
fix for url_for failing with format
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikgrygiel committed Oct 4, 2012
1 parent db56721 commit ea84364
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
46 changes: 24 additions & 22 deletions lib/i18n_routing_rails31.rb
Expand Up @@ -7,7 +7,7 @@ module I18nRouting
module Mapper

private

# Just create a Mapper:Resource with given parameters
def resource_from_params(type, *resources)
res = resources.clone
Expand All @@ -17,7 +17,7 @@ def resource_from_params(type, *resources)

type == :resource ? ActionDispatch::Routing::Mapper::SingletonResource.new(r, options.dup) : ActionDispatch::Routing::Mapper::Resource.new(r, options.dup)
end

# Localize a resources or a resource
def localized_resources(type = :resources, *resources, &block)
localizable_route = nil
Expand All @@ -42,7 +42,7 @@ def localized_resources(type = :resources, *resources, &block)
opts = options.dup
opts[:path] = localized_path
opts[:controller] ||= r.to_s.pluralize

resource = resource_from_params(type, r, opts.dup)

res = ["#{I18nRouting.locale_escaped(locale)}_#{r}".to_sym, opts]
Expand All @@ -53,7 +53,7 @@ def localized_resources(type = :resources, *resources, &block)
scope(:constraints => constraints, :path_names => I18nRouting.path_names(resource.name, @scope)) do
localized_branch(locale) do
send(type, *res) do

# In the resource(s) block, we need to keep and restore some context :
if block
old_name = @scope[:i18n_real_resource_name]
Expand Down Expand Up @@ -92,7 +92,7 @@ def localized_resources(type = :resources, *resources, &block)
# If yes, localizable is a name, or a Mapper::Resource
# Can take a block, if so, save the current context, set the new
# Call the block, then restore the old context and return the block return
def set_localizable_route(localizable)
def set_localizable_route(localizable)
if block_given?
old = @set.named_routes.localizable
@set.named_routes.set_localizable_route(localizable)
Expand All @@ -103,11 +103,11 @@ def set_localizable_route(localizable)
@set.named_routes.set_localizable_route(localizable)
end
end

def localizable_route
@set.named_routes.localizable
end

# Return the aproximate deep in scope level
def nested_deep
(@scope and Array === @scope[:nested_deep] and @scope[:scope_level]) ? @scope[:nested_deep].size : 0
Expand Down Expand Up @@ -156,15 +156,15 @@ def localized(locales = I18n.available_locales, opts = {})
I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq
@i18n_routing_path_set = true
end

old_value = @locales
@locales = locales
@i18n_verbose ||= opts.delete(:verbose)
yield
ensure
@locales = old_value
end

# Create a branch for create routes in the specified locale
def localized_branch(locale)
set_localizable_route(nil) do
Expand All @@ -176,7 +176,7 @@ def localized_branch(locale)
@localized_branch = old
end
end

# Set we do not want to localize next resource
def skip_localization
old = @skip_localization
Expand Down Expand Up @@ -227,7 +227,7 @@ def match(*args)

super
end

def create_globalized_resources(type, *resources, &block)
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"

Expand All @@ -254,16 +254,16 @@ def create_globalized_resources(type, *resources, &block)
send("#{type}_without_i18n_routing".to_sym, *resources, &block)
end
end

@scope[:nested_deep].pop
end

# Alias methods in order to handle i18n routes
def self.included(mod)
mod.send :alias_method_chain, :initialize, :i18n_routing
mod.send :alias_method_chain, :resource, :i18n_routing
mod.send :alias_method_chain, :resources, :i18n_routing

# Here we redefine some methods, in order to handle
# correct path_names translation on the fly
[:map_method, :member, :collection].each do |m|
Expand All @@ -272,7 +272,7 @@ def self.included(mod)
if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name]
o = @scope[:scope_level_resource]
@scope[:scope_level_resource] = @scope[:i18n_scope_level_resource]

pname = @scope[:path_names] || {}
i = 1
while i < args.size and (String === args[i] or Symbol === args[i])
Expand All @@ -285,18 +285,18 @@ def self.included(mod)
@scope[:scope_level_resource] = o
return
end

send(rfname, *args, &block)
end

mod.send :alias_method_chain, m, :i18n_routing
end
end

def resource_with_i18n_routing(*resources, &block)
create_globalized_resources(:resource, *resources, &block)
end

def resources_with_i18n_routing(*resources, &block)
create_globalized_resources(:resources, *resources, &block)
end
Expand All @@ -313,11 +313,12 @@ def initialize(locale, set, scope, path, options)
# try to get translated path :
I18n.locale = locale
ts = @path.gsub(/^\//, '')
append_format = ts =~ /\(\.:format\)/
ts.gsub!('(.:format)', '')
tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||

tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
!ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts

localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
path = localized_scope ? '/' << localized_scope : @scope[:path]
@localized_path = File.join((path || ''), tp).gsub(/\/$/, '')
Expand All @@ -327,6 +328,7 @@ def initialize(locale, set, scope, path, options)
#@options[:controller] ||= @options[:as]
@options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
@path = @localized_path
@path = "#{@path}(.:format)" if append_format
@options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
@options[:constraints][:i18n_locale] = locale.to_s
@options[:anchor] = true
Expand Down
46 changes: 24 additions & 22 deletions lib/i18n_routing_rails32.rb
Expand Up @@ -7,7 +7,7 @@ module I18nRouting
module Mapper

private

# Just create a Mapper:Resource with given parameters
def resource_from_params(type, *resources)
res = resources.clone
Expand All @@ -17,7 +17,7 @@ def resource_from_params(type, *resources)

type == :resource ? ActionDispatch::Routing::Mapper::SingletonResource.new(r, options.dup) : ActionDispatch::Routing::Mapper::Resource.new(r, options.dup)
end

# Localize a resources or a resource
def localized_resources(type = :resources, *resources, &block)
localizable_route = nil
Expand All @@ -42,7 +42,7 @@ def localized_resources(type = :resources, *resources, &block)
opts = options.dup
opts[:path] = localized_path
opts[:controller] ||= r.to_s.pluralize

resource = resource_from_params(type, r, opts.dup)

res = ["#{I18nRouting.locale_escaped(locale)}_#{r}".to_sym, opts]
Expand All @@ -53,7 +53,7 @@ def localized_resources(type = :resources, *resources, &block)
scope(:constraints => constraints, :path_names => I18nRouting.path_names(resource.name, @scope)) do
localized_branch(locale) do
send(type, *res) do

# In the resource(s) block, we need to keep and restore some context :
if block
old_name = @scope[:i18n_real_resource_name]
Expand Down Expand Up @@ -92,7 +92,7 @@ def localized_resources(type = :resources, *resources, &block)
# If yes, localizable is a name, or a Mapper::Resource
# Can take a block, if so, save the current context, set the new
# Call the block, then restore the old context and return the block return
def set_localizable_route(localizable)
def set_localizable_route(localizable)
if block_given?
old = @set.named_routes.localizable
@set.named_routes.set_localizable_route(localizable)
Expand All @@ -103,11 +103,11 @@ def set_localizable_route(localizable)
@set.named_routes.set_localizable_route(localizable)
end
end

def localizable_route
@set.named_routes.localizable
end

# Return the aproximate deep in scope level
def nested_deep
(@scope and Array === @scope[:nested_deep] and @scope[:scope_level]) ? @scope[:nested_deep].size : 0
Expand Down Expand Up @@ -155,15 +155,15 @@ def localized(locales = I18n.available_locales, opts = {})
I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq
@i18n_routing_path_set = true
end

old_value = @locales
@locales = locales
@i18n_verbose ||= opts.delete(:verbose)
yield
ensure
@locales = old_value
end

# Create a branch for create routes in the specified locale
def localized_branch(locale)
set_localizable_route(nil) do
Expand All @@ -175,7 +175,7 @@ def localized_branch(locale)
@localized_branch = old
end
end

# Set we do not want to localize next resource
def skip_localization
old = @skip_localization
Expand Down Expand Up @@ -226,7 +226,7 @@ def match(*args)

super
end

def create_globalized_resources(type, *resources, &block)
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"

Expand All @@ -253,16 +253,16 @@ def create_globalized_resources(type, *resources, &block)
send("#{type}_without_i18n_routing".to_sym, *resources, &block)
end
end

@scope[:nested_deep].pop
end

# Alias methods in order to handle i18n routes
def self.included(mod)
mod.send :alias_method_chain, :initialize, :i18n_routing
mod.send :alias_method_chain, :resource, :i18n_routing
mod.send :alias_method_chain, :resources, :i18n_routing

# Here we redefine some methods, in order to handle
# correct path_names translation on the fly
[:map_method, :member, :collection].each do |m|
Expand All @@ -271,7 +271,7 @@ def self.included(mod)
if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name]
o = @scope[:scope_level_resource]
@scope[:scope_level_resource] = @scope[:i18n_scope_level_resource]

pname = @scope[:path_names] || {}
i = 1
while i < args.size and (String === args[i] or Symbol === args[i])
Expand All @@ -284,18 +284,18 @@ def self.included(mod)
@scope[:scope_level_resource] = o
return
end

send(rfname, *args, &block)
end

mod.send :alias_method_chain, m, :i18n_routing
end
end

def resource_with_i18n_routing(*resources, &block)
create_globalized_resources(:resource, *resources, &block)
end

def resources_with_i18n_routing(*resources, &block)
create_globalized_resources(:resources, *resources, &block)
end
Expand All @@ -312,11 +312,12 @@ def initialize(locale, set, scope, path, options)
# try to get translated path :
I18n.locale = locale
ts = @path.gsub(/^\//, '')
append_format = ts =~ /\(\.:format\)/
ts.gsub!('(.:format)', '')
tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||

tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
!ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts

localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
path = localized_scope ? '/' << localized_scope : @scope[:path]
@localized_path = File.join((path || ''), tp).gsub(/\/$/, '')
Expand All @@ -326,6 +327,7 @@ def initialize(locale, set, scope, path, options)
#@options[:controller] ||= @options[:as]
@options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
@path = @localized_path
@path = "#{@path}(.:format)" if append_format
@options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
@options[:constraints][:i18n_locale] = locale.to_s
@options[:anchor] = true
Expand Down

0 comments on commit ea84364

Please sign in to comment.