route_translator 5.4.1 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60e00314bcf617c8a2865378a33cbb4c70326a96
4
- data.tar.gz: 7b35a7735c096861591f11136a42253f7777f686
3
+ metadata.gz: 31c45f4f07f63d37b9cdcf3d3d03e4fa1b59c2dd
4
+ data.tar.gz: 6ef546c5332a49738627d17945dc4885d27ff0d3
5
5
  SHA512:
6
- metadata.gz: 4af68cb388091bccbd13b5fc2f1f601ba7327cd80fce37d575741068e6688e2d778fc6b2b154cd13a1706c528bd2b59d24310b69f9571054d9f94cc9bf0640a3
7
- data.tar.gz: d2a04e4d7b66e25be8759ec8a3d95bbec903379138a48667c59f4da7415408058b785e4ffaabb412e4dea5ae997e501a1d68a5cb925256f4486b51d5142c8483
6
+ metadata.gz: 1db0724276af9860e0625f4658c5ed52cf8636e4367d6238f0386ac3bfa4cd9d94fcc51021cb171916f3d3887ed893cba4c445b4964e3e39bedb651437926614
7
+ data.tar.gz: a0d0a239715a71ac417ebfaef82be0cbfa32ad0042afdcd45d22dd4fe434ef9bbf18002104f531a46a06eaa4a1d0ee39fd4e9533ec7ce0f89cb4990d6c6b8c6a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.5.0 / 2017-06-29
4
+
5
+ * [FEATURE] Add an option to verify host path consistency (#167)
6
+ * [ENHANCHEMENT] Minor improvements
7
+
3
8
  ## 5.4.1 / 2017-06-01
4
9
 
5
10
  * [ENHANCEMENT] Update development dependencies
data/README.md CHANGED
@@ -284,7 +284,10 @@ end
284
284
  * **locale_segment_proc**
285
285
  The locale segment of the url will by default be `locale.to_s.downcase`
286
286
  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 }`
287
-
287
+ * **verify_host_path_consistency**
288
+ 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
289
+ a matching of the host associated locale with the translated path locale as part of the route definition.
290
+ Defaults to `false`.
288
291
 
289
292
  ### Host-based Locale
290
293
 
@@ -2,9 +2,11 @@
2
2
 
3
3
  require 'active_support'
4
4
 
5
- require File.expand_path('../route_translator/extensions', __FILE__)
6
- require File.expand_path('../route_translator/translator', __FILE__)
7
- require File.expand_path('../route_translator/host', __FILE__)
5
+ require 'route_translator/extensions'
6
+ require 'route_translator/translator'
7
+ require 'route_translator/host'
8
+ require 'route_translator/host_path_consistency_lambdas'
9
+ require 'route_translator/locale_sanitizer'
8
10
 
9
11
  module RouteTranslator
10
12
  extend RouteTranslator::Host
@@ -14,7 +16,7 @@ module RouteTranslator
14
16
  Configuration = Struct.new(:available_locales, :disable_fallback, :force_locale,
15
17
  :hide_locale, :host_locales, :generate_unlocalized_routes,
16
18
  :generate_unnamed_unlocalized_routes, :locale_param_key,
17
- :locale_segment_proc)
19
+ :locale_segment_proc, :verify_host_path_consistency)
18
20
 
19
21
  class << self
20
22
  private
@@ -40,6 +42,7 @@ module RouteTranslator
40
42
  @config.generate_unnamed_unlocalized_routes ||= false
41
43
  @config.locale_param_key ||= :locale
42
44
  @config.locale_segment_proc ||= nil
45
+ @config.verify_host_path_consistency ||= false
43
46
 
44
47
  yield @config if block
45
48
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require File.expand_path('../extensions/mapper', __FILE__)
4
- require File.expand_path('../extensions/route_set', __FILE__)
5
- require File.expand_path('../extensions/action_controller', __FILE__)
3
+ require 'route_translator/extensions/mapper'
4
+ require 'route_translator/extensions/route_set'
5
+ require 'route_translator/extensions/action_controller'
@@ -8,9 +8,9 @@ module ActionDispatch
8
8
  def add_localized_route(mapping, path_ast, name, anchor, scope, path, controller, default_action, to, via, formatted, options_constraints, options)
9
9
  route = RouteTranslator::Route.new(self, path, name, options_constraints, options, mapping)
10
10
 
11
- RouteTranslator::Translator.translations_for(route) do |translated_name, translated_path, translated_options_constraints, translated_options|
11
+ RouteTranslator::Translator.translations_for(route) do |locale, translated_name, translated_path, translated_options_constraints, translated_options|
12
12
  translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path)
13
- translated_mapping = ::ActionDispatch::Routing::Mapper::Mapping.build(scope, self, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options)
13
+ translated_mapping = translate_mapping(locale, self, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
14
14
 
15
15
  add_route_to_set translated_mapping, translated_path_ast, translated_name, anchor
16
16
  end
@@ -24,6 +24,21 @@ module ActionDispatch
24
24
 
25
25
  private
26
26
 
27
+ def translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
28
+ options = scope[:options] ? scope[:options].merge(translated_options) : translated_options
29
+
30
+ defaults = (scope[:defaults] || {}).dup
31
+ scope_constraints = scope[:constraints] || {}
32
+
33
+ blocks = scope[:blocks] ? scope[:blocks].dup : []
34
+
35
+ if RouteTranslator.config.verify_host_path_consistency
36
+ blocks.push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale)
37
+ end
38
+
39
+ ::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)
40
+ end
41
+
27
42
  def add_route_to_set(mapping, path_ast, name, anchor)
28
43
  if method(:add_route).arity == 4
29
44
  add_route mapping, path_ast, name, anchor
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RouteTranslator
4
+ module HostPathConsistencyLambdas
5
+ class << self
6
+ private
7
+
8
+ def lambdas
9
+ @lambdas ||= {}
10
+ end
11
+ end
12
+
13
+ module_function
14
+
15
+ def for_locale(locale)
16
+ sanitized_locale = RouteTranslator::LocaleSanitizer.sanitize(locale)
17
+
18
+ lambdas[sanitized_locale] ||= ->(req) { sanitized_locale == RouteTranslator::Host.locale_from_host(req.host).to_s }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RouteTranslator
4
+ module LocaleSanitizer
5
+ module_function
6
+
7
+ def sanitize(locale)
8
+ locale.to_s.gsub('native_', '')
9
+ end
10
+ end
11
+ end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require File.expand_path('../translator/route_helpers', __FILE__)
4
- require File.expand_path('../translator/path', __FILE__)
5
- require File.expand_path('../route', __FILE__)
3
+ require 'route_translator/translator/route_helpers'
4
+ require 'route_translator/translator/path'
5
+ require 'route_translator/route'
6
6
 
7
7
  module RouteTranslator
8
8
  module Translator
@@ -25,7 +25,7 @@ module RouteTranslator
25
25
  translated_options = options.dup
26
26
 
27
27
  if translated_options.exclude?(RouteTranslator.locale_param_key)
28
- translated_options[RouteTranslator.locale_param_key] = locale.to_s.gsub('native_', '')
28
+ translated_options[RouteTranslator.locale_param_key] = RouteTranslator::LocaleSanitizer.sanitize(locale)
29
29
  end
30
30
 
31
31
  translated_options
@@ -71,7 +71,7 @@ module RouteTranslator
71
71
  translated_options_constraints = translate_options_constraints(route.options_constraints, locale)
72
72
  translated_options = translate_options(route.options, locale)
73
73
 
74
- yield translated_name, translated_path, translated_options_constraints, translated_options
74
+ yield locale, translated_name, translated_path, translated_options_constraints, translated_options
75
75
  end
76
76
  end
77
77
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require File.expand_path('../path/segment', __FILE__)
3
+ require 'route_translator/translator/path/segment'
4
4
 
5
5
  module RouteTranslator
6
6
  module Translator
@@ -32,8 +32,8 @@ module RouteTranslator
32
32
  end
33
33
 
34
34
  def translate_string(str, locale, scope)
35
- locale = locale.to_s.gsub('native_', '')
36
- translated_resource = translate_resource(str, locale, scope)
35
+ sanitized_locale = RouteTranslator::LocaleSanitizer.sanitize(locale)
36
+ translated_resource = translate_resource(str, sanitized_locale, scope)
37
37
 
38
38
  URI.escape translated_resource
39
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RouteTranslator
4
- VERSION = '5.4.1'.freeze
4
+ VERSION = '5.5.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: route_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.1
4
+ version: 5.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geremia Taglialatela
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-06-01 00:00:00.000000000 Z
13
+ date: 2017-06-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -188,6 +188,8 @@ files:
188
188
  - lib/route_translator/extensions/mapper.rb
189
189
  - lib/route_translator/extensions/route_set.rb
190
190
  - lib/route_translator/host.rb
191
+ - lib/route_translator/host_path_consistency_lambdas.rb
192
+ - lib/route_translator/locale_sanitizer.rb
191
193
  - lib/route_translator/route.rb
192
194
  - lib/route_translator/translator.rb
193
195
  - lib/route_translator/translator/path.rb