Skip to content

Commit

Permalink
Integrate consistency lambdas into host
Browse files Browse the repository at this point in the history
Fixes #91 and closes #171
  • Loading branch information
tagliala committed Jan 13, 2021
1 parent ebf8ab0 commit 50532d2
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 74 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 10.0.0.alpha1 / 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

## 9.0.0 / 2020-11-07

* [ENHANCEMENT] Check for `empty?` instead of `any?` on available_locales array
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ end
| `host_locales` | Sets `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details | `{}` |
| `locale_param_key` | The param key used to set the locale to the newly generated routes | `:locale` |
| `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. `->(locale) { locale.to_s.upcase }` | `false` |
| `verify_host_path_consistency` | Forces a matching of the host associated locale with the translated path locale as part of the route definition. By default, if you use different hosts to translate your application, all translated paths will work on all hosts | `false` |
### Host-based Locale
Expand Down
4 changes: 1 addition & 3 deletions lib/route_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -23,8 +22,7 @@ module RouteTranslator
hide_locale: false,
host_locales: {},
locale_param_key: :locale,
locale_segment_proc: false,
verify_host_path_consistency: false
locale_segment_proc: false
}.freeze

Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys)
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 @@ -33,8 +33,8 @@ def translate_mapping(locale, route_set, translated_options, translated_path_ast
options: scope[:options] ? scope[:options].merge(translated_options) : translated_options
}

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

::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_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&.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 = '9.0.0'
VERSION = '10.0.0.alpha1'
end
34 changes: 0 additions & 34 deletions test/integration/host_locale_path_verify_consistency_test.rb

This file was deleted.

34 changes: 22 additions & 12 deletions test/integration/host_locales_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,11 @@ def test_explicit_path
assert_equal 'es', @response.body
assert_response :success

# ru route on es com
host! 'www.testapp.es'
get Addressable::URI.normalize_component('/ru/манекен')
assert_equal 'ru', @response.body
assert_response :success

# native ru route on ru com
host! 'ru.testapp.com'
get Addressable::URI.normalize_component('/манекен')
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
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 Addressable::URI.normalize_component('/ru/манекен')
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 '/dummy'
assert_response :not_found

# unprefixed ru route on es com
host! 'www.testapp.es'
get Addressable::URI.normalize_component('/манекен')
assert_response :not_found
end
end

0 comments on commit 50532d2

Please sign in to comment.