Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of Rails relative_url_root configuration #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)")].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