Skip to content

Commit

Permalink
Integrate consistency lambdas into host
Browse files Browse the repository at this point in the history
Fix #91, #171
  • Loading branch information
tagliala committed Dec 3, 2017
1 parent f21b20e commit b1a9ac0
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 86 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.0.0.develop / unreleased

* [BUGFIX] Verify host path consistency by default ([#91](https://github.com/enriclluelles/route_translator/issues/91), [#171](https://github.com/enriclluelles/route_translator/issues/171))
* [FEATURE] Remove the option to verify host path consistency
* [ENHANCEMENT] Update development dependencies

## 5.5.1 / 2017-11-14

* [BUGFIX] Change spec to reflect Rails 5.1.3 change in url generation ([#172](https://github.com/enriclluelles/route_translator/issues/172))
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,6 @@ end
* **locale_segment_proc**
The locale segment of the url will by default be `locale.to_s.downcase`
You can supply your own mechanism via a Proc that takes `locale` as an argument, e.g. `config.locale_segment_proc = ->(locale) { locale.to_s.upcase }`
* **verify_host_path_consistency**
By default, if you use different hosts to translate your application, all translated paths will work on all hosts. Set this option to `true` to force
a matching of the host associated locale with the translated path locale as part of the route definition.
Defaults to `false`.
### Host-based Locale
Expand Down
6 changes: 2 additions & 4 deletions lib/route_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'route_translator/extensions'
require 'route_translator/translator'
require 'route_translator/host'
require 'route_translator/host_path_consistency_lambdas'
require 'route_translator/locale_sanitizer'

module RouteTranslator
Expand All @@ -16,7 +15,7 @@ module RouteTranslator
Configuration = Struct.new(:available_locales, :disable_fallback, :force_locale,
:hide_locale, :host_locales, :generate_unlocalized_routes,
:generate_unnamed_unlocalized_routes, :locale_param_key,
:locale_segment_proc, :verify_host_path_consistency)
:locale_segment_proc)

class << self
private
Expand All @@ -42,11 +41,10 @@ def config(&block)
@config.generate_unnamed_unlocalized_routes ||= false
@config.locale_param_key ||= :locale
@config.locale_segment_proc ||= nil
@config.verify_host_path_consistency ||= false

yield @config if block

resolve_host_locale_config_conflicts unless @config.host_locales.empty?
resolve_host_locale_config_conflicts if @config.host_locales.present?

@config
end
Expand Down
4 changes: 2 additions & 2 deletions lib/route_translator/extensions/route_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def translate_mapping(locale, route_set, translated_options, translated_path_ast

blocks = scope[:blocks] ? scope[:blocks].dup : []

if RouteTranslator.config.verify_host_path_consistency
blocks.push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale)
if RouteTranslator.config.host_locales.present?
blocks.push RouteTranslator::Host.lambdas_for_locale(locale)
end

::ActionDispatch::Routing::Mapper::Mapping.new(route_set, translated_path_ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, blocks, via, translated_options_constraints, anchor, options)
Expand Down
10 changes: 10 additions & 0 deletions lib/route_translator/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Host
class << self
private

def lambdas
@lambdas ||= {}
end

def regex_for(host_string)
escaped = Regexp.escape(host_string).gsub('\*', '.*?').gsub('\.', '\.?')
Regexp.new("^#{escaped}$", Regexp::IGNORECASE)
Expand All @@ -28,5 +32,11 @@ def locale_from_host(host)
locales &= I18n.available_locales
(locales.first || I18n.default_locale).to_sym
end

def lambdas_for_locale(locale)
sanitized_locale = RouteTranslator::LocaleSanitizer.sanitize(locale)

lambdas[sanitized_locale] ||= ->(req) { sanitized_locale == RouteTranslator::Host.locale_from_host(req.host).to_s }
end
end
end
21 changes: 0 additions & 21 deletions lib/route_translator/host_path_consistency_lambdas.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/route_translator/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RouteTranslator
VERSION = '5.5.1'.freeze
VERSION = '6.0.0.develop'.freeze
end
35 changes: 0 additions & 35 deletions test/integration/host_locale_path_verify_consistency_test.rb

This file was deleted.

46 changes: 28 additions & 18 deletions test/integration/host_locales_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,41 @@ def teardown
end

def test_root_path
## root of es com
# root of es com
host! 'www.testapp.es'
get '/'
assert_equal 'es', @response.body
assert_response :success

## root of ru com
# root of ru com
host! 'ru.testapp.com'
get '/'
assert_equal 'ru', @response.body
assert_response :success
end

def test_explicit_path
## native es route on es com
# native es route on es com
host! 'www.testapp.es'
get '/dummy'
assert_equal 'es', @response.body
assert_response :success

## ru route on es com
host! 'www.testapp.es'
get "/ru/#{CGI.escape('манекен')}"
assert_equal 'ru', @response.body
assert_response :success

## native ru route on ru com
# native ru route on ru com
host! 'ru.testapp.com'
get "/#{CGI.escape('манекен')}"
assert_equal 'ru', @response.body
assert_response :success

## es route on ru com
host! 'ru.testapp.com'
get '/es/dummy'
assert_equal 'es', @response.body
assert_response :success
end

def test_generated_path
## native es route on es com
# native es route on es com
host! 'www.testapp.es'
get '/native'
assert_equal '/mostrar', @response.body
assert_response :success

## native ru route on ru com
# native ru route on ru com
host! 'ru.testapp.com'
get '/native'
assert_equal "/#{CGI.escape('показывать')}", @response.body
Expand All @@ -75,4 +63,26 @@ def test_preserve_i18n_locale

assert_equal :en, I18n.locale
end

def test_non_native_path
# ru route on es com
host! 'www.testapp.es'
get "/ru/#{CGI.escape('манекен')}"
assert_response :not_found

# es route on ru com
host! 'ru.testapp.com'
get '/es/dummy'
assert_response :not_found

# unprefixed es route on ru com
host! 'ru.testapp.com'
get "/#{CGI.escape('dummy')}"
assert_response :not_found

# unprefixed ru route on es com
host! 'www.testapp.es'
get "/#{CGI.escape('манекен')}"
assert_response :not_found
end
end
1 change: 0 additions & 1 deletion test/support/configuration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def config_reset
config_default_locale_settings :en
config_host_locales {}
config_locale_segment_proc false
config_verify_host_path_consistency false

BOOLEAN_OPTIONS.each do |option, default_value|
send(:"config_#{option}", default_value)
Expand Down

0 comments on commit b1a9ac0

Please sign in to comment.