Skip to content

Commit

Permalink
Allow usage of Rails relative_url_root
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias Fröhner committed Nov 6, 2023
1 parent 7e708eb commit 76d7291
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions js_from_routes/lib/js_from_routes/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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'
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 76d7291

Please sign in to comment.