├── .github ├── CODEOWNERS ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .rubocop_todo.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── js_rails_routes.gemspec ├── lib ├── js_rails_routes.rb ├── js_rails_routes │ ├── builder.rb │ ├── configuration.rb │ ├── engine.rb │ ├── generator.rb │ ├── language │ │ ├── base.rb │ │ ├── javascript.rb │ │ └── typescript.rb │ ├── route.rb │ ├── route_set.rb │ └── version.rb └── tasks │ └── js_rails_routes.rake └── spec ├── js_rails_routes ├── builder_spec.rb ├── configuration_spec.rb ├── generator_spec.rb ├── language │ ├── base_spec.rb │ ├── javascript_spec.rb │ └── typescript_spec.rb ├── route_set_spec.rb └── route_spec.rb ├── js_rails_routes_spec.rb ├── spec_helper.rb ├── support ├── matchers │ └── not_change.rb ├── shared_contexts │ └── run_in_a_sandbox.rb └── test_app.rb └── tmp └── .keep /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # package-ecosystem: bundler, directories: / 2 | /Gemfile @increments/shared-dev-group 3 | /Gemfile.lock @increments/shared-dev-group 4 | 5 | # package-ecosystem: github-actions, directories: / 6 | /.github/workflows @increments/shared-dev-group 7 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | 4 | - package-ecosystem: "bundler" 5 | directories: 6 | - "/" 7 | schedule: 8 | interval: "daily" 9 | time: "14:00" 10 | timezone: "Asia/Tokyo" 11 | open-pull-requests-limit: 5 12 | rebase-strategy: "disabled" 13 | 14 | - package-ecosystem: "github-actions" 15 | directories: 16 | - "/" 17 | schedule: 18 | interval: "daily" 19 | time: "14:00" 20 | timezone: "Asia/Tokyo" 21 | open-pull-requests-limit: 5 22 | rebase-strategy: "disabled" 23 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | workflow_dispatch: 9 | 10 | permissions: 11 | contents: read 12 | 13 | jobs: 14 | test: 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | os: ['ubuntu-22.04', 'ubuntu-latest', 'macos-latest'] 19 | ruby: [3.2, 3.3, 3.4] 20 | experimental: [false] 21 | include: 22 | - os: 'ubuntu-latest' 23 | ruby: 'head' 24 | experimental: true 25 | runs-on: ${{ matrix.os }} 26 | continue-on-error: ${{ matrix.experimental }} 27 | steps: 28 | - name: Get branch names 29 | id: branch-name 30 | uses: tj-actions/branch-names@dde14ac574a8b9b1cedc59a1cf312788af43d8d8 # v8.2.1 31 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 32 | - uses: ruby/setup-ruby@13e7a03dc3ac6c3798f4570bfead2aed4d96abfb # v1.244.0 33 | with: 34 | ruby-version: ${{ matrix.ruby }} 35 | bundler-cache: true 36 | - run: | 37 | bundle exec rake 38 | if: matrix.os == 'macos-latest' 39 | - name: Test & publish code coverage 40 | uses: paambaati/codeclimate-action@f429536ee076d758a24705203199548125a28ca7 # v9.0.0 41 | env: 42 | CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} 43 | GIT_BRANCH: ${{ steps.branch-name.outputs.current_branch }} 44 | GIT_COMMIT_SHA: ${{ github.sha }} 45 | with: 46 | coverageCommand: bundle exec rake 47 | if: matrix.os != 'macos-latest' 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /pkg/ 2 | *.gem 3 | Gemfile.lock 4 | spec/examples.txt 5 | coverage 6 | /tmp 7 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | inherit_from: .rubocop_todo.yml 2 | 3 | require: 4 | - rubocop-rspec 5 | 6 | AllCops: 7 | TargetRubyVersion: 2.6 8 | NewCops: enable 9 | 10 | Metrics/LineLength: 11 | Max: 120 12 | 13 | Metrics/BlockLength: 14 | Exclude: 15 | - 'spec/**/*' 16 | 17 | Style/Documentation: 18 | Enabled: false 19 | 20 | Layout/MultilineMethodCallIndentation: 21 | Exclude: 22 | - 'spec/**/*' 23 | 24 | RSpec/ExampleLength: 25 | Enabled: false 26 | 27 | RSpec/MultipleExpectations: 28 | Enabled: false 29 | 30 | RSpec/NamedSubject: 31 | Enabled: false 32 | 33 | RSpec/VerifiedDoubles: 34 | Enabled: false 35 | 36 | RSpec/NestedGroups: 37 | Max: 4 38 | 39 | RSpec/ContextWording: 40 | Prefixes: 41 | - when 42 | - with 43 | - without 44 | - if 45 | - and 46 | 47 | RSpec/FilePath: 48 | Enabled: false 49 | -------------------------------------------------------------------------------- /.rubocop_todo.yml: -------------------------------------------------------------------------------- 1 | # This configuration was generated by 2 | # `rubocop --auto-gen-config` 3 | # on 2022-05-10 07:01:52 UTC using RuboCop version 1.27.0. 4 | # The point is for the user to remove these configuration records 5 | # one by one as the offenses are removed from the code base. 6 | # Note that changes in the inspected code, or installation of new 7 | # versions of RuboCop, may require this file to be generated again. 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This change log adheres to [keepachangelog.com](http://keepachangelog.com). 6 | 7 | ## [Unreleased] 8 | 9 | ## [1.2.0] - 2024-04-19 10 | ### Added 11 | - Add support for parameters as Array 12 | 13 | ## [1.1.0] - 2024-02-09 14 | ### Changed 15 | - Add eslint-disable comment not to affect eslint format 16 | - Update generated scripts to adopt eslint rule 17 | 18 | ## [1.0.0] - 2022-05-13 19 | 20 | ### Changed 21 | - Remove codeclimate and execute rubocop in github actions 22 | - Update corporate name from Increments Inc. to Qiita Inc. 23 | - Update gem authors and emails in gemspec file 24 | - Change ci from travis.ci to github actions 25 | - Drop supporting legacy Ruby versions 26 | - Add supporting new ruby versions 27 | - Depend on rails >= 6.0 28 | 29 | ## [0.10.1] - 2020-03-09 30 | ### Fixed 31 | - Improved TypeScript typing 32 | 33 | ## [0.10.0] - 2018-10-29 34 | ### Added 35 | - Enable to change route's name 36 | 37 | ## [0.9.0] - 2018-08-24 38 | ### Added 39 | - Improved TypeScript params typing support 40 | 41 | ## [0.8.1] - 2018-08-21 42 | ### Fixed 43 | - Camelize `params` keys as `camelize` option 44 | 45 | ## [0.8.0] - 2018-08-21 46 | ### Added 47 | - Support TypeScript 48 | - Add `route_filter` and `route_set_filter` options 49 | 50 | ## [0.7.1] - 2018-07-26 51 | - Refactor whole code base 52 | 53 | ### Fixed 54 | - Enable to math engine name in case-sensitive way. 55 | 56 | ## [0.7.0] - 2018-07-11 57 | ### Added 58 | - Support `camelize` option 59 | 60 | ### Changed 61 | - Drop supporting legacy Ruby versions 62 | 63 | ## [0.6.0] - 2018-07-04 64 | ### Added 65 | - Support Rails engine 66 | 67 | ### Changed 68 | - Replace `path` with `output_dir` 69 | 70 | ## [0.5.0] - 2017-04-08 71 | ### Added 72 | - Support additional parameters 73 | 74 | ## [0.4.0] - 2017-04-05 75 | ### Added 76 | - Add `include_names` and `exclude_names` options 77 | 78 | ### Changed 79 | - Rename `includes` and `excludes` as `include_paths` and `exclude_paths` respectively 80 | 81 | ## [0.3.0] - 2016-06-25 82 | ### Added 83 | - Enable to configure through Ruby interface 84 | 85 | ### Changed 86 | - Depend on rails >= 3.2 87 | - Remove rake command line parameter from js file 88 | 89 | ## [0.2.1] - 2016-06-24 90 | ### Fixed 91 | - Make sure that js file ends with a new line char 92 | 93 | ## [0.2.0] - 2016-06-24 94 | ### Changed 95 | - Rename the task name as "js:routes" 96 | 97 | ## [0.1.0] - 2016-06-24 98 | ### Added 99 | - Implement "js:rails:routes" task 100 | 101 | [Unreleased]: https://github.com/increments/js_rails_routes/compare/v1.2.0...HEAD 102 | [1.2.0]: https://github.com/increments/js_rails_routes/compare/v1.1.0...v1.2.0 103 | [1.1.0]: https://github.com/increments/js_rails_routes/compare/v1.0.0...v1.1.0 104 | [1.0.0]: https://github.com/increments/js_rails_routes/compare/v0.10.1...v1.0.0 105 | [0.10.1]: https://github.com/increments/js_rails_routes/compare/v0.10.0...v0.10.1 106 | [0.10.0]: https://github.com/increments/js_rails_routes/compare/v0.9.0...v0.10.0 107 | [0.9.0]: https://github.com/increments/js_rails_routes/compare/v0.8.1...v0.9.0 108 | [0.8.1]: https://github.com/increments/js_rails_routes/compare/v0.8.0...v0.8.1 109 | [0.8.0]: https://github.com/increments/js_rails_routes/compare/v0.7.1...v0.8.0 110 | [0.7.1]: https://github.com/increments/js_rails_routes/compare/v0.7.0...v0.7.1 111 | [0.7.0]: https://github.com/increments/js_rails_routes/compare/v0.6.0...v0.7.0 112 | [0.6.0]: https://github.com/increments/js_rails_routes/compare/v0.5.0...v0.6.0 113 | [0.5.0]: https://github.com/increments/js_rails_routes/compare/v0.4.0...v0.5.0 114 | [0.4.0]: https://github.com/increments/js_rails_routes/compare/v0.3.0...v0.4.0 115 | [0.3.0]: https://github.com/increments/js_rails_routes/compare/v0.2.1...v0.3.0 116 | [0.2.1]: https://github.com/increments/js_rails_routes/compare/v0.2.0...v0.2.1 117 | [0.2.0]: https://github.com/increments/js_rails_routes/compare/v0.1.0...v0.2.0 118 | [0.1.0]: https://github.com/increments/js_rails_routes/compare/033b945...v0.1.0 119 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | gemspec 5 | 6 | gem 'pry' 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Qiita Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rake js:routes 2 | 3 | [![Gem](https://img.shields.io/gem/v/js_rails_routes.svg?maxAge=2592000)](https://rubygems.org/gems/js_rails_routes) 4 | ![Build Status](https://github.com/increments/js_rails_routes/actions/workflows/test.yml/badge.svg?branch=master) 5 | [![Code Climate](https://codeclimate.com/github/increments/js_rails_routes/badges/gpa.svg)](https://codeclimate.com/github/increments/js_rails_routes) 6 | [![Test Coverage](https://codeclimate.com/github/increments/js_rails_routes/badges/coverage.svg)](https://codeclimate.com/github/increments/js_rails_routes/coverage) 7 | [![license](https://img.shields.io/github/license/increments/js_rails_routes.svg?maxAge=2592000)](https://github.com/increments/js_rails_routes/blob/master/LICENSE) 8 | 9 | Generate a ES6 module that contains Rails routes. 10 | 11 | ## Description 12 | 13 | This gem provides "js:routes" rake task. 14 | It generates a ES6 requirable module which exports url helper functions defined in your Rails application. 15 | 16 | Suppose the app has following routes: 17 | 18 | ```rb 19 | # == Route Map 20 | # 21 | # Prefix Verb URI Pattern Controller#Action 22 | # articles GET /articles(.:format) articles#index 23 | # POST /articles(.:format) articles#create 24 | # new_article GET /articles/new(.:format) articles#new 25 | # edit_article GET /articles/:id/edit(.:format) articles#edit 26 | # article GET /articles/:id(.:format) articles#show 27 | # PATCH /articles/:id(.:format) articles#update 28 | # PUT /articles/:id(.:format) articles#update 29 | # DELETE /articles/:id(.:format) articles#destroy 30 | Rails.application.routes.draw do 31 | resources :articles 32 | end 33 | ``` 34 | 35 | then `rake js:routes` generates "app/assets/javascripts/rails-routes.js" as: 36 | 37 | ```js 38 | // Don't edit manually. `rake js:routes` generates this file. 39 | function process(route, params, keys) { 40 | var query = []; 41 | for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) { 42 | if (keys.indexOf(param) === -1) { 43 | query.push(param + "=" + encodeURIComponent(params[param])); 44 | } 45 | } 46 | return query.length ? route + "?" + query.join("&") : route; 47 | } 48 | 49 | export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); } 50 | export function articles_path(params) { return process('/articles', params, []); } 51 | export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); } 52 | export function new_article_path(params) { return process('/articles/new', params, []); } 53 | ``` 54 | 55 | ## VS. 56 | 57 | [railsware/js-routes](https://github.com/railsware/js-routes) spreads url helpers via global variable. 58 | 59 | This gem uses ES6 modules. 60 | 61 | ## Requirement 62 | 63 | - Rails >= 3.2 64 | 65 | ## Usage 66 | 67 | Generate routes file. 68 | 69 | ```bash 70 | rake js:routes 71 | ``` 72 | 73 | ### Configuration 74 | 75 | JSRailsRoutes supports several parameters: 76 | 77 | Name | Type | Description | Default 78 | -------------------|-----------|---------------------------------------------------------------------------------------|---------------------------------------- 79 | `include_paths` | `Regexp` | Paths match to the regexp are included | `/.*/` 80 | `exclude_paths` | `Regexp` | Paths match to the regexp are excluded | `/^$/` 81 | `include_names` | `Regexp` | Names match to the regexp are included | `/.*/` 82 | `exclude_names` | `Regexp` | Names match to the regexp are excluded | `/^$/` 83 | `exclude_engines` | `Regexp` | Rails engines match to the regexp are excluded | `/^$/` 84 | `output_dir` | `String` | Output JS file into the specified directory | `Rails.root.join("app", "assets", "javascripts")` 85 | `camelize` | `Symbol` | Output JS file with chosen camelcase type it also avaliable for `:lower` and `:upper` | `nil` 86 | `target` | `String` | Target type. `"js"` or `"ts"` | `"js"` 87 | `route_filter` | `Proc` | Fully customizable filter on `JSRails::Route` | `->(route) { true }` 88 | `route_set_filter` | `Proc` | Fully customizable filter on `JSRails::RouteSet` | `->(route_set) { true }` 89 | 90 | You can configure via `JSRailsRoutes.configure`. 91 | 92 | ```rb 93 | # Rakefile 94 | JSRailsRoutes.configure do |c| 95 | c.exclude_paths = %r{^/(rails|sidekiq)} 96 | c.output_dir = Rails.root.join('client/javascripts') 97 | end 98 | ``` 99 | 100 | Now `rake js:routes` ignores paths starting with "/rails" or "/sidekiq". 101 | 102 | ### Command line parameters 103 | 104 | You can override the coniguration via command line parameters: 105 | 106 | ```bash 107 | rake js:routes exclude_paths='^/rails' 108 | ``` 109 | 110 | The command still ignores "/rails" but includes "/sidekiq". 111 | 112 | ### Rename route 113 | 114 | You can rename route in `route_filter`: 115 | 116 | ```rb 117 | # Rakefile 118 | JSRailsRoutes.configure do |c| 119 | c.route_filter = -> (route) do 120 | # Remove common prefix if route's name starts with it. 121 | route.name = route.name[4..-1] if route.name.start_with?('foo_') 122 | true 123 | end 124 | end 125 | ``` 126 | 127 | ## Install 128 | 129 | Your Rails Gemfile: 130 | 131 | ```rb 132 | gem 'js_rails_routes', group: :development 133 | ``` 134 | 135 | ## License 136 | 137 | [MIT](https://github.com/increments/js_rails_routes/blob/master/LICENSE) 138 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'bundler/gem_tasks' 4 | require 'rspec/core/rake_task' 5 | require 'rubocop/rake_task' 6 | 7 | RSpec::Core::RakeTask.new(:spec) 8 | RuboCop::RakeTask.new 9 | 10 | task default: %i[rubocop spec] 11 | -------------------------------------------------------------------------------- /js_rails_routes.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH << File.expand_path('lib', __dir__) 4 | 5 | require 'js_rails_routes/version' 6 | 7 | Gem::Specification.new do |spec| 8 | spec.name = 'js_rails_routes' 9 | spec.version = JSRailsRoutes::VERSION 10 | spec.authors = ['Qiita Inc.'] 11 | spec.email = ['engineers@qiita.com'] 12 | spec.summary = 'Generate a ES6 module that contains Rails routes.' 13 | spec.homepage = 'https://github.com/increments/js_rails_routes' 14 | spec.license = 'MIT' 15 | spec.files = `git ls-files -z`.split("\x0") 16 | spec.require_paths = ['lib'] 17 | spec.required_ruby_version = '>= 2.6.0' 18 | 19 | spec.add_dependency 'rails', '>= 6.0' 20 | spec.add_development_dependency 'bundler', '>= 1.16' 21 | spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0' 22 | spec.add_development_dependency 'rake', '~> 12.3' 23 | spec.add_development_dependency 'rspec', '~> 3.8' 24 | spec.add_development_dependency 'rubocop', '~> 1.27.0' 25 | spec.add_development_dependency 'rubocop-rspec', '~> 2.0' 26 | spec.add_development_dependency 'simplecov', '~> 0.16.1', '!= 0.18.0', '!= 0.18.1', '!= 0.18.2', '!= 0.18.3', '!= 0.18.4', '!= 0.18.5', '!= 0.19.0', '!= 0.19.1' # rubocop:disable Metrics/LineLength 27 | spec.metadata['rubygems_mfa_required'] = 'true' 28 | end 29 | -------------------------------------------------------------------------------- /lib/js_rails_routes.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/engine' 4 | require 'js_rails_routes/configuration' 5 | require 'js_rails_routes/generator' 6 | require 'js_rails_routes/version' 7 | require 'js_rails_routes/language/javascript' 8 | require 'js_rails_routes/language/typescript' 9 | 10 | module JSRailsRoutes 11 | PARAM_REGEXP = %r{:(.*?)(/|$)}.freeze 12 | 13 | module_function 14 | 15 | # @yield [Configuration] 16 | def configure 17 | yield config if block_given? 18 | end 19 | 20 | # Current configuration. 21 | # 22 | # @return [Configuration] 23 | def config 24 | @config ||= Configuration.new 25 | end 26 | 27 | # @param task [String] 28 | def generate(task) 29 | builder = Builder.new(JSRailsRoutes.language) 30 | Generator.new(builder).generate(task) 31 | end 32 | 33 | # Execute a given block within a new sandbox. For test purpose. 34 | # 35 | # @yield 36 | def sandbox 37 | raise 'Already in a sandbox' if @sandbox 38 | 39 | @sandbox = true 40 | prev = @config 41 | @config = Configuration.new 42 | begin 43 | yield if block_given? 44 | ensure 45 | @config = prev 46 | @sandbox = nil 47 | end 48 | end 49 | 50 | # @return [JSRailsRoutes::Language::Base] 51 | def language 52 | case config.target 53 | when 'js' 54 | Language::JavaScript.new 55 | when 'ts' 56 | Language::TypeScript.new 57 | else 58 | raise NotImplementedError, config.target 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /lib/js_rails_routes/builder.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/route_set' 4 | 5 | module JSRailsRoutes 6 | class Builder 7 | Artifact = Struct.new(:engine_name, :ext, :body) do 8 | # @return [String] 9 | def file_name 10 | "#{engine_name.gsub('::Engine', '').underscore.tr('/', '-')}-routes.#{ext}" 11 | end 12 | end 13 | 14 | # @return [Array] 15 | attr_reader :route_set_list 16 | 17 | # @return [JSRailsRoutes::Language::Base] 18 | attr_reader :language 19 | 20 | # @param language [JSRailsRoutes::Language::Base] 21 | # @param route_set_list [Array] 22 | def initialize(language, route_set_list = RouteSet.correct_matching_route_set_list) 23 | @language = language 24 | @route_set_list = route_set_list 25 | end 26 | 27 | # @return [Array] 28 | def build 29 | route_set_list.map do |route_set| 30 | Artifact.new(route_set.name, language.ext, language.handle_route_set(route_set)) 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/js_rails_routes/configuration.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module JSRailsRoutes 4 | class Configuration 5 | attr_accessor :include_paths, 6 | :exclude_paths, 7 | :include_names, 8 | :exclude_names, 9 | :exclude_engines, 10 | :output_dir, 11 | :camelize, 12 | :target, 13 | :route_filter, 14 | :route_set_filter 15 | 16 | def initialize # rubocop:disable Metrics/AbcSize 17 | self.include_paths = /.*/ 18 | self.exclude_paths = /^$/ 19 | self.include_names = /.*/ 20 | self.exclude_names = /^$/ 21 | self.exclude_engines = /^$/ 22 | self.camelize = nil 23 | self.output_dir = Rails.root.join('app', 'assets', 'javascripts') 24 | self.target = 'js' 25 | self.route_filter = ->(_route) { true } 26 | self.route_set_filter = ->(_route_set) { true } 27 | end 28 | 29 | # @param env [Hash{String=>String}] 30 | def configure_with_env_vars(env = ENV) # rubocop:disable Metrics/AbcSize 31 | %w[include_paths exclude_paths include_names exclude_names exclude_engines].each do |name| 32 | public_send("#{name}=", Regexp.new(env[name])) if env[name] 33 | end 34 | self.output_dir = env['output_dir'] if env['output_dir'] 35 | self.camelize = env['camelize'].presence.to_sym if env['camelize'] 36 | self.target = env['target'] if env['target'] 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/js_rails_routes/engine.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rails' 4 | 5 | module JSRailsRoutes 6 | class Engine < ::Rails::Engine 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /lib/js_rails_routes/generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/route_set' 4 | require 'js_rails_routes/builder' 5 | 6 | module JSRailsRoutes 7 | class Generator 8 | # @param builder [JSRailsRoutes::Builder] 9 | # @param writable [#write] 10 | def initialize(builder, writable: File) 11 | @builder = builder 12 | @writable = writable 13 | end 14 | 15 | # @param task [String] 16 | # @return [Hash{String => String}] 17 | def generate(task) 18 | builder.build.each do |artifact| 19 | file_name = File.join(config.output_dir, artifact.file_name) 20 | file_body = <<~FILE_BODY.chomp 21 | /* eslint-disable */ 22 | // Don't edit manually. `rake #{task}` generates this file. 23 | #{artifact.body} 24 | FILE_BODY 25 | writable.write(file_name, file_body) 26 | end 27 | end 28 | 29 | private 30 | 31 | attr_reader :writable, :builder 32 | 33 | # @return [JSRailsRoutes::Configuration] 34 | def config 35 | JSRailsRoutes.config 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/js_rails_routes/language/base.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module JSRailsRoutes 4 | module Language 5 | class Base 6 | # @param routes [JSRailsRoutes::RouteSet] 7 | # @return [String] 8 | def handle_route_set(routes) 9 | raise NotImplementedError 10 | end 11 | 12 | # @return [String] 13 | def ext 14 | raise NotImplementedError 15 | end 16 | 17 | private 18 | 19 | # @return [JSRailsRoutes::Configuration] 20 | def config 21 | JSRailsRoutes.config 22 | end 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/js_rails_routes/language/javascript.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/route' 4 | require 'js_rails_routes/language/base' 5 | 6 | module JSRailsRoutes 7 | module Language 8 | class JavaScript < Base 9 | PROCESS_FUNC = <<~JAVASCRIPT 10 | function process(route, params, keys) { 11 | var query = []; 12 | for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) { 13 | if (keys.indexOf(param) === -1) { 14 | if (Array.isArray(params[param])) { 15 | for (var value of params[param]) { 16 | query.push(param + "[]=" + encodeURIComponent(value)); 17 | } 18 | } else { 19 | query.push(param + "=" + encodeURIComponent(params[param])); 20 | } 21 | } 22 | } 23 | return query.length ? route + "?" + query.join("&") : route; 24 | } 25 | JAVASCRIPT 26 | 27 | # @note Implementation for {JSRailsRoutes::Language::Base#generate} 28 | def handle_route_set(routes) 29 | set = routes.each_with_object([self.class::PROCESS_FUNC]) do |route, lines| 30 | lines.push(handle_route(route)) 31 | end.join("\n") 32 | "#{set}\n" 33 | end 34 | 35 | # @param route [JSRailsRoutes::Route] 36 | # @return [String] 37 | def handle_route(route) 38 | path, keys = parse(route.path) 39 | name = function_name(route.name) 40 | "export function #{name}(params) { return process('#{path}', params, [#{keys.join(',')}]); }" 41 | end 42 | 43 | # @note Implementation for {JSRailsRoutes::Language::Base#ext} 44 | def ext 45 | 'js' 46 | end 47 | 48 | private 49 | 50 | # @param route_path [String] 51 | # @return [Array<(String, Array)>] 52 | def parse(route_path) 53 | destructured_path = route_path.dup 54 | keys = [] 55 | while destructured_path =~ JSRailsRoutes::PARAM_REGEXP 56 | key = camelize(Regexp.last_match(1)) 57 | keys.push("'#{key}'") 58 | destructured_path.sub!(JSRailsRoutes::PARAM_REGEXP, "' + params.#{key} + '#{Regexp.last_match(2)}") 59 | end 60 | [destructured_path, keys] 61 | end 62 | 63 | # @param route_name [String] 64 | # @return [String] 65 | def function_name(route_name) 66 | url_helper_name = "#{route_name}_path" 67 | config.camelize.nil? ? url_helper_name : url_helper_name.camelize(config.camelize) 68 | end 69 | 70 | # @param name [String] 71 | # @return [String] 72 | def camelize(name) 73 | config.camelize ? name.camelize(config.camelize) : name 74 | end 75 | 76 | # @return [JSRailsRoutes::Configuration] 77 | def config 78 | JSRailsRoutes.config 79 | end 80 | end 81 | end 82 | end 83 | -------------------------------------------------------------------------------- /lib/js_rails_routes/language/typescript.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/route' 4 | require 'js_rails_routes/language/javascript' 5 | 6 | module JSRailsRoutes 7 | module Language 8 | class TypeScript < JavaScript 9 | PROCESS_FUNC = <<~TYPESCRIPT 10 | type Value = string | number | (string | number)[]; 11 | type Params = { [key in Keys]: Value } & Record 12 | function process(route: string, params: Record | undefined, keys: string[]): string { 13 | if (!params) return route 14 | var query: string[] = []; 15 | for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) { 16 | if (keys.indexOf(param) === -1) { 17 | if (Array.isArray(params[param])) { 18 | for (var value of params[param] as (string | number)[]) { 19 | query.push(param + "[]=" + encodeURIComponent(value.toString())); 20 | } 21 | } else { 22 | query.push(param + "=" + encodeURIComponent(params[param].toString())); 23 | } 24 | } 25 | } 26 | return query.length ? route + "?" + query.join("&") : route; 27 | } 28 | TYPESCRIPT 29 | 30 | # @param route [JSRailsRoutes::Route] 31 | # @return [String] 32 | def handle_route(route) 33 | path, keys = parse(route.path) 34 | name = function_name(route.name) 35 | params = keys.empty? ? 'params?: Record' : "params: Params<#{keys.join(' | ')}>" 36 | "export function #{name}(#{params}) { return process('#{path}', params, [#{keys.join(',')}]); }" 37 | end 38 | 39 | # @note Implementation for {JSRailsRoutes::Language::Base#ext} 40 | def ext 41 | 'ts' 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/js_rails_routes/route.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module JSRailsRoutes 4 | # Encapsulate a single routing rule 5 | class Route 6 | # @return [String] route name. It becomes JavaScript function name. 7 | attr_accessor :name 8 | 9 | # @return [String] 10 | attr_reader :path 11 | 12 | # @return [ActionDispatch::Journey::Route] 13 | attr_reader :route 14 | 15 | # @param route [ActionDispatch::Journey::Route] 16 | def initialize(route) 17 | @route = route 18 | @name = route.name 19 | @path = route.path.spec.to_s.split('(').first 20 | end 21 | 22 | # @return [Boolean] 23 | def match? # rubocop:disable Metrics/AbcSize 24 | return false if config.include_paths !~ path 25 | return false if config.exclude_paths =~ path 26 | return false if config.include_names !~ name 27 | return false if config.exclude_names =~ name 28 | 29 | config.route_filter.call(self) 30 | end 31 | 32 | private 33 | 34 | # @return [JSRailsRoutes::Configuration] 35 | def config 36 | JSRailsRoutes.config 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/js_rails_routes/route_set.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'js_rails_routes/route' 4 | 5 | module JSRailsRoutes 6 | # Encapsulate a set of routes 7 | class RouteSet 8 | include ::Enumerable 9 | 10 | # @return [Array] 11 | def self.correct_matching_route_set_list 12 | [ 13 | RouteSet.new('Rails', ::Rails.application.routes), 14 | ::Rails::Engine.subclasses.map do |engine| 15 | RouteSet.new(engine.name, engine.routes) 16 | end 17 | ].flatten.select(&:match?) 18 | end 19 | 20 | # @!method each 21 | # @yield [JSRailsRoutes::Route>] 22 | # @note Implementation for {Enumerable} 23 | delegate :each, to: :routes 24 | 25 | # @return [String] 26 | attr_reader :name 27 | 28 | # @return [ActionDispatch::Routing::RouteSet] 29 | attr_reader :route_set 30 | 31 | # @return [Array] 32 | attr_reader :routes 33 | 34 | # @param name [String] engine name 35 | # @param routes [ActionDispatch::Routing::RouteSet] 36 | def initialize(name, routes) 37 | @name = name 38 | @route_set = route_set 39 | @routes = routes.routes 40 | .select(&:name) 41 | .map { |route| Route.new(route) } 42 | .select(&:match?) 43 | end 44 | 45 | # @return [Boolean] 46 | def match? 47 | name !~ config.exclude_engines && routes.present? && config.route_set_filter.call(self) 48 | end 49 | 50 | private 51 | 52 | # @return [JSRailsRoutes::Configuration] 53 | def config 54 | JSRailsRoutes.config 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/js_rails_routes/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module JSRailsRoutes 4 | VERSION = '1.2.0' 5 | end 6 | -------------------------------------------------------------------------------- /lib/tasks/js_rails_routes.rake: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | desc 'Generate a ES6 module that contains Rails routes' 4 | namespace :js do 5 | task routes: :environment do |task| 6 | JSRailsRoutes.config.configure_with_env_vars 7 | JSRailsRoutes.generate(task) 8 | puts "Routes saved into #{JSRailsRoutes.config.output_dir}." 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /spec/js_rails_routes/builder_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Builder do 4 | subject(:builder) { described_class.new(language, route_set_list) } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | let(:language) { instance_double(JSRailsRoutes::Language::Base, handle_route_set: body, ext: %w[js ts].sample) } 9 | let(:body) { 'hello' } 10 | let(:route_set_list) { [rails_route_set, engine_route_set] } 11 | 12 | let(:rails_route_set) do 13 | route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes| 14 | routes.draw do 15 | get '/articles' => 'articles#index' 16 | end 17 | end 18 | JSRailsRoutes::RouteSet.new('Rails', route_set) 19 | end 20 | 21 | let(:engine_route_set) do 22 | route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes| 23 | routes.draw do 24 | get '/users' => 'users#index' 25 | end 26 | end 27 | JSRailsRoutes::RouteSet.new('Users::Engine', route_set) 28 | end 29 | 30 | describe '#build' do 31 | subject { builder.build } 32 | 33 | it 'returns an array of artifacts' do 34 | expect(subject).to contain_exactly( 35 | an_object_having_attributes(engine_name: rails_route_set.name, body: body), 36 | an_object_having_attributes(engine_name: engine_route_set.name, body: body) 37 | ) 38 | expect(language).to have_received(:handle_route_set).twice 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /spec/js_rails_routes/configuration_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Configuration do 4 | subject(:config) { described_class.new } 5 | 6 | describe '#configure_with_env_vars' do 7 | subject { config.configure_with_env_vars(env) } 8 | 9 | context 'with empty env' do 10 | let(:env) { {} } 11 | 12 | it 'does not change' do 13 | expect { subject }.to not_change(config, :include_paths) 14 | .and not_change(config, :exclude_paths) 15 | .and not_change(config, :include_names) 16 | .and not_change(config, :exclude_names) 17 | .and not_change(config, :exclude_engines) 18 | .and not_change(config, :output_dir) 19 | .and not_change(config, :camelize) 20 | end 21 | end 22 | 23 | context 'with include_paths env' do 24 | let(:env) { { 'include_paths' => 'a' } } 25 | 26 | it 'changes #include_paths' do 27 | expect { subject }.to change(config, :include_paths).to eq(/a/) 28 | end 29 | end 30 | 31 | context 'with exclude_paths env' do 32 | let(:env) { { 'exclude_paths' => 'a' } } 33 | 34 | it 'changes #exclude_paths' do 35 | expect { subject }.to change(config, :exclude_paths).to eq(/a/) 36 | end 37 | end 38 | 39 | context 'with include_names env' do 40 | let(:env) { { 'include_names' => 'a' } } 41 | 42 | it 'changes #include_names' do 43 | expect { subject }.to change(config, :include_names).to eq(/a/) 44 | end 45 | end 46 | 47 | context 'with exclude_names env' do 48 | let(:env) { { 'exclude_names' => 'a' } } 49 | 50 | it 'changes #exclude_names' do 51 | expect { subject }.to change(config, :exclude_names).to eq(/a/) 52 | end 53 | end 54 | 55 | context 'with exclude_engines env' do 56 | let(:env) { { 'exclude_engines' => 'a' } } 57 | 58 | it 'changes #exclude_engines' do 59 | expect { subject }.to change(config, :exclude_engines).to eq(/a/) 60 | end 61 | end 62 | 63 | context 'with output_dir env' do 64 | let(:env) { { 'output_dir' => 'path' } } 65 | 66 | it 'changes #output_dir' do 67 | expect { subject }.to change(config, :output_dir).to eq 'path' 68 | end 69 | end 70 | 71 | context 'with camelize env' do 72 | let(:env) { { 'camelize' => 'lower' } } 73 | 74 | it 'changes #camelize' do 75 | expect { subject }.to change(config, :camelize).to eq :lower 76 | end 77 | end 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /spec/js_rails_routes/generator_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Generator do 4 | subject(:generator) { described_class.new(builder, writable: writable) } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | let(:writable) { spy('writable') } 9 | let(:builder) { double('builder', build: result) } 10 | let(:result) do 11 | [ 12 | JSRailsRoutes::Builder::Artifact.new('Rails', 'js', 'rails body'), 13 | JSRailsRoutes::Builder::Artifact.new('Admin::Engine', 'js', 'admin body') 14 | ] 15 | end 16 | 17 | describe '#generate' do 18 | subject { generator.generate(task) } 19 | 20 | let(:task) { 'js:routes' } 21 | 22 | it 'writes with path to file and its contents' do 23 | allow(writable).to receive(:write) 24 | subject 25 | expect(writable).to have_received(:write).with( 26 | a_string_ending_with('app/assets/javascripts/rails-routes.js'), 27 | a_string_including('rails body') 28 | ).ordered 29 | expect(writable).to have_received(:write).with( 30 | a_string_ending_with('app/assets/javascripts/admin-routes.js'), 31 | a_string_including('admin body') 32 | ).ordered 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /spec/js_rails_routes/language/base_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Language::Base do 4 | subject(:language) { described_class.new } 5 | 6 | describe '#handle_route_set' do 7 | subject { language.handle_route_set(double('route set')) } 8 | 9 | it { expect { subject }.to raise_error(NotImplementedError) } 10 | end 11 | 12 | describe '#ext' do 13 | subject { language.ext } 14 | 15 | it { expect { subject }.to raise_error(NotImplementedError) } 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /spec/js_rails_routes/language/javascript_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Language::JavaScript do 4 | subject(:language) { described_class.new } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | describe '::PROCESS_FUNC' do 9 | subject { described_class::PROCESS_FUNC } 10 | 11 | it 'returns a javascript function' do 12 | expect(subject).to eq <<~JAVASCRIPT 13 | function process(route, params, keys) { 14 | var query = []; 15 | for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) { 16 | if (keys.indexOf(param) === -1) { 17 | if (Array.isArray(params[param])) { 18 | for (var value of params[param]) { 19 | query.push(param + "[]=" + encodeURIComponent(value)); 20 | } 21 | } else { 22 | query.push(param + "=" + encodeURIComponent(params[param])); 23 | } 24 | } 25 | } 26 | return query.length ? route + "?" + query.join("&") : route; 27 | } 28 | JAVASCRIPT 29 | end 30 | end 31 | 32 | describe '#handle_route_set' do 33 | subject { language.handle_route_set(route_set) } 34 | 35 | let(:route_set) do 36 | rails_route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes| 37 | routes.draw do 38 | resources :articles 39 | end 40 | end 41 | JSRailsRoutes::RouteSet.new('Rails', rails_route_set) 42 | end 43 | 44 | context 'without camelize option' do 45 | it 'returns a javascript with snake_case functions' do 46 | expect(subject).to eq <<~JAVASCRIPT 47 | #{described_class::PROCESS_FUNC} 48 | export function articles_path(params) { return process('/articles', params, []); } 49 | export function new_article_path(params) { return process('/articles/new', params, []); } 50 | export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); } 51 | export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); } 52 | JAVASCRIPT 53 | end 54 | end 55 | 56 | context 'with camelize = :lower option' do 57 | before do 58 | JSRailsRoutes.configure do |c| 59 | c.camelize = :lower 60 | end 61 | end 62 | 63 | it 'returns a javascript with lowerCamelCase functions' do 64 | expect(subject).to eq <<~JAVASCRIPT 65 | #{described_class::PROCESS_FUNC} 66 | export function articlesPath(params) { return process('/articles', params, []); } 67 | export function newArticlePath(params) { return process('/articles/new', params, []); } 68 | export function editArticlePath(params) { return process('/articles/' + params.id + '/edit', params, ['id']); } 69 | export function articlePath(params) { return process('/articles/' + params.id + '', params, ['id']); } 70 | JAVASCRIPT 71 | end 72 | end 73 | 74 | context 'with camelize = :upper option' do 75 | before do 76 | JSRailsRoutes.configure do |c| 77 | c.camelize = :upper 78 | end 79 | end 80 | 81 | it 'returns a javascript with UpperCamelCase functions' do 82 | expect(subject).to eq <<~JAVASCRIPT 83 | #{described_class::PROCESS_FUNC} 84 | export function ArticlesPath(params) { return process('/articles', params, []); } 85 | export function NewArticlePath(params) { return process('/articles/new', params, []); } 86 | export function EditArticlePath(params) { return process('/articles/' + params.Id + '/edit', params, ['Id']); } 87 | export function ArticlePath(params) { return process('/articles/' + params.Id + '', params, ['Id']); } 88 | JAVASCRIPT 89 | end 90 | end 91 | 92 | context 'with include_paths option' do 93 | before do 94 | JSRailsRoutes.configure do |c| 95 | c.include_paths = /new/ 96 | end 97 | end 98 | 99 | it 'returns a javascript matching to the regexp' do 100 | expect(subject).to eq <<~JAVASCRIPT 101 | #{described_class::PROCESS_FUNC} 102 | export function new_article_path(params) { return process('/articles/new', params, []); } 103 | JAVASCRIPT 104 | end 105 | end 106 | 107 | context 'with exclude_paths option' do 108 | before do 109 | JSRailsRoutes.configure do |c| 110 | c.exclude_paths = /new/ 111 | end 112 | end 113 | 114 | it 'returns a javascript not matching to the regexp' do 115 | expect(subject).to eq <<~JAVASCRIPT 116 | #{described_class::PROCESS_FUNC} 117 | export function articles_path(params) { return process('/articles', params, []); } 118 | export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); } 119 | export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); } 120 | JAVASCRIPT 121 | end 122 | end 123 | 124 | context 'with include_names option' do 125 | before do 126 | JSRailsRoutes.configure do |c| 127 | c.include_names = /new/ 128 | end 129 | end 130 | 131 | it 'returns a javascript matching to the regexp' do 132 | expect(subject).to eq <<~JAVASCRIPT 133 | #{described_class::PROCESS_FUNC} 134 | export function new_article_path(params) { return process('/articles/new', params, []); } 135 | JAVASCRIPT 136 | end 137 | end 138 | 139 | context 'with exclude_names option' do 140 | before do 141 | JSRailsRoutes.configure do |c| 142 | c.exclude_names = /new/ 143 | end 144 | end 145 | 146 | it 'returns a javascript not matching to the regexp' do 147 | expect(subject).to eq <<~JAVASCRIPT 148 | #{described_class::PROCESS_FUNC} 149 | export function articles_path(params) { return process('/articles', params, []); } 150 | export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); } 151 | export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); } 152 | JAVASCRIPT 153 | end 154 | end 155 | end 156 | 157 | describe '#ext' do 158 | subject { language.ext } 159 | 160 | it { is_expected.to eq 'js' } 161 | end 162 | end 163 | -------------------------------------------------------------------------------- /spec/js_rails_routes/language/typescript_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Language::TypeScript do 4 | subject(:language) { described_class.new } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | describe '::PROCESS_FUNC' do 9 | subject { described_class::PROCESS_FUNC } 10 | 11 | it 'returns a typescript function' do 12 | expect(subject).to eq <<~TYPESCRIPT 13 | type Value = string | number | (string | number)[]; 14 | type Params = { [key in Keys]: Value } & Record 15 | function process(route: string, params: Record | undefined, keys: string[]): string { 16 | if (!params) return route 17 | var query: string[] = []; 18 | for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) { 19 | if (keys.indexOf(param) === -1) { 20 | if (Array.isArray(params[param])) { 21 | for (var value of params[param] as (string | number)[]) { 22 | query.push(param + "[]=" + encodeURIComponent(value.toString())); 23 | } 24 | } else { 25 | query.push(param + "=" + encodeURIComponent(params[param].toString())); 26 | } 27 | } 28 | } 29 | return query.length ? route + "?" + query.join("&") : route; 30 | } 31 | TYPESCRIPT 32 | end 33 | end 34 | 35 | describe '#handle_route_set' do 36 | subject { language.handle_route_set(route_set) } 37 | 38 | let(:route_set) do 39 | rails_route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes| 40 | routes.draw do 41 | resources :articles 42 | end 43 | end 44 | JSRailsRoutes::RouteSet.new('Rails', rails_route_set) 45 | end 46 | 47 | context 'without camelize option' do 48 | it 'returns a typescript with snake_case functions' do 49 | expect(subject).to eq <<~TYPESCRIPT 50 | #{described_class::PROCESS_FUNC} 51 | export function articles_path(params?: Record) { return process('/articles', params, []); } 52 | export function new_article_path(params?: Record) { return process('/articles/new', params, []); } 53 | export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); } 54 | export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); } 55 | TYPESCRIPT 56 | end 57 | end 58 | 59 | context 'with camelize = :lower option' do 60 | before do 61 | JSRailsRoutes.configure do |c| 62 | c.camelize = :lower 63 | end 64 | end 65 | 66 | it 'returns a javascript with lowerCamelCase functions' do 67 | expect(subject).to eq <<~TYPESCRIPT 68 | #{described_class::PROCESS_FUNC} 69 | export function articlesPath(params?: Record) { return process('/articles', params, []); } 70 | export function newArticlePath(params?: Record) { return process('/articles/new', params, []); } 71 | export function editArticlePath(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); } 72 | export function articlePath(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); } 73 | TYPESCRIPT 74 | end 75 | end 76 | 77 | context 'with camelize = :upper option' do 78 | before do 79 | JSRailsRoutes.configure do |c| 80 | c.camelize = :upper 81 | end 82 | end 83 | 84 | it 'returns a javascript with UpperCamelCase functions' do 85 | expect(subject).to eq <<~TYPESCRIPT 86 | #{described_class::PROCESS_FUNC} 87 | export function ArticlesPath(params?: Record) { return process('/articles', params, []); } 88 | export function NewArticlePath(params?: Record) { return process('/articles/new', params, []); } 89 | export function EditArticlePath(params: Params<'Id'>) { return process('/articles/' + params.Id + '/edit', params, ['Id']); } 90 | export function ArticlePath(params: Params<'Id'>) { return process('/articles/' + params.Id + '', params, ['Id']); } 91 | TYPESCRIPT 92 | end 93 | end 94 | 95 | context 'with include_paths option' do 96 | before do 97 | JSRailsRoutes.configure do |c| 98 | c.include_paths = /new/ 99 | end 100 | end 101 | 102 | it 'returns a javascript matching to the regexp' do 103 | expect(subject).to eq <<~TYPESCRIPT 104 | #{described_class::PROCESS_FUNC} 105 | export function new_article_path(params?: Record) { return process('/articles/new', params, []); } 106 | TYPESCRIPT 107 | end 108 | end 109 | 110 | context 'with exclude_paths option' do 111 | before do 112 | JSRailsRoutes.configure do |c| 113 | c.exclude_paths = /new/ 114 | end 115 | end 116 | 117 | it 'returns a javascript not matching to the regexp' do 118 | expect(subject).to eq <<~TYPESCRIPT 119 | #{described_class::PROCESS_FUNC} 120 | export function articles_path(params?: Record) { return process('/articles', params, []); } 121 | export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); } 122 | export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); } 123 | TYPESCRIPT 124 | end 125 | end 126 | 127 | context 'with include_names option' do 128 | before do 129 | JSRailsRoutes.configure do |c| 130 | c.include_names = /new/ 131 | end 132 | end 133 | 134 | it 'returns a javascript matching to the regexp' do 135 | expect(subject).to eq <<~TYPESCRIPT 136 | #{described_class::PROCESS_FUNC} 137 | export function new_article_path(params?: Record) { return process('/articles/new', params, []); } 138 | TYPESCRIPT 139 | end 140 | end 141 | 142 | context 'with exclude_names option' do 143 | before do 144 | JSRailsRoutes.configure do |c| 145 | c.exclude_names = /new/ 146 | end 147 | end 148 | 149 | it 'returns a javascript not matching to the regexp' do 150 | expect(subject).to eq <<~TYPESCRIPT 151 | #{described_class::PROCESS_FUNC} 152 | export function articles_path(params?: Record) { return process('/articles', params, []); } 153 | export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); } 154 | export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); } 155 | TYPESCRIPT 156 | end 157 | end 158 | end 159 | 160 | describe '#ext' do 161 | subject { language.ext } 162 | 163 | it { is_expected.to eq 'ts' } 164 | end 165 | end 166 | -------------------------------------------------------------------------------- /spec/js_rails_routes/route_set_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::RouteSet do 4 | subject(:route_set) { described_class.new(name, routes) } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | let(:name) { 'Foo::Engine' } 9 | let(:routes) do 10 | ActionDispatch::Routing::RouteSet.new.tap do |routes| 11 | routes.draw do 12 | get '/articles' => 'articles#index' 13 | get '/users' => 'users#index' 14 | end 15 | end 16 | end 17 | 18 | describe '.correct_matching_route_set_list' do 19 | subject { described_class.correct_matching_route_set_list } 20 | 21 | it 'returns an array of matching route sets' do 22 | # See spec/support/test_app.rb 23 | expect(subject).to match [ 24 | be_a(described_class).and(have_attributes(name: 'Rails')).and(be_match), 25 | be_a(described_class).and(have_attributes(name: 'Admin::Engine')).and(be_match) 26 | ] 27 | end 28 | end 29 | 30 | describe '#name' do 31 | subject { route_set.name } 32 | 33 | it { is_expected.to eq name } 34 | end 35 | 36 | describe '#routes' do 37 | subject { route_set.routes } 38 | 39 | it { is_expected.to all be_a(JSRailsRoutes::Route).and(be_match) } 40 | 41 | context 'when some routes are excluded' do 42 | before do 43 | JSRailsRoutes.configure do |c| 44 | c.exclude_names = /users/ 45 | end 46 | end 47 | 48 | it "doesn't include the excluded route" do 49 | expect(subject).to include be_a(JSRailsRoutes::Route).and(have_attributes(name: /articles/)) 50 | expect(subject).not_to include be_a(JSRailsRoutes::Route).and(have_attributes(name: /users/)) 51 | end 52 | end 53 | end 54 | 55 | describe '#match?' do 56 | subject { route_set.match? } 57 | 58 | it { is_expected.to be true } 59 | 60 | context 'when exclude_engines option is specified' do 61 | before do 62 | JSRailsRoutes.configure do |c| 63 | c.exclude_engines = exclude_engines 64 | end 65 | end 66 | 67 | context 'and it matches to the name' do 68 | let(:exclude_engines) { /Foo/ } 69 | 70 | it { is_expected.to be false } 71 | end 72 | 73 | context 'and it does not match to the name' do 74 | let(:exclude_engines) { /Bar/ } 75 | 76 | it { is_expected.to be true } 77 | end 78 | end 79 | 80 | context 'when routes are empty' do 81 | let(:routes) do 82 | ActionDispatch::Routing::RouteSet.new.tap do |routes| 83 | routes.draw {} # rubocop:disable Lint/EmptyBlock 84 | end 85 | end 86 | 87 | it { is_expected.to be false } 88 | end 89 | 90 | context 'when route_set_filter option is specified' do 91 | before do 92 | JSRailsRoutes.configure do |c| 93 | c.route_set_filter = ->(_route) { result } 94 | end 95 | end 96 | 97 | context 'and it returns true' do 98 | let(:result) { true } 99 | 100 | it { is_expected.to be true } 101 | end 102 | 103 | context 'and it returns false' do 104 | let(:result) { false } 105 | 106 | it { is_expected.to be false } 107 | end 108 | end 109 | end 110 | end 111 | -------------------------------------------------------------------------------- /spec/js_rails_routes/route_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes::Route do 4 | subject(:route) { described_class.new(raw_route) } 5 | 6 | include_context 'run in a sandbox' 7 | 8 | let(:raw_route) do 9 | ActionDispatch::Routing::RouteSet.new.tap do |routes| 10 | routes.draw do 11 | get '/articles' => 'articles#index' 12 | end 13 | end.routes.first 14 | end 15 | 16 | describe '#name' do 17 | subject { route.name } 18 | 19 | it { is_expected.to eq 'articles' } 20 | end 21 | 22 | describe '#name=' do 23 | subject { route.name = value } 24 | 25 | let(:value) { 'foo' } 26 | 27 | it { expect { subject }.to change(route, :name).to(value) } 28 | end 29 | 30 | describe '#path' do 31 | subject { route.path } 32 | 33 | it { is_expected.to eq '/articles' } 34 | end 35 | 36 | describe '#match?' do 37 | subject { route.match? } 38 | 39 | it { is_expected.to be true } 40 | 41 | context 'when include_paths option is specified' do 42 | before do 43 | JSRailsRoutes.configure do |c| 44 | c.include_paths = include_paths 45 | end 46 | end 47 | 48 | context 'and it matches to the path' do 49 | let(:include_paths) { /articles/ } 50 | 51 | it { is_expected.to be true } 52 | end 53 | 54 | context 'and it does not matche to the path' do 55 | let(:include_paths) { /users/ } 56 | 57 | it { is_expected.to be false } 58 | end 59 | end 60 | 61 | context 'when exclude_paths option is specified' do 62 | before do 63 | JSRailsRoutes.configure do |c| 64 | c.exclude_paths = exclude_paths 65 | end 66 | end 67 | 68 | context 'and it matches to the path' do 69 | let(:exclude_paths) { /articles/ } 70 | 71 | it { is_expected.to be false } 72 | end 73 | 74 | context 'and it does not matche to the path' do 75 | let(:exclude_paths) { /users/ } 76 | 77 | it { is_expected.to be true } 78 | end 79 | end 80 | 81 | context 'when include_names option is specified' do 82 | before do 83 | JSRailsRoutes.configure do |c| 84 | c.include_names = include_names 85 | end 86 | end 87 | 88 | context 'and it matches to the name' do 89 | let(:include_names) { /articles/ } 90 | 91 | it { is_expected.to be true } 92 | end 93 | 94 | context 'and it does not matche to the name' do 95 | let(:include_names) { /users/ } 96 | 97 | it { is_expected.to be false } 98 | end 99 | end 100 | 101 | context 'when exclude_names option is specified' do 102 | before do 103 | JSRailsRoutes.configure do |c| 104 | c.exclude_names = exclude_names 105 | end 106 | end 107 | 108 | context 'and it matches to the name' do 109 | let(:exclude_names) { /articles/ } 110 | 111 | it { is_expected.to be false } 112 | end 113 | 114 | context 'and it does not matche to the name' do 115 | let(:exclude_names) { /users/ } 116 | 117 | it { is_expected.to be true } 118 | end 119 | end 120 | 121 | context 'when route_filter option is specified' do 122 | before do 123 | JSRailsRoutes.configure do |c| 124 | c.route_filter = ->(_route) { result } 125 | end 126 | end 127 | 128 | context 'and it returns true' do 129 | let(:result) { true } 130 | 131 | it { is_expected.to be true } 132 | end 133 | 134 | context 'and it returns false' do 135 | let(:result) { false } 136 | 137 | it { is_expected.to be false } 138 | end 139 | end 140 | end 141 | end 142 | -------------------------------------------------------------------------------- /spec/js_rails_routes_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe JSRailsRoutes do 4 | describe '.configure' do 5 | it 'yields with .config' do 6 | expect { |b| described_class.configure(&b) }.to yield_with_args(described_class.config) 7 | end 8 | end 9 | 10 | describe '.config' do 11 | subject { described_class.config } 12 | 13 | it { is_expected.to be_a JSRailsRoutes::Configuration } 14 | end 15 | 16 | describe '.sandbox' do 17 | it 'yields within a new sandbox' do 18 | original = described_class.config 19 | described_class.sandbox do 20 | expect(described_class.config).not_to be original 21 | expect(described_class.config).to be_a JSRailsRoutes::Configuration 22 | end 23 | expect(described_class.config).to be original 24 | end 25 | end 26 | 27 | describe '.generate' do 28 | subject { described_class.generate(task) } 29 | 30 | include_context 'run in a sandbox' 31 | 32 | let(:task) { 'js:routes' } 33 | let(:app_root) { JSRailsRoutes::SpecHelper::TestApp.root } 34 | 35 | before do 36 | FileUtils.rm_rf(app_root) 37 | FileUtils.mkdir_p(app_root.join('app/assets/javascripts')) 38 | end 39 | 40 | shared_examples_for 'javascript target' do 41 | it 'generates javascript files' do 42 | subject 43 | 44 | expect(File.read(app_root.join('app/assets/javascripts/rails-routes.js'))).to eq <<~JAVASCRIPT 45 | /* eslint-disable */ 46 | // Don't edit manually. `rake #{task}` generates this file. 47 | #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC} 48 | export function blogs_path(params) { return process('/blogs', params, []); } 49 | export function new_blog_path(params) { return process('/blogs/new', params, []); } 50 | export function edit_blog_path(params) { return process('/blogs/' + params.id + '/edit', params, ['id']); } 51 | export function blog_path(params) { return process('/blogs/' + params.id + '', params, ['id']); } 52 | export function users_path(params) { return process('/users', params, []); } 53 | export function new_user_path(params) { return process('/users/new', params, []); } 54 | export function edit_user_path(params) { return process('/users/' + params.id + '/edit', params, ['id']); } 55 | export function user_path(params) { return process('/users/' + params.id + '', params, ['id']); } 56 | JAVASCRIPT 57 | 58 | expect(File.read(app_root.join('app/assets/javascripts/admin-routes.js'))).to eq <<~JAVASCRIPT 59 | /* eslint-disable */ 60 | // Don't edit manually. `rake #{task}` generates this file. 61 | #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC} 62 | export function notes_path(params) { return process('/notes', params, []); } 63 | export function new_note_path(params) { return process('/notes/new', params, []); } 64 | export function edit_note_path(params) { return process('/notes/' + params.id + '/edit', params, ['id']); } 65 | export function note_path(params) { return process('/notes/' + params.id + '', params, ['id']); } 66 | export function photos_path(params) { return process('/photos', params, []); } 67 | export function new_photo_path(params) { return process('/photos/new', params, []); } 68 | export function edit_photo_path(params) { return process('/photos/' + params.id + '/edit', params, ['id']); } 69 | export function photo_path(params) { return process('/photos/' + params.id + '', params, ['id']); } 70 | JAVASCRIPT 71 | end 72 | end 73 | 74 | context 'without target config' do 75 | include_examples 'javascript target' 76 | end 77 | 78 | context 'with target="js"' do 79 | before do 80 | described_class.configure do |c| 81 | c.target = 'js' 82 | end 83 | end 84 | 85 | include_examples 'javascript target' 86 | end 87 | 88 | context 'with target="ts"' do 89 | before do 90 | described_class.configure do |c| 91 | c.target = 'ts' 92 | end 93 | end 94 | 95 | it 'generates typescript files' do 96 | subject 97 | 98 | expect(File.read(app_root.join('app/assets/javascripts/rails-routes.ts'))).to eq <<~TYPESCRIPT 99 | /* eslint-disable */ 100 | // Don't edit manually. `rake #{task}` generates this file. 101 | #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC} 102 | export function blogs_path(params?: Record) { return process('/blogs', params, []); } 103 | export function new_blog_path(params?: Record) { return process('/blogs/new', params, []); } 104 | export function edit_blog_path(params: Params<'id'>) { return process('/blogs/' + params.id + '/edit', params, ['id']); } 105 | export function blog_path(params: Params<'id'>) { return process('/blogs/' + params.id + '', params, ['id']); } 106 | export function users_path(params?: Record) { return process('/users', params, []); } 107 | export function new_user_path(params?: Record) { return process('/users/new', params, []); } 108 | export function edit_user_path(params: Params<'id'>) { return process('/users/' + params.id + '/edit', params, ['id']); } 109 | export function user_path(params: Params<'id'>) { return process('/users/' + params.id + '', params, ['id']); } 110 | TYPESCRIPT 111 | 112 | expect(File.read(app_root.join('app/assets/javascripts/admin-routes.ts'))).to eq <<~TYPESCRIPT 113 | /* eslint-disable */ 114 | // Don't edit manually. `rake #{task}` generates this file. 115 | #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC} 116 | export function notes_path(params?: Record) { return process('/notes', params, []); } 117 | export function new_note_path(params?: Record) { return process('/notes/new', params, []); } 118 | export function edit_note_path(params: Params<'id'>) { return process('/notes/' + params.id + '/edit', params, ['id']); } 119 | export function note_path(params: Params<'id'>) { return process('/notes/' + params.id + '', params, ['id']); } 120 | export function photos_path(params?: Record) { return process('/photos', params, []); } 121 | export function new_photo_path(params?: Record) { return process('/photos/new', params, []); } 122 | export function edit_photo_path(params: Params<'id'>) { return process('/photos/' + params.id + '/edit', params, ['id']); } 123 | export function photo_path(params: Params<'id'>) { return process('/photos/' + params.id + '', params, ['id']); } 124 | TYPESCRIPT 125 | end 126 | end 127 | end 128 | end 129 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH << File.expand_path('../lib', __dir__) 4 | 5 | require 'simplecov' 6 | SimpleCov.start 7 | 8 | require 'rails/all' 9 | require 'js_rails_routes' 10 | 11 | Dir[File.expand_path('support/**/*.rb', __dir__)].sort.each { |f| require f } 12 | 13 | RSpec.configure do |config| 14 | config.expect_with :rspec do |expectations| 15 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 16 | end 17 | 18 | config.mock_with :rspec do |mocks| 19 | mocks.verify_partial_doubles = true 20 | end 21 | 22 | config.filter_run :focus 23 | config.run_all_when_everything_filtered = true 24 | config.example_status_persistence_file_path = 'spec/examples.txt' 25 | config.disable_monkey_patching! 26 | 27 | config.default_formatter = 'doc' if config.files_to_run.one? 28 | 29 | config.profile_examples = 10 30 | config.order = :random 31 | Kernel.srand config.seed 32 | end 33 | -------------------------------------------------------------------------------- /spec/support/matchers/not_change.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec::Matchers.define_negated_matcher(:not_change, :change) 4 | -------------------------------------------------------------------------------- /spec/support/shared_contexts/run_in_a_sandbox.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.shared_context 'run in a sandbox' do # rubocop:disable RSpec/ContextWording 4 | around do |example| 5 | JSRailsRoutes.sandbox { example.run } 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/support/test_app.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module JSRailsRoutes 4 | module SpecHelper 5 | class TestApp < ::Rails::Application 6 | config.root = ::File.expand_path('../../tmp/test_app', __dir__) 7 | 8 | routes.draw do 9 | resources :blogs 10 | resources :users 11 | end 12 | end 13 | 14 | class TestEngine < ::Rails::Engine 15 | def self.name 16 | 'Admin::Engine' 17 | end 18 | 19 | routes.draw do 20 | resources :notes 21 | resources :photos 22 | end 23 | end 24 | 25 | class EmptyEngine < ::Rails::Engine 26 | def self.name 27 | 'Empty::Engine' 28 | end 29 | 30 | routes.draw {} # rubocop:disable Lint/EmptyBlock 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /spec/tmp/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/increments/js_rails_routes/c477c6f1be40fd355e639c776d1129220ac06b92/spec/tmp/.keep --------------------------------------------------------------------------------