asset_pipeline_routes 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -20,20 +20,28 @@ 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
|
24
|
-
The path fragments are replaced with Mustache-style attribute bindings by default:
|
23
|
+
in it. Then, in you're javascript file you'd call `r.users_path`. All path fragments are replaced with Mustache-style attribute bindings by default:
|
25
24
|
|
26
25
|
# application.js.coffee.erb
|
27
26
|
userPath = '<%= r.user_path %>' # => yields /users/{{id}}
|
28
27
|
usersPath = '<%= r.users_path %>' # => yields /users
|
29
28
|
|
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
|
29
|
+
You can even hook up member- or collection routes, whatever you like really. Just prefix your routes with `r.` and you can directly use them in your asset-pipeline!
|
31
30
|
|
32
|
-
|
31
|
+
All `_path`-methods take an arbitrary argument which is used to evaluate the final route.
|
32
|
+
So if you want a regexp matching all users-show actions, you can do it just like this:
|
33
33
|
|
34
34
|
# application.js.coffee.erb
|
35
|
-
|
35
|
+
usersPath = '<%= r.user_path '\d+' %>' # => yields /users/\d+
|
36
|
+
|
37
|
+
Sometimes you want to generate the URL for a given resource on the client-side entirely. That's possible as well:
|
38
|
+
|
39
|
+
# application.js.coffee.erb
|
40
|
+
editUserRoute = `<%= r.edit_user_method %>` # => yields anonymous function
|
41
|
+
editUserPath = editUserRoute(42) # => yields /users/42/edit
|
42
|
+
|
43
|
+
Now you have total control over your Rails routes.
|
36
44
|
|
37
45
|
# Addendum
|
38
46
|
|
39
|
-
|
47
|
+
If you happen to use haml\_assets to be able to use HAML in your asset pipeline, you could easily create forms to be used in Backbone.js or similar - because you can add an url option which correctly binds to your context!
|
@@ -2,21 +2,27 @@ require 'active_support/core_ext/string/inflections'
|
|
2
2
|
|
3
3
|
module AssetPipelineRoutes
|
4
4
|
module JsFunctionHelper
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def route_to_anonymous_function route, style = :js
|
8
|
+
function_arguments = route.scan(/:(\w+)/).flatten.map { |param|
|
9
|
+
param.camelcase(:lower)
|
10
|
+
}
|
11
|
+
url_parts = route.split(/:\w+/).map{ |fragment| "'#{fragment}'" }
|
12
|
+
function = if style == :js
|
13
|
+
<<-JS
|
14
|
+
(function() {
|
15
|
+
return function (#{function_arguments.join ', '}) {
|
16
|
+
return #{url_parts.zip(function_arguments).flatten.reject{ |part| part.nil? }.join(' + ')}
|
17
|
+
};
|
18
|
+
}).call(this);
|
17
19
|
JS
|
18
|
-
|
20
|
+
elsif style == :coffee
|
21
|
+
<<-COFFEESCRIPT
|
22
|
+
(-> (#{function_arguments.join ', '}) -> #{url_parts.zip(function_arguments).flatten.reject{ |part| part.nil? }.join(' + ')})(this)
|
23
|
+
COFFEESCRIPT
|
19
24
|
end
|
25
|
+
function.gsub(/\s+/,' ').strip
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -6,14 +6,14 @@ module AssetPipelineRoutes
|
|
6
6
|
def initialize(routes, default_block = '{{\1}}')
|
7
7
|
routes.each do |route|
|
8
8
|
next if route.name.nil? # only handle named_routes
|
9
|
-
|
9
|
+
|
10
10
|
self.class.instance_eval do
|
11
11
|
define_method :"#{route.name}_path" do |id_replacement = default_block|
|
12
12
|
proc { |route, mapping| build_url route, mapping }.curry[route].call id_replacement
|
13
13
|
end
|
14
|
-
|
15
|
-
define_method :"#{route.name}
|
16
|
-
AssetPipelineRoutes::JsFunctionHelper::route_to_anonymous_function build_url
|
14
|
+
|
15
|
+
define_method :"#{route.name}_path_method" do |style = :js|
|
16
|
+
AssetPipelineRoutes::JsFunctionHelper::route_to_anonymous_function build_url(route, ':\1'), style
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -33,27 +33,34 @@ describe AssetPipelineRoutes do
|
|
33
33
|
its(:edit_user_path) { should eql('/users/{{id}}/edit') }
|
34
34
|
it { subject.edit_user_path('\d+').should eql('/users/\d+/edit') }
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
describe 'resources without name' do
|
38
38
|
before { @route = build_route nil, '/foo' }
|
39
39
|
subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
|
40
|
-
|
40
|
+
|
41
41
|
it { subject.should_not_receive(:build_url) }
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
describe 'nested routes' do
|
45
45
|
before { @route = build_route 'project_ticket', '/projects/:project_id/tickets/:id(.:format)' }
|
46
46
|
subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
|
47
|
-
|
47
|
+
|
48
48
|
it { should respond_to(:project_ticket_path) }
|
49
49
|
its(:project_ticket_path) { should eql('/projects/{{project_id}}/tickets/{{id}}') }
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
describe 'javascript method generation' do
|
53
53
|
before { @route = build_route 'user', '/users/:id/edit(.:format)' }
|
54
54
|
subject { AssetPipelineRoutes::RoutesHelper.new [@route] }
|
55
55
|
|
56
|
-
it { should respond_to(:
|
57
|
-
|
56
|
+
it { should respond_to(:user_path_method) }
|
57
|
+
it "should generate JavaScript mapping method" do
|
58
|
+
js_method = "(function() { return function (id) { return '/users/' + id + '/edit' }; }).call(this);"
|
59
|
+
subject.edit_user_path_method.should eql(js_method)
|
60
|
+
end
|
61
|
+
it "should generate CoffeScript mapping method" do
|
62
|
+
coffee_method = "(-> (id) -> '/users/' + id + '/edit')(this)"
|
63
|
+
subject.edit_user_path_method(:coffee).should eql(coffee_method)
|
64
|
+
end
|
58
65
|
end
|
59
66
|
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.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70170979291500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.2.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70170979291500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
requirement: &
|
27
|
+
requirement: &70170979291080 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70170979291080
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70170979290620 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70170979290620
|
47
47
|
description: Add a routes helper for all asset pipeline needs
|
48
48
|
email:
|
49
49
|
- nicolai86@me.com
|