Skip to content

Commit 8cdb32f

Browse files
committed
Add support for Rails 8.1
1 parent 9a93836 commit 8cdb32f

File tree

8 files changed

+131
-1
lines changed

8 files changed

+131
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
- '3.2'
4545
- 'head'
4646
rails:
47+
- rails_8.1
4748
- rails_8.0
4849
- rails_7.2
4950

.rubocop.yml

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

1819
Gemspec/RequireMFA:
1920
Enabled: false

Appraisals

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ end
88
appraise 'rails_8.0' do
99
gem 'rails', '~> 8.0.0'
1010
end
11+
12+
appraise 'rails_8.1' do
13+
gem 'rails', '~> 8.1.0.rc1'
14+
end

gemfiles/rails_8.1.gemfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git"
6+
gem "dbg-rb"
7+
gem "puma"
8+
gem "rake"
9+
gem "rspec-rails"
10+
gem "simplecov"
11+
gem "sqlite3", "~> 2.1.0"
12+
gem "warning"
13+
gem "bar", path: "../spec/dummy/engines/bar"
14+
gem "baz", path: "../spec/dummy/engines/baz"
15+
gem "foo", path: "../spec/dummy/engines/foo"
16+
gem "guard-rspec", require: false
17+
gem "rubocop", require: false
18+
gem "rubocop-performance", require: false
19+
gem "rubocop-rails", require: false
20+
gem "rubocop-rake", require: false
21+
gem "rubocop-rspec", require: false
22+
gem "rubocop-rspec_rails", require: false
23+
gem "rails", "~> 8.1.0.rc1"
24+
25+
gemspec path: "../"

lib/route_translator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ def self.translator_for(engine)
2727
@translators[engine]
2828
end
2929

30+
31+
def self.rails_81?
32+
Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('8.1.0.rc1')
33+
end
34+
3035
end
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# frozen_string_literal: true
2+
3+
module RouteTranslator
4+
module CoreExt
5+
module MapperPatch81
6+
extend ActiveSupport::Concern
7+
8+
URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
9+
private_constant :URI_PARSER
10+
11+
12+
def localized(engine)
13+
@localized = true
14+
@engine = engine
15+
yield
16+
@localized = false
17+
@engine = nil
18+
end
19+
20+
21+
private
22+
23+
24+
def add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping)
25+
return super unless @localized
26+
27+
localized_add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping)
28+
end
29+
30+
31+
def localized_add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping)
32+
# From Rails source
33+
# See: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/routing/mapper.rb#L2195
34+
path = path_for_action(action, _path)
35+
raise ArgumentError, "path is required" if path.blank?
36+
37+
action = action.to_s
38+
39+
default_action = options_action || @scope[:action]
40+
41+
if /^[\w\-\/]+$/.match?(action)
42+
default_action ||= action.tr("-", "_") unless action.include?("/")
43+
else
44+
action = nil
45+
end
46+
47+
as = name_for_action(as, action) if as
48+
path = ActionDispatch::Routing::Mapper::Mapping.normalize_path URI_PARSER.escape(path), formatted
49+
ast = ActionDispatch::Journey::Parser.parse path
50+
51+
mapping = ActionDispatch::Routing::Mapper::Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping)
52+
# End of Rails source
53+
54+
# Original
55+
# @set.add_route(mapping, as)
56+
57+
# Our override
58+
_add_localized_route(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping, mapping, as, path, @engine)
59+
end
60+
61+
62+
def _add_localized_route(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping, mapping, as, path, engine)
63+
route = RouteTranslator::Route.new(set, path, as, options_constraints, options_mapping, mapping)
64+
translator = RouteTranslator.translator_for(engine)
65+
66+
raise RouteTranslator::TranslatorNotFound, "RouteTranslator is not configured for current engine : #{engine}" if translator.nil?
67+
68+
translator.translations_for(route) do |locale, translated_name, translated_path, translated_options_constraints, translated_options|
69+
translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path)
70+
translated_mapping = _translate_mapping(locale, set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor, internal)
71+
72+
# call original method
73+
set.add_route translated_mapping, translated_name
74+
end
75+
end
76+
77+
78+
def _translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor, internal)
79+
scope_params = {
80+
blocks: scope[:blocks] || [],
81+
constraints: scope[:constraints] || {},
82+
defaults: (scope[:defaults] || {}).dup,
83+
module: scope[:module],
84+
options: scope[:options] ? scope[:options].merge(translated_options) : translated_options
85+
}
86+
87+
::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, internal, translated_options
88+
end
89+
90+
end
91+
end
92+
end

lib/route_translator/railtie.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def add_route_translator_for(engine, opts = {})
2020

2121

2222
initializer 'route_translator.patch' do
23-
ActionDispatch::Routing::Mapper.prepend(RouteTranslator::CoreExt::MapperPatch)
23+
mapper_patch = RouteTranslator.rails_81? ? 'RouteTranslator::CoreExt::MapperPatch81' : 'RouteTranslator::CoreExt::MapperPatch'
24+
ActionDispatch::Routing::Mapper.prepend(mapper_patch.constantize)
2425
end
2526

2627
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Start SimpleCov
2424
SimpleCov.start do
2525
formatter SimpleCov::Formatter::MultiFormatter.new([SimpleCov::Formatter::HTMLFormatter, SimpleCov::Formatter::JSONFormatter])
26+
add_filter 'gemfiles/'
2627
add_filter 'spec/'
2728
end
2829

0 commit comments

Comments
 (0)