js_route_helpers 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/Rakefile +1 -0
- data/app/assets/javascript/route_helpers.js.erb +90 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/js_route_helpers.gemspec +25 -0
- data/lib/js_route_helpers/engine.rb +4 -0
- data/lib/js_route_helpers/version.rb +3 -0
- data/lib/js_route_helpers.rb +17 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c7d3f40c1b19ac3dff8cecce7f49eaeda506c3d2
|
4
|
+
data.tar.gz: 8c120103aa1a79700fa3254332ae31f89a2e2461
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c5af2a6f4ceda85ecdfc8d9835a82f358a52fd1e08d71860fcd2f044ad57718b0eeeba7282a77ed462ddf377c606455fdc57ab381a4af0088c93a0127adbb226
|
7
|
+
data.tar.gz: 33d1a53f11f1755b8ac786d1a4cfcd99847501896da4551a88e1cb89e8acdbc76da79d57717ff2fe24202a4f836c9d943157010c55ef99e9a3b7fa626c624deb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jordan Stephens
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# JsRouteHelpers
|
2
|
+
|
3
|
+
Use Rails route helpers in your javascript.
|
4
|
+
|
5
|
+
For example, take the following route definition:
|
6
|
+
|
7
|
+
**config/routes.rb**
|
8
|
+
|
9
|
+
get "/users/:id", to: "users#show", as: :user
|
10
|
+
|
11
|
+
Rails gives you a `user_path` helper, which you can use like this:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
user_path(3) # => "/users/3"
|
15
|
+
```
|
16
|
+
|
17
|
+
JsRouteHelpers gives you a similar javascript helper:
|
18
|
+
|
19
|
+
```javascript
|
20
|
+
RouteHelpers.userPath(3) // => "/users/3"
|
21
|
+
```
|
22
|
+
|
23
|
+
**NOTE**: Javascript names are conventionally camelCase, so make sure to use the camelCase versions of your route helpers when calling them client-side.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'js_route_helpers'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install js_route_helpers
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
### Setup
|
44
|
+
|
45
|
+
1. Define a route pattern whitelist in an initializer. Each item in the list should be a regular expression. If a route helper's name matches any pattern in the list, it will be exposed to the client in the javascript file.
|
46
|
+
|
47
|
+
**config/initializers/js_route_helpers.rb**
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
JsRouteHelpers.include [
|
51
|
+
%r(user),
|
52
|
+
%r(post)
|
53
|
+
]
|
54
|
+
```
|
55
|
+
|
56
|
+
**Important**: *You must whitelist any route you wish to expose.* You will probably not want to expose all of your routes. No routes are whitelisted by default.
|
57
|
+
|
58
|
+
2. Now, require `route_helpers` in your javascript file.
|
59
|
+
|
60
|
+
**app/assets/javascripts/application.js**
|
61
|
+
|
62
|
+
```javascript
|
63
|
+
//= require route_helpers
|
64
|
+
```
|
65
|
+
|
66
|
+
3. Now you can use your route helpers in your javascript just like you would in ruby.
|
67
|
+
|
68
|
+
```javascript
|
69
|
+
RouteHelpers.userPath(3, { param: "value", format: "json" }) // => "/users/3.json?param=value"
|
70
|
+
```
|
71
|
+
|
72
|
+
* * *
|
73
|
+
|
74
|
+
**Important!**
|
75
|
+
|
76
|
+
You may need to flush the asset cache after making changes to `config/routes.rb` in order to force the changes to propagate into `route_helpers.js`. You can do this by running
|
77
|
+
|
78
|
+
```shell
|
79
|
+
$ rake tmp:cache:clear
|
80
|
+
```
|
81
|
+
|
82
|
+
This shouldn't be _too_ much of a nuisance in the long run, as routes tend to be fairly stable.
|
83
|
+
|
84
|
+
* * *
|
85
|
+
|
86
|
+
### Examples
|
87
|
+
|
88
|
+
Here are some example rails route helpers, and then the javascript counterparts provided by JsRouteHelpers:
|
89
|
+
|
90
|
+
**ruby**:
|
91
|
+
```ruby
|
92
|
+
user_path(3) # => "/users/3"
|
93
|
+
user_path(3, { param: "value" }) # => "/users/3?param=value"
|
94
|
+
user_path(3, { param: "value", format: "json" }) # => "/users/3.json?param=value"
|
95
|
+
user_path(3, { list: [1, 2] }) # => "/users/3?list%5B%5D=1&list%5B%5D=2"
|
96
|
+
```
|
97
|
+
|
98
|
+
**javascript**:
|
99
|
+
```javascript
|
100
|
+
RouteHelpers.userPath(3) // => "/users/3"
|
101
|
+
RouteHelpers.userPath(3, { param: "value" }) // => "/users/3?param=value"
|
102
|
+
RouteHelpers.userPath(3, { param: "value", format: "json" }) // => "/users/3.json?param=value"
|
103
|
+
RouteHelpers.userPath(3, { list: [1, 2] }) // => "/users/3?list%5B%5D=1&list%5B%5D=2"
|
104
|
+
```
|
105
|
+
|
106
|
+
Again, note that JsRouteHelper method names are camelCase to follow javascript convention.
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
1. Fork it ( https://github.com/jordanstephens/js_route_helpers/fork )
|
111
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
112
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
113
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
114
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,90 @@
|
|
1
|
+
var RouteHelpers = (function() {
|
2
|
+
var exports = {},
|
3
|
+
slice = Array.prototype.slice;
|
4
|
+
|
5
|
+
<%= JsRouteHelpers.routes.reduce("") do |memo, (name, route)|
|
6
|
+
spec = route.path.spec.to_s
|
7
|
+
names = route.path.names - ["format"]
|
8
|
+
arguments = names + ["params"]
|
9
|
+
|
10
|
+
memo << <<-JS
|
11
|
+
|
12
|
+
exports.#{name.to_s.camelize(:lower)}Path = function(#{arguments.join(", ")}) {
|
13
|
+
return helper("#{spec}", slice.call(arguments));
|
14
|
+
};
|
15
|
+
JS
|
16
|
+
memo
|
17
|
+
end %>
|
18
|
+
|
19
|
+
// src: https://github.com/knowledgecode/jquery-param
|
20
|
+
function param(a) {
|
21
|
+
var add = function(s, k, v) {
|
22
|
+
v = typeof v === "function" ? v() : v === null ? "" : v === undefined ? "" : v;
|
23
|
+
s[s.length] = encodeURIComponent(k) + "=" + encodeURIComponent(v);
|
24
|
+
}, buildParams = function (prefix, obj, s) {
|
25
|
+
var i, len, key;
|
26
|
+
|
27
|
+
if (Object.prototype.toString.call(obj) === "[object Array]") {
|
28
|
+
for (i = 0, len = obj.length; i < len; i++) {
|
29
|
+
buildParams(prefix + "[" + (typeof obj[i] === "object" ? i : "") + "]", obj[i], s);
|
30
|
+
}
|
31
|
+
} else if (obj && obj.toString() === "[object Object]") {
|
32
|
+
for (key in obj) {
|
33
|
+
if (obj.hasOwnProperty(key)) {
|
34
|
+
if (prefix) {
|
35
|
+
buildParams(prefix + "[" + key + "]", obj[key], s, add);
|
36
|
+
} else {
|
37
|
+
buildParams(key, obj[key], s, add);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
41
|
+
} else if (prefix) {
|
42
|
+
add(s, prefix, obj);
|
43
|
+
} else {
|
44
|
+
for (key in obj) {
|
45
|
+
add(s, key, obj[key]);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
return s;
|
49
|
+
};
|
50
|
+
return buildParams("", a, []).join("&").replace(/%20/g, "+");
|
51
|
+
};
|
52
|
+
|
53
|
+
function filterProps(o, fn) {
|
54
|
+
return Object.keys(o).reduce(function(memo, k) {
|
55
|
+
if (fn(k)) { memo[k] = o[k]; }
|
56
|
+
return memo;
|
57
|
+
}, {});
|
58
|
+
}
|
59
|
+
|
60
|
+
function fillSpecWithParams(spec, args, params) {
|
61
|
+
return spec.replace(/\((.+(:(\w+)))\)/, function(_, p1, p2, p3) {
|
62
|
+
// replace optional tokens (:foo) in spec with values from params
|
63
|
+
if (params[p3]) {
|
64
|
+
// if token matches a key in params, replace the token with the
|
65
|
+
// prop value and remove the prop from params
|
66
|
+
var replacement = params[p3];
|
67
|
+
delete params[p3];
|
68
|
+
return p1.replace(p2, replacement);
|
69
|
+
} else {
|
70
|
+
return "";
|
71
|
+
}
|
72
|
+
}).replace(/:\w+/g, function(_) {
|
73
|
+
// replace required tokens :foo in spec with values from args
|
74
|
+
var replacement = args.shift();
|
75
|
+
if (["string", "number"].indexOf(typeof replacement) === -1) { throw new Error("InvalidArguments"); }
|
76
|
+
return replacement;
|
77
|
+
});
|
78
|
+
}
|
79
|
+
|
80
|
+
function helper(spec, args) {
|
81
|
+
var argsLength = args.length,
|
82
|
+
params = typeof args[args.length - 1] === "object" ? args[args.length - 1] : {},
|
83
|
+
path = fillSpecWithParams(spec, args, params),
|
84
|
+
queryString = param(filterProps(params, function(k) { return params[k] !== null && params[k] !== undefined; }));
|
85
|
+
|
86
|
+
return (queryString.length) ? path.concat("?", queryString) : path;
|
87
|
+
}
|
88
|
+
|
89
|
+
return exports;
|
90
|
+
})();
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "js_route_helpers"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'js_route_helpers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "js_route_helpers"
|
8
|
+
spec.version = JsRouteHelpers::VERSION
|
9
|
+
spec.authors = ["Jordan Stephens"]
|
10
|
+
spec.email = ["iam@jordanstephens.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Use Rails route helpers in your javascript}
|
13
|
+
spec.description = %q{Use Rails route helpers in your javascript}
|
14
|
+
spec.homepage = "http://github.com/jordanstephens/js_route_helpers"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_dependency "railties", ">= 3.1"
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "js_route_helpers/engine"
|
2
|
+
require "js_route_helpers/version"
|
3
|
+
|
4
|
+
module JsRouteHelpers
|
5
|
+
@whitelist_patterns = []
|
6
|
+
|
7
|
+
def self.include(patterns)
|
8
|
+
@whitelist_patterns = patterns
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.routes
|
12
|
+
::Rails.application.reload_routes!
|
13
|
+
::Rails.application.routes.named_routes.routes.select do |name, route|
|
14
|
+
@whitelist_patterns.any? { |r| r.match(name) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js_route_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Stephens
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
description: Use Rails route helpers in your javascript
|
56
|
+
email:
|
57
|
+
- iam@jordanstephens.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- app/assets/javascript/route_helpers.js.erb
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- js_route_helpers.gemspec
|
74
|
+
- lib/js_route_helpers.rb
|
75
|
+
- lib/js_route_helpers/engine.rb
|
76
|
+
- lib/js_route_helpers/version.rb
|
77
|
+
homepage: http://github.com/jordanstephens/js_route_helpers
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Use Rails route helpers in your javascript
|
101
|
+
test_files: []
|