handlebars_routes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ Handlebars Routes
2
+ =================
3
+ Installing
4
+ ----------
5
+ Bundle it
6
+
7
+ gem 'handlebars_routes', '~> 0.0.1'
8
+
9
+ Sprockets require it (in app/assets/javascripts/application.(js|coffee) for example):
10
+
11
+ //= handlebars_routes
12
+
13
+ What it does
14
+ ------------
15
+ 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.
16
+
17
+ I've added my Handlebars Helpers `router` and `link_to`. The `link_to` helper uses the `router` helper to create links in Handlebars templates very much like creating links in Rails templates.
18
+
19
+ The `router` will traverse your object looking for the right attributes to put into the route. You could also use these routes in Backbone models' urls, haven't tried it yet but its a thought.
20
+
21
+ Examples are probably better eh?
22
+ --------------------------------
23
+
24
+ Given the Rails routes:
25
+
26
+ resources :items do
27
+ resources :products
28
+ end
29
+
30
+ Given the data (a Backbone like object):
31
+
32
+ item = { attributes: {
33
+ id: 42,
34
+ product: {id:24, title: 'Pugs not Drugs'},
35
+ item_code: "PUGS",
36
+ created_at:'11/11/2011'
37
+ }
38
+ }
39
+
40
+ And the template item/show.jst.hbs (notice the **triple-stache** around link_to):
41
+
42
+ <tr>
43
+ {{#attributes}}
44
+ <td>{{{link_to product.title "item_product"}}}</td>
45
+ <td> {{item_code}} </td>
46
+ <td> {{created_at}}</td>
47
+ {{/attributes}}
48
+ </tr>
49
+
50
+ And this coffeescript Backbone View:
51
+
52
+ class ItemView extends Backbone.View
53
+ # ...
54
+
55
+ render: =>
56
+ $(@el).prepend JST['item/show'] @model
57
+
58
+ # ...
59
+ end
60
+
61
+ This awesome looking html will be produced!!!
62
+
63
+ <tr>
64
+ <td><a href="/items/42/products/24">Pugs not Drugs</a</td>
65
+ <td>PUGS</td>
66
+ <td>11/11/2011</td>
67
+ </tr>
68
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "handlebars_routes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "handlebars_routes"
7
+ s.version = HandlebarsRoutes::VERSION
8
+ s.authors = ["Tyler Montgomery"]
9
+ s.email = ["tyler.a.montgomery@gmail.com"]
10
+ s.homepage = "https://github.com/ubermajestix/handlebars_routes"
11
+ s.summary = "Rails routes in your javascripts, link_to and route helper for Handlebars"
12
+ s.description = "Rails routes in your javascripts, link_to and route helper for Handlebars"
13
+
14
+ s.rubyforge_project = "handlebars_routes"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "execjs", "~> 1.2.9"
22
+ s.add_runtime_dependency "handlebars_assets", "~> 0.1.3"
23
+
24
+ end
@@ -0,0 +1,11 @@
1
+ require "handlebars_routes/version"
2
+ require 'handlebars_routes/engine'
3
+
4
+ module HandlebarsRoutes
5
+ def self.assets_path
6
+ puts "="*45
7
+ puts 'omg!'
8
+ puts "="*45
9
+ @assets_path ||= File.expand_path('../../vendor/assets/', __FILE__)
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module HandlebarsRoutes
2
+ class Engine < ::Rails::Engine
3
+ initializer "handlebars.routes", :after => "sprockets.handlebars" do |app|
4
+ next unless app.assets
5
+ puts "="*45
6
+ puts 'engine!'
7
+ puts HandlebarsRoutes.assets_path.inspect
8
+ puts "="*45
9
+ app.assets.append_path HandlebarsRoutes.assets_path
10
+
11
+ end
12
+
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module HandlebarsRoutes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ //= require_directory .
@@ -0,0 +1,66 @@
1
+ # Builds a link using the route helper below
2
+ # text - String
3
+ # route_name - String used to lookup route in the rails_routes global object
4
+ #
5
+ Handlebars.registerHelper 'link_to', (text, route_name) ->
6
+ href = Handlebars.helpers['router'](route_name, @)
7
+ template = _.template("<a href='{{href}}'>{{text}}</a>")
8
+ template(text: text, href: href)
9
+
10
+ # Returns what it returns.
11
+ # Grab a named route from the global rails_routes object
12
+ # rails_routes are mustached, so we'll try to match the object's attributes
13
+ # to the attribute names in the route. We'll even recurse into the object
14
+ # if needed.
15
+ #
16
+ # route_name - String to lookup route in the rails_routes global object
17
+ # object - (optional) Object to feed the rails_routes attributes.
18
+ # If you use route helper directly and don't pass object it will
19
+ # be inferred from the current Handlebars context
20
+ # Example:
21
+ # object = {id: 1, title: 'boom', company_id: 5 }
22
+ # route = /companies/:company_id/resources/:id
23
+ # # => /companies/5/resources/1
24
+ #
25
+ # Example:
26
+ # object = {id: 1, title: 'boom', company: { id: 2 }}
27
+ # route = /companies/:company_id/resources/:id
28
+ # # => /companies/2/resources/1
29
+ #
30
+ Handlebars.registerHelper 'router', (route_name, object) ->
31
+ object or= @
32
+ route = rails_routes[route_name]
33
+ if route
34
+ attrs = []
35
+ # extract the attributes from the mustaches in the route
36
+ mustache = /\{\{(.+?)\}\}/g
37
+ route.replace(mustache, (match, code) -> attrs.push code)
38
+ attr_hash = {}
39
+ # build a hash of the attrs we found with the replace function
40
+ # build an array to traverse if the attr has an underscore in it
41
+ # we'll look for nested objects if we don't find a value in the object
42
+ # for the underscored attr.
43
+ _.each attrs, (attr)->
44
+ attr_hash[attr] or= [attr]
45
+ if attr.match(/_/)
46
+ attr_hash[attr].push attr.split("_")
47
+ # for each key in attr_hash, look for the value for that key in object
48
+ # when you find a value, replace the array with the object's value
49
+ # if you don't find anything, raise an error (somehow?)
50
+ # then use the new attr_hash and throw it at _.template
51
+ traverse = (obj, path) ->
52
+ while path.length > 0
53
+ node = path.shift()
54
+ if typeof(obj[node]) == "object"
55
+ return traverse(obj[node], path)
56
+ else
57
+ return obj[node]
58
+ _.each _.keys(attr_hash), (key) ->
59
+ if object[key]?
60
+ attr_hash[key] = object[key]
61
+ else
62
+ attr_hash[key] = traverse(object, _.clone(attr_hash[key][1]))
63
+
64
+ _.template(route)(attr_hash)
65
+
66
+
@@ -0,0 +1,5 @@
1
+ window.rails_routes =
2
+ <% Rails.application.routes.routes.each do |route| %>
3
+ <% next unless route.name %>
4
+ <%= route.name %>: "<%= route.path.gsub(/\(\.:\w+\)/,'').gsub(/:(\w+)/, "{{\\1}}").to_s %>"
5
+ <% end %>
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handlebars_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Montgomery
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: execjs
16
+ requirement: &2165207800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2165207800
25
+ - !ruby/object:Gem::Dependency
26
+ name: handlebars_assets
27
+ requirement: &2165206760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2165206760
36
+ description: Rails routes in your javascripts, link_to and route helper for Handlebars
37
+ email:
38
+ - tyler.a.montgomery@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - README.md
44
+ - Rakefile
45
+ - handlebars_routes.gemspec
46
+ - lib/handlebars_routes.rb
47
+ - lib/handlebars_routes/engine.rb
48
+ - lib/handlebars_routes/version.rb
49
+ - vendor/assets/javascripts/handlebars_routes.coffee
50
+ - vendor/assets/javascripts/handlebars_routes_helpers.coffee
51
+ - vendor/assets/javascripts/handlebars_routes_routes.coffee.erb
52
+ homepage: https://github.com/ubermajestix/handlebars_routes
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project: handlebars_routes
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Rails routes in your javascripts, link_to and route helper for Handlebars
76
+ test_files: []