js_rails_routes 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c971acaca259fdde1cd934667f6ea2ca761f42e9
4
- data.tar.gz: 8895cd1743d727e936e9da298f392e8a2c1c9960
3
+ metadata.gz: 6542396accf937692fb966d1cea9e13b037059fc
4
+ data.tar.gz: 2fabec2ed3c39d5b12c9735e0a2e84aaaa39e640
5
5
  SHA512:
6
- metadata.gz: 709cca081ac5958d1cf68398ff15a0ff5cc17ce3201f1e05fa232198bbca591be22969ca51140b9770edfa87b45e6b94e3b11efbefb90b666f759d78b5a7632f
7
- data.tar.gz: e07ad1c8d18f843701a008b59796c94952a29eaa0eb314b21727e27095dd4099a4fcc4655fe7a812ed05d8f5f9fd8dbb15ef431f107e1906916407c952d13b2f
6
+ metadata.gz: 5d0df45754906758a9266abbc7382dc07390e5ad0b06a564883fdf21e3569da2d92797496789dc72331566c3ba76308c427b3430d7cb837a1819ca375cbb7ea2
7
+ data.tar.gz: 160bf9a51331d3ecb1809f6f229aff92748d7a3853f3047920d608bdc28c4260fa194c8ce870dd2cb27773f79e4bd00b19812a3a1c8de54d608067a9bb651354
data/.codeclimate.yml ADDED
@@ -0,0 +1,16 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ fixme:
9
+ enabled: true
10
+ rubocop:
11
+ enabled: true
12
+ ratings:
13
+ paths:
14
+ - "**.rb"
15
+ exclude_paths:
16
+ - spec/
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  /pkg/
2
2
  *.gem
3
3
  Gemfile.lock
