js_rails_routes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61db98e6e7dc44440b72ab03ebc63a7a8f03a58a
4
+ data.tar.gz: 36a09a7db939d6ce498f5efd1e39b82785dc95db
5
+ SHA512:
6
+ metadata.gz: 77cc9a29fbeaae2fbc634c9efdbc35c20ad45388c89df1718d59aeafbe943af392ee798846e67ca6c91ae4465d12b4f1938641d8a22856dd5bac1f06588145ff
7
+ data.tar.gz: c04d2e6e05e77a01c65aa32850fc782e432d977b8128ff6146e0bc4dfdaa892cef282c85037f090ad056fa480b9589758a19280ff8356500694a49ef7c634839
@@ -0,0 +1,3 @@
1
+ /pkg/
2
+ *.gem
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yuku TAKAHASHI
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
+
@@ -0,0 +1,90 @@
1
+ # rake js:rails\_routes
2
+
3
+ Generate a ES6 module that contains Rails routes.
4
+
5
+ ## Description
6
+
7
+ This gem provides "js:rails:routes" rake task.
8
+ It generates a ES6 requirable module whith exports url helper functions defined by your Rails application.
9
+
10
+ Suppose there are some routes defined:
11
+
12
+ ```rb
13
+ # == Route Map
14
+ #
15
+ # Prefix Verb URI Pattern Controller#Action
16
+ # articles GET /articles(.:format) articles#index
17
+ # POST /articles(.:format) articles#create
18
+ # new_article GET /articles/new(.:format) articles#new
19
+ # edit_article GET /articles/:id/edit(.:format) articles#edit
20
+ # article GET /articles/:id(.:format) articles#show
21
+ # PATCH /articles/:id(.:format) articles#update
22
+ # PUT /articles/:id(.:format) articles#update
23
+ # DELETE /articles/:id(.:format) articles#destroy
24
+ Rails.application.routes.draw do
25
+ resources :articles
26
+ end
27
+ ```
28
+
29
+ The JS file generated by this gem exports following functions:
30
+
31
+ ```js
32
+ var RailsRoutes = require('./rails-routes');
33
+ RailsRoutes.articles_path(); //=> '/articles'
34
+ RailsRoutes.new_article_path(); //=> '/articles/new'
35
+ RailsRoutes.edit_article_path({ id: 1 }); //=> '/articles/1/edit'
36
+ RailsRoutes.article_path({ id: 1 }); //=> '/articles/1'
37
+ ```
38
+
39
+ ## VS.
40
+
41
+ [railsware/js-routes](https://github.com/railsware/js-routes) spreads url helpers via global variable.
42
+
43
+ This gem uses ES6 modules.
44
+
45
+ ## Requirement
46
+
47
+ - Rails >= 3.0
48
+
49
+ ## Usage
50
+
51
+ Write all routes to app/assets/javascripts/rails-routes.js:
52
+
53
+ ```
54
+ rake js:rails:routes
55
+ ```
56
+
57
+ Write to client/src/rails-routes.js:
58
+
59
+ ```
60
+ rake js:rails:routes path='client/src/rails-routes.js'
61
+ ```
62
+
63
+ Generate routes except paths start with "/rails/" or "/sidekiq/":
64
+
65
+ ```
66
+ rake js:rails:routes excludes='^/(rails|sidekiq)/'
67
+ ```
68
+ Generate routes start with "/articles/" only:
69
+
70
+ ```
71
+ rake js:rails:routes includes='^/articles/'
72
+ ```
73
+
74
+ ## Install
75
+
76
+ Your Rails Gemfile:
77
+
78
+ ```
79
+ gem 'js_rails_routes', group: :development
80
+ ```
81
+
82
+ ## License
83
+
84
+ [MIT](https://github.com/yuku-t/js_rails_routes/blob/master/LICENCE)
85
+
86
+ ## Author
87
+
88
+ [mizchi](https://github.com/mizchi) wrote "js:rake" task with referencing [mtrpcic/js-routes](https://github.com/mtrpcic/js-routes).
89
+
90
+ [yuku-t](https://yuku-t.com) refactor and improve the mizchi's script and published to rubygems.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,18 @@
1
+ $: << File.expand_path('../lib', __FILE__)
2
+
3
+ require 'js_rails_routes/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'js_rails_routes'
7
+ spec.version = JSRailsRoutes::VERSION
8
+ spec.authors = ['Yuku Takahashi']
9
+ spec.email = ['taka84u9@gmail.com']
10
+ spec.summary = 'Generate a ES6 module that contains Rails routes.'
11
+ spec.homepage = 'https://github.com/yuku_t/js_rails_routes'
12
+ spec.license = 'MIT'
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.require_paths = ['lib']
15
+ spec.add_dependency 'rails', '>= 3'
16
+ spec.add_development_dependency 'bundler', '~> 1.7'
17
+ spec.add_development_dependency 'rake', '~> 10.0'
18
+ end
@@ -0,0 +1,3 @@
1
+ require 'js_rails_routes/engine'
2
+ require 'js_rails_routes/generator'
3
+ require 'js_rails_routes/version'
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ module JSRailsRoutes
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,56 @@
1
+ module JSRailsRoutes
2
+ class Generator
3
+ COMPARE_REGEXP = /:(.*?)(\/|$)/
4
+
5
+ def initialize(includes: nil, excludes: nil)
6
+ @includes = includes
7
+ @excludes = excludes
8
+ Rails.application.reload_routes!
9
+ end
10
+
11
+ def generate(task, save_path)
12
+ lines = ["// Don't edit manually. `rake #{task}#{env}` generates this file."]
13
+ lines += routes.map do |name, path|
14
+ handle_route(name, path) if match?(name, path)
15
+ end.compact
16
+ File.open(save_path, 'w') { |f| f.write(lines.join("\n")) }
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :includes, :excludes
22
+
23
+ def includes_regexp
24
+ @includes_regexp ||= Regexp.new(includes)
25
+ end
26
+
27
+ def excludes_regexp
28
+ @excludes_regexp ||= Regexp.new(excludes)
29
+ end
30
+
31
+ def match?(name, path)
32
+ return false if includes && includes_regexp !~ path
33
+ return false if excludes && excludes_regexp =~ path
34
+ true
35
+ end
36
+
37
+ def handle_route(name, path)
38
+ path.sub!(COMPARE_REGEXP, "' + params.#{$1} + '#{$2}") while path =~ COMPARE_REGEXP
39
+ "export function #{name}_path(params) { return '#{path}'; }"
40
+ end
41
+
42
+ def routes
43
+ @routes ||= Rails.application.routes.routes
44
+ .select(&:name)
45
+ .map { |r| [r.name, r.path.spec.to_s.split('(')[0]] }
46
+ .sort { |a, b| a[0] <=> b[0] }
47
+ end
48
+
49
+ def env
50
+ result = []
51
+ result << "includes='#{includes}'" if includes
52
+ result << "excludes='#{excludes}'" if excludes
53
+ result.empty? ? '' : ' ' + result.join(' ')
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module JSRailsRoutes
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ desc 'Generate a ES6 module that contains Rails routes'
2
+ namespace :js do
3
+ namespace :rails do
4
+ task routes: :environment do |task|
5
+ path = ENV['path'] || Rails.root.join('app', 'assets', 'javascripts', 'rails-routes.js')
6
+ generator = JSRailsRoutes::Generator.new(
7
+ includes: ENV['includes'],
8
+ excludes: ENV['excludes']
9
+ )
10
+ generator.generate(task, path)
11
+ puts "Routes saved to #{path}."
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_rails_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuku Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - taka84u9@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - js_rails_routes.gemspec
68
+ - lib/js_rails_routes.rb
69
+ - lib/js_rails_routes/engine.rb
70
+ - lib/js_rails_routes/generator.rb
71
+ - lib/js_rails_routes/version.rb
72
+ - lib/tasks/js_rails_routes.rake
73
+ homepage: https://github.com/yuku_t/js_rails_routes
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Generate a ES6 module that contains Rails routes.
97
+ test_files: []