js-routes-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/js-routes-rails-template.js +19 -0
- data/js-routes-rails.gemspec +35 -0
- data/lib/js-routes/rails/engine.rb +14 -0
- data/lib/js-routes/rails/route_exporter.rb +37 -0
- data/lib/js-routes/rails/route_finder.rb +31 -0
- data/lib/js-routes/rails/version.rb +7 -0
- data/lib/js-routes/rails.rb +26 -0
- data/lib/js-routes/tasks/js-routes-rails.rake +6 -0
- data/lib/js-routes-rails.rb +9 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2e41da3f7d5988bb153308e80144c92278820f7
|
4
|
+
data.tar.gz: 354ef08359fa825406e15dc906855c4ddbf2c82f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d38649bd0817b0c77c50d4d85a53084485a7fedc24be3cede82c1230a16e5c115e08de5772de4b7e5cf7236460460579a3aaec25a4e08ca7cf12817e584e18f
|
7
|
+
data.tar.gz: 390599f022123fea8bdf8dc5072f75ea0dabcb8ec14ecc0d09b566b89b44ccbb08f3b358fb86ff429adf4c8da7ca3ca4860f52deb2fe426b9d4f77a10852af63
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at damian.nicholson21@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Damian Nicholson
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Js::Routes::Rails
|
2
|
+
|
3
|
+
Generates a JavaScript file containing a predefined set of Rails URL helpers on the client.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'js-routes-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Set up
|
18
|
+
|
19
|
+
Append `export: true` to any route defined in you `config/routes.rb` which declares that resource able to be exported to the client.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# config/routes.rb
|
23
|
+
resources :articles, export: true
|
24
|
+
|
25
|
+
resources :comments
|
26
|
+
|
27
|
+
resources :publishers, export: true do
|
28
|
+
resources :magazines do
|
29
|
+
resources :photos
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
match "/about", to: "static#index", via: :get, export: true
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle exec rake js_routes_rails:export
|
39
|
+
|
40
|
+
This will generate a JavaScript file in `app/assets/javascript/js-routes-rails.js`, which you can consume using the Asset Pipeline, for example:
|
41
|
+
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
javascript_include_tag 'js-routes-rails'
|
45
|
+
```
|
46
|
+
|
47
|
+
or in `application.js`
|
48
|
+
|
49
|
+
```javascript
|
50
|
+
//= 'js-routes-rails'
|
51
|
+
```
|
52
|
+
|
53
|
+
## Usage
|
54
|
+
|
55
|
+
Once you've brought the exported file in to your project your now able to use the `JsRoutesRails` object which gives you access to all of your Rails URL helpers on the client.
|
56
|
+
|
57
|
+
```javascript
|
58
|
+
JsRoutesRails.new_articles_path() // '/articles/new'
|
59
|
+
JsRoutesRails.edit_articles_path({ id: 23 }) // '/articles/23/edit'
|
60
|
+
JsRoutesRails.edit_magazines_path({ publishers_id: 5, id: 9 }) // '/publishers/5/magazines/9/edit'
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/damian/js-routes-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
66
|
+
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
71
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
var JsRoutesRails = (function() {
|
2
|
+
var routes = {};
|
3
|
+
<% @routes.each do |helper, path| %>
|
4
|
+
routes['<%= helper %>'] = function(options) {
|
5
|
+
return format('<%= path %>', options);
|
6
|
+
};
|
7
|
+
<% end %>
|
8
|
+
|
9
|
+
function format(string, options) {
|
10
|
+
var str = string.toString();
|
11
|
+
for (option in options) {
|
12
|
+
str = str.replace(RegExp("\\:" + option, "gi"), options[option]);
|
13
|
+
}
|
14
|
+
|
15
|
+
return str;
|
16
|
+
};
|
17
|
+
|
18
|
+
return routes;
|
19
|
+
})();
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'js-routes/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "js-routes-rails"
|
8
|
+
spec.version = Js::Routes::Rails::VERSION
|
9
|
+
spec.authors = ["Damian Nicholson"]
|
10
|
+
spec.email = ["hi@damiannicholson.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Brings Rails url_helpers to JavaScript}
|
13
|
+
spec.description = %q{Generates a JavaScript file containing specific url_helpers defined in config/routes.rb}
|
14
|
+
spec.homepage = "https://github.com/damian/js-routes-rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "railties", ">= 4.2.0"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
# spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "js-routes/rails/route_finder"
|
2
|
+
require "js-routes/rails/route_exporter"
|
3
|
+
|
4
|
+
module Js
|
5
|
+
module Routes
|
6
|
+
module Rails
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
rake_tasks do
|
9
|
+
load "js-routes/tasks/js-routes-rails.rake"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Js
|
2
|
+
module Routes
|
3
|
+
module Rails
|
4
|
+
class RouteExporter
|
5
|
+
attr_reader :routes
|
6
|
+
|
7
|
+
def initialize(finder = nil)
|
8
|
+
@routes = finder.routes
|
9
|
+
end
|
10
|
+
|
11
|
+
def export!
|
12
|
+
File.open(output_path, 'w') { |f| f.write(compiled) }
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def output_path
|
18
|
+
::Rails.root.join('app', 'assets', 'javascripts', 'js-routes-rails.js')
|
19
|
+
end
|
20
|
+
|
21
|
+
def compiled
|
22
|
+
b = binding
|
23
|
+
b.local_variable_set(:routes, routes)
|
24
|
+
ERB.new(template).result(b)
|
25
|
+
end
|
26
|
+
|
27
|
+
def template
|
28
|
+
File.read(template_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def template_path
|
32
|
+
File.join(Js::Routes.root, 'js-routes-rails-template.js')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Js
|
2
|
+
module Routes
|
3
|
+
module Rails
|
4
|
+
class RouteFinder
|
5
|
+
attr_reader :routes
|
6
|
+
|
7
|
+
def initialize(application_routes = nil)
|
8
|
+
@routes = {}
|
9
|
+
@application_routes = application_routes
|
10
|
+
find_routes
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_routes
|
14
|
+
@application_routes.to_a.each do |route|
|
15
|
+
if route.defaults.key?(:export) && route.name.present?
|
16
|
+
@routes[path_helper(route)] = path(route)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def path_helper(route)
|
22
|
+
"#{route.name}_path"
|
23
|
+
end
|
24
|
+
|
25
|
+
def path(route)
|
26
|
+
route.path.spec.to_s.chomp("(.:format)")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "js-routes/rails/engine"
|
2
|
+
require "js-routes/rails/version"
|
3
|
+
|
4
|
+
module Js
|
5
|
+
module Routes
|
6
|
+
module Rails
|
7
|
+
class << self
|
8
|
+
attr_writer :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= OpenStruct.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.export!(routes = nil)
|
16
|
+
finder = Js::Routes::Rails::RouteFinder.new(routes)
|
17
|
+
exporter = Js::Routes::Rails::RouteExporter.new(finder)
|
18
|
+
exporter.export!
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.configure
|
22
|
+
yield configuration
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js-routes-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damian Nicholson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.0
|
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.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
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: Generates a JavaScript file containing specific url_helpers defined in
|
56
|
+
config/routes.rb
|
57
|
+
email:
|
58
|
+
- hi@damiannicholson.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- js-routes-rails-template.js
|
71
|
+
- js-routes-rails.gemspec
|
72
|
+
- lib/js-routes-rails.rb
|
73
|
+
- lib/js-routes/rails.rb
|
74
|
+
- lib/js-routes/rails/engine.rb
|
75
|
+
- lib/js-routes/rails/route_exporter.rb
|
76
|
+
- lib/js-routes/rails/route_finder.rb
|
77
|
+
- lib/js-routes/rails/version.rb
|
78
|
+
- lib/js-routes/tasks/js-routes-rails.rake
|
79
|
+
homepage: https://github.com/damian/js-routes-rails
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
allowed_push_host: https://rubygems.org
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.4.5
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Brings Rails url_helpers to JavaScript
|
104
|
+
test_files: []
|