4
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Metrics/AbcSize:
5
+ Max: 20
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/TrailingCommaInLiteral:
11
+ Enabled: false
12
+
13
+ Style/PerlBackrefs:
14
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ notifications:
5
+ email: false
data/CHANGELOG.md CHANGED
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  This change log adheres to [keepachangelog.com](http://keepachangelog.com).
6
6
 
7
+ ## [0.3.0]
8
+ ### Added
9
+ - Enable to configure through Ruby interface
10
+
11
+ ### Changed
12
+ - Depend on rails >= 3.2
13
+ - Remove rake command line parameter from js file
14
+
7
15
  ## [0.2.1] - 2016-06-24
8
16
  ### Fixed
9
17
  - Make sure that js file ends with a new line char
@@ -16,6 +24,7 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
16
24
  ### Added
17
25
  - Implement "js:rails:routes" task
18
26
 
27
+ [0.3.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.2.1...v0.3.0
19
28
  [0.2.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.2.0...v0.2.1
20
29
  [0.2.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.1.0...v0.2.0
21
30
  [0.1.0]: https://github.com/yuku-t/js_rails_routes/compare/033b945...v0.1.0
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
+
4
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # rake js:routes
2
2
 
3
+ [![Gem](https://img.shields.io/gem/v/js_rails_routes.svg?maxAge=2592000)](https://rubygems.org/gems/js_rails_routes) [![Build Status](https://travis-ci.org/yuku-t/js_rails_routes.svg?branch=master)](https://travis-ci.org/yuku-t/js_rails_routes) [![Code Climate](https://codeclimate.com/github/yuku-t/js_rails_routes/badges/gpa.svg)](https://codeclimate.com/github/yuku-t/js_rails_routes) [![Test Coverage](https://codeclimate.com/github/yuku-t/js_rails_routes/badges/coverage.svg)](https://codeclimate.com/github/yuku-t/js_rails_routes/coverage) [![license](https://img.shields.io/github/license/yuku-t/js_rails_routes.svg?maxAge=2592000)](https://github.com/yuku-t/js_rails_routes/blob/master/LICENSE)
4
+
3
5
  Generate a ES6 module that contains Rails routes.
4
6
 
5
7
  ## Description
@@ -7,7 +9,7 @@ Generate a ES6 module that contains Rails routes.
7
9
  This gem provides "js:routes" rake task.
8
10
  It generates a ES6 requirable module with exports url helper functions defined by your Rails application.
9
11
 
10
- Suppose there are some routes defined:
12
+ Suppose a Rails app has some routes:
11
13
 
12
14
  ```rb
13
15
  # == Route Map
@@ -26,14 +28,14 @@ Rails.application.routes.draw do
26
28
  end
27
29
  ```
28
30
 
29
- The JS file generated by this gem exports following functions:
31
+ This gem generates a JS file looks like this:
30
32
 
31
33
  ```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'
34
+ // Don't edit manually. `rake js:routes` generates this file.
35
+ export function article_path(params) { return '/articles/' + params.id + ''; }
36
+ export function articles_path(params) { return '/articles'; }
37
+ export function edit_article_path(params) { return '/articles/' + params.id + '/edit'; }
38
+ export function new_article_path(params) { return '/articles/new'; }
37
39
  ```
38
40
 
39
41
  ## VS.
@@ -44,38 +46,53 @@ This gem uses ES6 modules.
44
46
 
45
47
  ## Requirement
46
48
 
47
- - Rails >= 3.0
49
+ - Rails >= 3.2
48
50
 
49
51
  ## Usage
50
52
 
51
- Write all routes to app/assets/javascripts/rails-routes.js:
53
+ Generate routes file.
52
54
 
53
- ```
55
+ ```bash
54
56
  rake js:routes
55
57
  ```
56
58
 
57
- Write to client/src/rails-routes.js:
59
+ ### Configuration
58
60
 
59
- ```
60
- rake js:routes path='client/src/rails-routes.js'
61
- ```
61
+ JSRailsRoutes supports several parameters:
62
62
 
63
- Generate routes except paths start with "/rails/" or "/sidekiq/":
63
+ Name | Type | Description | Default
64
+ -----------|----------|-----------------------------------------|----------------------------------------
65
+ `includes` | `Regexp` | routes match to the regexp are included | `/.*/`
66
+ `excludes` | `Regexp` | routes match to the regexp are excluded | `/^$/`
67
+ `path` | `String` | JS file path | `"app/assets/javascripts/rails-routes.js"`
64
68
 
65
- ```
66
- rake js:routes excludes='^/(rails|sidekiq)/'
67
- ```
68
- Generate routes start with "/articles/" only:
69
+ You can configure via `JSRailsRoutes.configure`.
69
70
 
71
+ ```rb
72
+ # Rakefile
73
+ JSRailsRoutes.configure |c|
74
+ c.excludes = %r{^/(rails|sidekiq)}
75
+ c.path = 'path/to/rails-routes.js'
76
+ end
70
77
  ```
71
- rake js:routes includes='^/articles/'
78
+
79
+ Now `rake js:routes` ignores paths starting with "/rails" or "/sidekiq".
80
+
81
+ ### Command line parameters
82
+
83
+ You can override the coniguration via command line parameters:
84
+
85
+ ```bash
86
+ rake js:routes excludes='^/rails'
72
87
  ```
73
88
 
89
+ The command still ignores "/rails" but includes "/sidekiq".
90
+
74
91
  ## Install
75
92
 
76
93
  Your Rails Gemfile:
77
94
 
78
- ```
95
+ ```rb
79
96
  gem 'js_rails_routes', group: :development
80
97
  ```
81
98
 
@@ -87,4 +104,4 @@ gem 'js_rails_routes', group: :development
87
104
 
88
105
  [mizchi](https://github.com/mizchi) wrote "js:routes" task with referencing [mtrpcic/js-routes](https://github.com/mtrpcic/js-routes).
89
106
 
90
- [yuku-t](https://yuku-t.com) refactor and improve the mizchi's script and published to rubygems.
107
+ [yuku-t](https://yuku-t.com) refactored and improved the mizchi's script and published to [rubygems](https://rubygems.org/gems/js_rails_routes).
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: [:rubocop, :spec]
@@ -1,4 +1,4 @@
1
- $: << File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
2
 
3
3
  require 'js_rails_routes/version'
4
4
 
@@ -12,7 +12,9 @@ Gem::Specification.new do |spec|
12
12
  spec.license = 'MIT'
13
13
  spec.files = `git ls-files -z`.split("\x0")
14
14
  spec.require_paths = ['lib']
15
- spec.add_dependency 'rails', '>= 3'
15
+ spec.add_dependency 'rails', '>= 3.2'
16
16
  spec.add_development_dependency 'bundler', '~> 1.7'
17
17
  spec.add_development_dependency 'rake', '~> 10.0'
18
+ spec.add_development_dependency 'rspec', '~> 3.4.0'
19
+ spec.add_development_dependency 'rubocop', '~> 0.40.0'
18
20
  end
@@ -1,57 +1,51 @@
1
+ require 'singleton'
2
+
1
3
  module JSRailsRoutes
2
4
  class Generator
3
- COMPARE_REGEXP = /:(.*?)(\/|$)/
5
+ COMPARE_REGEXP = %r{:(.*?)(/|$)}
6
+
7
+ include Singleton
4
8
 
5
- def initialize(includes: nil, excludes: nil)
6
- @includes = includes
7
- @excludes = excludes
9
+ attr_accessor :includes, :excludes, :path
10
+
11
+ def initialize
12
+ self.includes = /.*/
13
+ self.excludes = /^$/
14
+ self.path = Rails.root.join('app', 'assets', 'javascripts', 'rails-routes.js')
8
15
  Rails.application.reload_routes!
9
16
  end
10
17
 
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)
18
+ def generate(task)
19
+ lines = ["// Don't edit manually. `rake #{task}` generates this file."]
20
+ lines += routes.map do |route_name, route_path|
21
+ handle_route(route_name, route_path) if match?(route_name, route_path)
15
22
  end.compact
16
- lines += '' # End with new line
17
- File.open(save_path, 'w') { |f| f.write(lines.join("\n")) }
23
+ lines += [''] # End with new line
24
+ write(lines.join("\n"))
18
25
  end
19
26
 
20
27
  private
21
28
 
22
- attr_reader :includes, :excludes
23
-
24
- def includes_regexp
25
- @includes_regexp ||= Regexp.new(includes)
26
- end
27
-
28
- def excludes_regexp
29
- @excludes_regexp ||= Regexp.new(excludes)
30
- end
31
-
32
- def match?(name, path)
33
- return false if includes && includes_regexp !~ path
34
- return false if excludes && excludes_regexp =~ path
29
+ def match?(_route_name, route_path)
30
+ return false if includes !~ route_path
31
+ return false if excludes =~ route_path
35
32
  true
36
33
  end
37
34
 
38
- def handle_route(name, path)
39
- path.sub!(COMPARE_REGEXP, "' + params.#{$1} + '#{$2}") while path =~ COMPARE_REGEXP
40
- "export function #{name}_path(params) { return '#{path}'; }"
35
+ def handle_route(route_name, route_path)
36
+ route_path.sub!(COMPARE_REGEXP, "' + params.#{$1} + '#{$2}") while route_path =~ COMPARE_REGEXP
37
+ "export function #{route_name}_path(params) { return '#{route_path}'; }"
41
38
  end
42
39
 
43
40
  def routes
44
41
  @routes ||= Rails.application.routes.routes
45
- .select(&:name)
46
- .map { |r| [r.name, r.path.spec.to_s.split('(')[0]] }
47
- .sort { |a, b| a[0] <=> b[0] }
42
+ .select(&:name)
43
+ .map { |r| [r.name, r.path.spec.to_s.split('(')[0]] }
44
+ .sort { |a, b| a[0] <=> b[0] }
48
45
  end
49
46
 
50
- def env
51
- result = []
52
- result << "includes='#{includes}'" if includes
53
- result << "excludes='#{excludes}'" if excludes
54
- result.empty? ? '' : ' ' + result.join(' ')
47
+ def write(string)
48
+ File.open(path, 'w') { |f| f.write(string) }
55
49
  end
56
50
  end
57
51
  end
@@ -1,3 +1,3 @@
1
1
  module JSRailsRoutes
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -1,3 +1,11 @@
1
1
  require 'js_rails_routes/engine'
2
2
  require 'js_rails_routes/generator'
3
3
  require 'js_rails_routes/version'
4
+
5
+ module JSRailsRoutes
6
+ module_function
7
+
8
+ def configure
9
+ yield Generator.instance if block_given?
10
+ end
11
+ end
@@ -1,12 +1,11 @@
1
1
  desc 'Generate a ES6 module that contains Rails routes'
2
2
  namespace :js do
3
3
  task routes: :environment do |task|
4
- path = ENV['path'] || Rails.root.join('app', 'assets', 'javascripts', 'rails-routes.js')
5
- generator = JSRailsRoutes::Generator.new(
6
- includes: ENV['includes'],
7
- excludes: ENV['excludes']
8
- )
9
- generator.generate(task, path)
10
- puts "Routes saved to #{path}."
4
+ generator = JSRailsRoutes::Generator.instance
5
+ generator.includes = Regexp.new(ENV['includes']) if ENV['includes']
6
+ generator.excludes = Regexp.new(ENV['excludes']) if ENV['excludes']
7
+ generator.path = ENV['path'] if ENV['path']
8
+ generator.generate(task)
9
+ puts "Routes saved to #{generator.path}."
11
10
  end
12
11
  end
@@ -0,0 +1,62 @@
1
+ RSpec.describe JSRailsRoutes::Generator do
2
+ let(:generator) do
3
+ described_class.clone.instance
4
+ end
5
+
6
+ it { expect(described_class).to include(Singleton) }
7
+
8
+ describe '#generate' do
9
+ subject do
10
+ generator.generate(task)
11
+ end
12
+
13
+ let(:task) do
14
+ 'js:routes'
15
+ end
16
+
17
+ it 'writes a JS file' do
18
+ expect(generator).to receive(:write).with(a_string_including("rake #{task}"))
19
+ subject
20
+ end
21
+
22
+ context 'when includes is given' do
23
+ before do
24
+ generator.includes = Regexp.new(includes)
25
+ end
26
+
27
+ let(:includes) do
28
+ '/new'
29
+ end
30
+
31
+ it 'writes paths matching with the parameter' do
32
+ expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
33
+ paths = arg.split("\n")[1..-1]
34
+ expect(paths).not_to be_empty
35
+ expect(paths).to all(a_string_including(includes))
36
+ end
37
+ subject
38
+ end
39
+ end
40
+
41
+ context 'when excludes is given' do
42
+ before do
43
+ generator.excludes = Regexp.new(excludes)
44
+ end
45
+
46
+ let(:excludes) do
47
+ '/new'
48
+ end
49
+
50
+ it 'writes paths not matching with the parameter' do
51
+ expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
52
+ paths = arg.split("\n")[1..-1]
53
+ expect(paths).not_to be_empty
54
+ paths.each do |path|
55
+ expect(path).to_not include(excludes)
56
+ end
57
+ end
58
+ subject
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.describe JSRailsRoutes do
2
+ describe '.configure' do
3
+ it 'yields with Generator instance' do
4
+ expect { |b| described_class.configure(&b) }.to yield_with_args(JSRailsRoutes::Generator.instance)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'codeclimate-test-reporter'
4
+ CodeClimate::TestReporter.start
5
+
6
+ require 'rails/all'
7
+ require 'js_rails_routes'
8
+
9
+ class TestApp < Rails::Application
10
+ config.root = File.expand_path('../test_app', __FILE__)
11
+ end
12
+
13
+ RSpec.configure do |config|
14
+ config.before :all do
15
+ TestApp.routes.draw do
16
+ resources :apps
17
+ end
18
+ end
19
+
20
+ config.expect_with :rspec do |expectations|
21
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
22
+ end
23
+
24
+ config.mock_with :rspec do |mocks|
25
+ mocks.verify_partial_doubles = true
26
+ end
27
+
28
+ config.filter_run :focus
29
+ config.run_all_when_everything_filtered = true
30
+ config.example_status_persistence_file_path = 'spec/examples.txt'
31
+ config.disable_monkey_patching!
32
+
33
+ config.default_formatter = 'doc' if config.files_to_run.one?
34
+
35
+ config.profile_examples = 10
36
+ config.order = :random
37
+ Kernel.srand config.seed
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_rails_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuku Takahashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-24 00:00:00.000000000 Z
11
+ date: 2016-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3'
19
+ version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3'
26
+ version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.40.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.40.0
55
83
  description:
56
84
  email:
57
85
  - taka84u9@gmail.com
@@ -59,7 +87,11 @@ executables: []
59
87
  extensions: []
60
88
  extra_rdoc_files: []
61
89
  files:
90
+ - ".codeclimate.yml"
62
91
  - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - ".travis.yml"
63
95
  - CHANGELOG.md
64
96
  - Gemfile
65
97
  - LICENSE
@@ -71,6 +103,9 @@ files:
71
103
  - lib/js_rails_routes/generator.rb
72
104
  - lib/js_rails_routes/version.rb
73
105
  - lib/tasks/js_rails_routes.rake
106
+ - spec/js_rails_routes/generator_spec.rb
107
+ - spec/js_rails_routes_spec.rb
108
+ - spec/spec_helper.rb
74
109
  homepage: https://github.com/yuku-t/js_rails_routes
75
110
  licenses:
76
111
  - MIT