Skip to content

Commit 025a8a5

Browse files
committed
Simplify Rails patches
1 parent 58b8a2c commit 025a8a5

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

.rubocop.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ AllCops:
1414
- gemfiles/*
1515
- spec/dummy/**/*
1616
- lib/route_translator/core_ext/mapper_patch.rb
17-
- lib/route_translator/core_ext/route_set_patch.rb
1817

1918
Gemspec/RequireMFA:
2019
Enabled: false

lib/route_translator/core_ext/mapper_patch.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,36 @@ def localized_add_route(action, controller, options, _path, to, via, formatted,
6060
# @set.add_route(mapping, as)
6161

6262
# Our override
63-
@set.add_localized_route(@scope, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options, mapping, as, path, @engine)
63+
_add_localized_route(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options, mapping, as, path, @engine)
64+
end
65+
66+
67+
def _add_localized_route(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options, mapping, name, path, engine)
68+
route = RouteTranslator::Route.new(set, path, name, options_constraints, options, mapping)
69+
translator = RouteTranslator.translator_for(engine)
70+
71+
raise RouteTranslator::TranslatorNotFound, "RouteTranslator is not configured for current engine : #{engine}" if translator.nil?
72+
73+
translator.translations_for(route) do |locale, translated_name, translated_path, translated_options_constraints, translated_options|
74+
translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path)
75+
translated_mapping = _translate_mapping(locale, set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
76+
77+
# call original method
78+
set.add_route translated_mapping, translated_name
79+
end
80+
end
81+
82+
83+
def _translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
84+
scope_params = {
85+
blocks: scope[:blocks] || [],
86+
constraints: scope[:constraints] || {},
87+
defaults: (scope[:defaults] || {}).dup,
88+
module: scope[:module],
89+
options: scope[:options] ? scope[:options].merge(translated_options) : translated_options
90+
}
91+
92+
::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options
6493
end
6594

6695
end

lib/route_translator/core_ext/route_set_patch.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

lib/route_translator/railtie.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def add_route_translator_for(engine, opts = {})
2121

2222
initializer 'route_translator.patch' do
2323
ActionDispatch::Routing::Mapper.prepend(RouteTranslator::CoreExt::MapperPatch)
24-
ActionDispatch::Routing::RouteSet.prepend(RouteTranslator::CoreExt::RouteSetPatch)
2524
end
2625

2726
end

0 commit comments

Comments
 (0)