From 00c1576899fd0b92ba248b0cf7caab87a8a72cbb Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 28 Jul 2023 09:43:42 +0200 Subject: [PATCH] Add deprecation message --- README.md | 7 +++- lib/route_translator.rb | 15 ++++++++- lib/route_translator/route.rb | 2 +- test/integration/i18n_slash_separator_test.rb | 33 +++++++++++++++++++ test/routing_test.rb | 2 +- 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/integration/i18n_slash_separator_test.rb diff --git a/README.md b/README.md index 8cfe13f0..07ed0a4e 100644 --- a/README.md +++ b/README.md @@ -280,8 +280,13 @@ 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` | -| `i18n_use_controller_path` | Use the exact controller path `account/foo` for looking up translations instead of nested keys `account.foo` | `false` | +| `i18n_use_slash_separator` | Use the exact controller path `account/foo` for looking up translations instead of nested keys `account.foo` | `false` | +#### Deprecated options + +- `i18n_use_slash_separator` is deprecated and will be forced to `true` in the next major release of Route Translator. This only + affects application with nested routes. Please set this option to `true` to remove the deprecation message and ensure + to convert nested routes like `people.products` to the new `people/products`. ### Host-based Locale diff --git a/lib/route_translator.rb b/lib/route_translator.rb index 8c840de8..28a4b6ad 100644 --- a/lib/route_translator.rb +++ b/lib/route_translator.rb @@ -21,7 +21,7 @@ module RouteTranslator host_locales: {}, locale_param_key: :locale, locale_segment_proc: false, - i18n_use_controller_path: false + i18n_use_slash_separator: false }.freeze Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys) @@ -35,6 +35,18 @@ def resolve_host_locale_config_conflicts @config.generate_unnamed_unlocalized_routes = false @config.hide_locale = true end + + def check_deprecations + return if @config.i18n_use_slash_separator + + ActiveSupport::Deprecation.warn <<~MSG + `i18n_use_slash_separator` set to `false` is deprecated and will be + removed in the next major release of Route Translator to match + Rails' ActiveRecord nested model syntax. + + More information at https://github.com/enriclluelles/route_translator/pull/285 + MSG + end end module_function @@ -49,6 +61,7 @@ def config yield @config if block_given? resolve_host_locale_config_conflicts if @config.host_locales.present? + check_deprecations @config end diff --git a/lib/route_translator/route.rb b/lib/route_translator/route.rb index 4dcb0951..aa6b445a 100644 --- a/lib/route_translator/route.rb +++ b/lib/route_translator/route.rb @@ -16,7 +16,7 @@ def initialize(route_set, path, name, options_constraints, options, mapping) def scope @scope ||= if mapping.defaults[:controller] - if RouteTranslator.config.i18n_use_controller_path + if RouteTranslator.config.i18n_use_slash_separator %i[routes controllers].push mapping.defaults[:controller] else %i[routes controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym) diff --git a/test/integration/i18n_slash_separator_test.rb b/test/integration/i18n_slash_separator_test.rb new file mode 100644 index 00000000..18ab7cbb --- /dev/null +++ b/test/integration/i18n_slash_separator_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'test_helper' + +class I18nSlashSeparatorTest < ActionDispatch::IntegrationTest + include RouteTranslator::ConfigurationHelper + + def teardown + teardown_config + end + + def test_deprecation_when_default + assert_deprecated('i18n_use_slash_separator') do + RouteTranslator.config + end + end + + def test_deprecation_when_false + config_i18n_use_slash_separator false + + assert_deprecated('i18n_use_slash_separator') do + RouteTranslator.config + end + end + + def test_no_deprecation_when_true + config_i18n_use_slash_separator true + + assert_not_deprecated do + RouteTranslator.config + end + end +end diff --git a/test/routing_test.rb b/test/routing_test.rb index 92a517b4..dcffe8da 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -159,7 +159,7 @@ def test_namespaced_resources end def test_controller_namespaced_resources - config_i18n_use_controller_path(true) + config_i18n_use_slash_separator(true) I18n.default_locale = :es draw_routes do