asset_pipeline_routes 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
@@ -8,27 +8,31 @@ But except for hard-coded links this won't help you, because all resource links
|
|
8
8
|
Heh, you might think! Just call a route helper and pass in a dynamic parameter mapping, like
|
9
9
|
`user_path('{{id}}')`. Sadly this won't yield the desired result! Instead of `/users/{{id}}`, you'll be presented with `/users/%7B%7Bid%7D%7D` because you're mapping just got html_escaped!
|
10
10
|
|
11
|
-
This is where
|
11
|
+
This is where asset\_pipeline\_routes comes to the rescue!
|
12
12
|
|
13
13
|
# What it does
|
14
14
|
|
15
|
-
It adds a helper object to your
|
16
|
-
`r` you can access all your routes you'd normally do, except that all those
|
17
|
-
|
15
|
+
It adds a helper object to your asset-pipeline called `r`. Using
|
16
|
+
`r` you can access all your routes you'd normally do, except that all those path fragments
|
17
|
+
can be changed to whatever you need them to.
|
18
18
|
|
19
19
|
Here's an example, assuming you got a routes.rb with
|
20
20
|
|
21
21
|
resources :users # => yields multiple routes, e.g. /users/:id(.:format)
|
22
22
|
|
23
|
-
in it. Then, in you're javascript file you'd call `r.users_path` instead of `users_path
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
r.
|
23
|
+
in it. Then, in you're javascript file you'd call `r.users_path` instead of `users_path`.
|
24
|
+
The path fragments are replaced with Mustache-style attribute bindings by default:
|
25
|
+
|
26
|
+
# application.js.coffee.erb
|
27
|
+
userPath = '<%= r.user_path %>' # => yields /users/{{id}}
|
28
|
+
usersPath = '<%= r.users_path %>' # => yields /users
|
28
29
|
|
29
30
|
You can even hook up member- or collection routes, whatever you like. Just prefix your routes with `r.` and you can directly use them in your view!
|
30
31
|
|
31
|
-
|
32
|
+
If you don't want the attribute binding pass in whatever argument you like and the path fragments get replaced. Just like that:
|
33
|
+
|
34
|
+
# application.js.coffee.erb
|
35
|
+
userPath = '<%= r.user_path '\d+' %>' # => yields /users/\d+
|
32
36
|
|
33
37
|
# Addendum
|
34
38
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
|
3
|
+
module AssetPipelineRoutes
|
4
|
+
module JsFunctionHelper
|
5
|
+
class << self
|
6
|
+
def route_to_anonymous_function route
|
7
|
+
function_arguments = route.scan(/:(\w+)/).flatten.map { |param|
|
8
|
+
param.camelcase(:lower)
|
9
|
+
}
|
10
|
+
url_parts = route.split(/:\w+/).map{ |fragment| "'#{fragment}'" }
|
11
|
+
function = <<-JS
|
12
|
+
(function() {
|
13
|
+
return function (#{function_arguments.join ', '}) {
|
14
|
+
return #{url_parts.zip(function_arguments).flatten.reject{ |part| part.nil? }.join(' + ')}
|
15
|
+
};
|
16
|
+
}).call(this);
|
17
|
+
JS
|
18
|
+
function.gsub(/\s+/,' ').strip
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative "routes_context"
|
2
|
+
require_relative "js_function_helper"
|
2
3
|
|
3
4
|
module AssetPipelineRoutes
|
4
5
|
class RoutesHelper
|
@@ -10,6 +11,10 @@ module AssetPipelineRoutes
|
|
10
11
|
define_method :"#{route.name}_path" do |id_replacement = default_block|
|
11
12
|
proc { |route, mapping| build_url route, mapping }.curry[route].call id_replacement
|
12
13
|
end
|
14
|
+
|
15
|
+
define_method :"#{route.name}_method" do
|
16
|
+
AssetPipelineRoutes::JsFunctionHelper::route_to_anonymous_function build_url route, ':\1'
|
17
|
+
end
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
@@ -40,4 +40,20 @@ describe AssetPipelineRoutes do
|
|
40
40
|
|
41
41
|
it { subject.should_not_receive(:build_url) }
|
42
42
|
end
|
43
|
+
|
44
|
+
describe 'nested routes' do
|
45
|
+
before { @route = build_route 'project_ticket', '/projects/:project_id/tickets/:id(.:format)' }
|
46
|
+
subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
|
47
|
+
|
48
|
+
it { should respond_to(:project_ticket_path) }
|
49
|
+
its(:project_ticket_path) { should eql('/projects/{{project_id}}/tickets/{{id}}') }
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'javascript method generation' do
|
53
|
+
before { @route = build_route 'user', '/users/:id/edit(.:format)' }
|
54
|
+
subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
|
55
|
+
|
56
|
+
it { should respond_to(:user_method) }
|
57
|
+
its(:edit_user_method) { should eql("(function() { return function (id) { return '/users/' + id + '/edit' }; }).call(this);") }
|
58
|
+
end
|
43
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asset_pipeline_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-30 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70196565622140 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: 3.2.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70196565622140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70196565621720 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70196565621720
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rspec
|
27
|
-
requirement: &
|
38
|
+
requirement: &70196565621260 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,7 +43,7 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70196565621260
|
36
47
|
description: Add a routes helper for all asset pipeline needs
|
37
48
|
email:
|
38
49
|
- nicolai86@me.com
|
@@ -40,6 +51,7 @@ executables: []
|
|
40
51
|
extensions: []
|
41
52
|
extra_rdoc_files: []
|
42
53
|
files:
|
54
|
+
- lib/asset_pipeline_routes/js_function_helper.rb
|
43
55
|
- lib/asset_pipeline_routes/routes_context.rb
|
44
56
|
- lib/asset_pipeline_routes/routes_helper.rb
|
45
57
|
- lib/asset_pipeline_routes/version.rb
|