Skip to content

[feature request] add public method to generate __jsr object #339

@phegman

Description

@phegman

Background

I am customizing the output of js-routes because of some customizations in our routing system and to split the path helpers up into multiple files. I am using JsRoutes::Route to generate the path helper but there is no built in way to generate the __jsr object without also generating the path helpers. I am using this workaround at the moment but it would be cleaner if I could just call a method.

# frozen_string_literal: true

# We want the js-routes JavaScript utils but need to customize the output of the JavaScript path helpers.
# To force js-routes to generate the JavaScript utils but not the JavaScript path helpers
# we can pass a fake `Rails::Application` object with empty `named_routes`.
# We then customize the output of the JavaScript path helpers in `lib/gitlab/js_routes.rb`
class EmptyRoutesApplication
  Routes = Struct.new(:named_routes)

  NamedRoutes = Struct.new do
    def to_h
      {}
    end
  end

  def routes
    Routes.new(NamedRoutes.new)
  end

  def reload_routes_unless_loaded; end

  def reload_routes!; end

  def is_a?(klass)
    klass == Rails::Application
  end
end

JsRoutes.setup do |c|
  c.application = -> do
    EmptyRoutesApplication.new
  end
end

Proposal

Add a public method such as generate_jsr to lib/js_routes/instance.rb. This will allow more custom setups where you can fully customize the output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions