handlebars_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
@@ -1,15 +1,24 @@
|
|
1
1
|
Handlebars Routes
|
2
2
|
=================
|
3
|
+
|
3
4
|
Installing
|
4
5
|
----------
|
6
|
+
|
5
7
|
Bundle it
|
6
8
|
|
7
9
|
gem 'handlebars_routes', '~> 0.0.1'
|
8
10
|
|
9
11
|
Sprockets require it (in app/assets/javascripts/application.(js|coffee) for example):
|
10
|
-
|
12
|
+
|
11
13
|
//= handlebars_routes
|
12
14
|
|
15
|
+
The `link_to` helper uses [underscore.js](https://github.com/rweng/underscore-rails) templating and assumes you have put it into "mustache mode"
|
16
|
+
|
17
|
+
# Setup underscore's templating to use Mustache style templates
|
18
|
+
_.templateSettings =
|
19
|
+
interpolate: /\{\{(.+?)\}\}/g
|
20
|
+
|
21
|
+
|
13
22
|
What it does
|
14
23
|
------------
|
15
24
|
Its really annoying not to have all your Rails routes on the client side especially when you're doing Javascript templating. This gem aims to fix that by dumping your rails routes into a `rails_routes` object in javascript.
|
@@ -22,11 +31,11 @@ Examples are probably better eh?
|
|
22
31
|
--------------------------------
|
23
32
|
|
24
33
|
Given the Rails routes:
|
25
|
-
|
34
|
+
|
26
35
|
resources :items do
|
27
36
|
resources :products
|
28
37
|
end
|
29
|
-
|
38
|
+
|
30
39
|
Given the data (a Backbone like object):
|
31
40
|
|
32
41
|
item = { attributes: {
|
@@ -36,9 +45,12 @@ Given the data (a Backbone like object):
|
|
36
45
|
created_at:'11/11/2011'
|
37
46
|
}
|
38
47
|
}
|
39
|
-
|
40
|
-
And the template item/show.jst.hbs (notice the **triple-stache** around link_to)
|
41
|
-
|
48
|
+
|
49
|
+
And the template item/show.jst.hbs (notice the **triple-stache** around link_to).
|
50
|
+
`"item_product"` is the route that Rails provided. The list of routes
|
51
|
+
are stored in the `rails_routes` global javascript object. Take a look at
|
52
|
+
it in Inspector to find specific routes.
|
53
|
+
|
42
54
|
<tr>
|
43
55
|
{{#attributes}}
|
44
56
|
<td>{{{link_to product.title "item_product"}}}</td>
|
@@ -51,10 +63,10 @@ And this coffeescript Backbone View:
|
|
51
63
|
|
52
64
|
class ItemView extends Backbone.View
|
53
65
|
# ...
|
54
|
-
|
66
|
+
|
55
67
|
render: =>
|
56
68
|
$(@el).prepend JST['item/show'] @model
|
57
|
-
|
69
|
+
|
58
70
|
# ...
|
59
71
|
end
|
60
72
|
|
@@ -65,4 +77,4 @@ This awesome looking html will be produced!!!
|
|
65
77
|
<td>PUGS</td>
|
66
78
|
<td>11/11/2011</td>
|
67
79
|
</tr>
|
68
|
-
|
80
|
+
|
data/handlebars_routes.gemspec
CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_runtime_dependency "execjs", "~> 1.
|
21
|
+
s.add_runtime_dependency "execjs", "~> 1.3"
|
22
22
|
s.add_runtime_dependency "handlebars_assets", "~> 0.1.3"
|
23
|
+
s.add_runtime_dependency "underscore-rails", "~> 1.3"
|
23
24
|
|
24
25
|
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module HandlebarsRoutes
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
@@ -7,7 +7,6 @@ Handlebars.registerHelper 'link_to', (text, route_name) ->
|
|
7
7
|
template = _.template("<a href='{{href}}'>{{text}}</a>")
|
8
8
|
template(text: text, href: href)
|
9
9
|
|
10
|
-
# Returns what it returns.
|
11
10
|
# Grab a named route from the global rails_routes object
|
12
11
|
# rails_routes are mustached, so we'll try to match the object's attributes
|
13
12
|
# to the attribute names in the route. We'll even recurse into the object
|
@@ -20,13 +19,16 @@ Handlebars.registerHelper 'link_to', (text, route_name) ->
|
|
20
19
|
# Example:
|
21
20
|
# object = {id: 1, title: 'boom', company_id: 5 }
|
22
21
|
# route = /companies/:company_id/resources/:id
|
22
|
+
# router("company_resource", object)
|
23
23
|
# # => /companies/5/resources/1
|
24
24
|
#
|
25
25
|
# Example:
|
26
26
|
# object = {id: 1, title: 'boom', company: { id: 2 }}
|
27
27
|
# route = /companies/:company_id/resources/:id
|
28
|
+
# router("company_resource", object)
|
28
29
|
# # => /companies/2/resources/1
|
29
30
|
#
|
31
|
+
# Returns a String.
|
30
32
|
Handlebars.registerHelper 'router', (route_name, object) ->
|
31
33
|
object or= @
|
32
34
|
route = rails_routes[route_name]
|
@@ -40,6 +42,11 @@ Handlebars.registerHelper 'router', (route_name, object) ->
|
|
40
42
|
# build an array to traverse if the attr has an underscore in it
|
41
43
|
# we'll look for nested objects if we don't find a value in the object
|
42
44
|
# for the underscored attr.
|
45
|
+
# For example if one of the attrs is "company_id" we'll look for
|
46
|
+
# the top level key "company_id" in object or look for the key
|
47
|
+
# "company" and then look for the key "id" inside the object at
|
48
|
+
# "company". The example in the comments above shows you both
|
49
|
+
# scenarios.
|
43
50
|
_.each attrs, (attr)->
|
44
51
|
attr_hash[attr] or= [attr]
|
45
52
|
if attr.match(/_/)
|
@@ -55,12 +62,13 @@ Handlebars.registerHelper 'router', (route_name, object) ->
|
|
55
62
|
return traverse(obj[node], path)
|
56
63
|
else
|
57
64
|
return obj[node]
|
65
|
+
|
58
66
|
_.each _.keys(attr_hash), (key) ->
|
59
67
|
if object[key]?
|
60
68
|
attr_hash[key] = object[key]
|
61
69
|
else
|
62
70
|
attr_hash[key] = traverse(object, _.clone(attr_hash[key][1]))
|
63
|
-
|
71
|
+
|
64
72
|
_.template(route)(attr_hash)
|
65
73
|
|
66
|
-
|
74
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebars_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,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-04-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: execjs
|
16
|
-
requirement: &
|
16
|
+
requirement: &70167036036660 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.
|
21
|
+
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70167036036660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: handlebars_assets
|
27
|
-
requirement: &
|
27
|
+
requirement: &70167036035480 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,18 @@ dependencies:
|
|
32
32
|
version: 0.1.3
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70167036035480
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: underscore-rails
|
38
|
+
requirement: &70167036034860 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.3'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70167036034860
|
36
47
|
description: Rails routes in your javascripts, link_to and route helper for Handlebars
|
37
48
|
email:
|
38
49
|
- tyler.a.montgomery@gmail.com
|
@@ -69,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
80
|
version: '0'
|
70
81
|
requirements: []
|
71
82
|
rubyforge_project: handlebars_routes
|
72
|
-
rubygems_version: 1.8.
|
83
|
+
rubygems_version: 1.8.5
|
73
84
|
signing_key:
|
74
85
|
specification_version: 3
|
75
86
|
summary: Rails routes in your javascripts, link_to and route helper for Handlebars
|