From 76d729116859e08ca1830db563f52cc6fd05d6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20Fr=C3=B6hner?= Date: Mon, 6 Nov 2023 14:59:37 +0100 Subject: [PATCH] Allow usage of Rails relative_url_root --- js_from_routes/lib/js_from_routes/generator.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js_from_routes/lib/js_from_routes/generator.rb b/js_from_routes/lib/js_from_routes/generator.rb index 495e052..aba57ea 100644 --- a/js_from_routes/lib/js_from_routes/generator.rb +++ b/js_from_routes/lib/js_from_routes/generator.rb @@ -16,7 +16,7 @@ def initialize(controller, routes, config) @controller, @config = controller, config @routes = routes .uniq { |route| route.requirements.fetch(:action) } - .map { |route| Route.new(route, config.helper_mappings) } + .map { |route| Route.new(route, config.helper_mappings, relative_url_root: config.relative_url_root) } end # Public: Used to check whether the file should be generated again, changes @@ -53,8 +53,9 @@ def basename # Internal: A presenter for an individual Rails action. class Route - def initialize(route, mappings = {}) + def initialize(route, mappings = {}, relative_url_root: "/") @route, @mappings = route, mappings + @relative_url_root = relative_url_root.to_s.gsub(/\/$/, "") end # Public: The `export` setting specified for the action. @@ -69,7 +70,7 @@ def verb # Public: The path for the action. Example: '/users/:id/edit' def path - @route.path.spec.to_s.chomp("(.:format)") + [@relative_url_root, @route.path.spec.to_s.chomp("(.:format)")].compact.join end # Public: The name of the JS helper for the action. Example: 'destroyAll' @@ -181,6 +182,7 @@ def default_config(root) template_path: File.expand_path("template.js.erb", __dir__), template_all_path: File.expand_path("template_all.js.erb", __dir__), template_index_path: File.expand_path("template_index.js.erb", __dir__), + relative_url_root: Rails.configuration.relative_url_root } end