handlebars-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@railshbs
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 James A. Rosen and Yehuda Katz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ ## What ##
2
+
3
+ Use the awesome [Handlebars.js](https://github.com/wycats/handlebars.js)
4
+ both server- and client-side.
5
+
6
+ ## Why ##
7
+
8
+ DRY. That's why.
9
+
10
+ ## How ##
11
+
12
+ First, grab the copy of handlebars you are using and copy it into
13
+ `vendor/javascripts/handlebars.js`. If you want, you can symlink
14
+ it from your `public` directory.
15
+
16
+ In `app/controllers/blogs_controller.rb`:
17
+
18
+ BlogsController
19
+ def show
20
+ @blog = Blog.find(params[:id])
21
+ end
22
+ end
23
+
24
+ In `app/views/blogs/show.html.hbs`:
25
+
26
+ <article>
27
+ <header>
28
+ <h1>{{{ blog/title }}}</h1>
29
+ </header>
30
+ {{{ blog/body }}}
31
+ <footer>
32
+ by {{{ link_to blog/author/name blog/author }}}
33
+ </article>
34
+
35
+ ### Usage Gotchas ###
36
+
37
+ * Template line numbers may not match stack trace line numbers. This
38
+ will be resolved upstream.
39
+ * Block helpers do not currently work.
40
+
41
+ #### HTML-Safety ####
42
+
43
+ Rails returns HTML-safe strings, but the string-safety information
44
+ isn't passed into Handlebars, so Handlebars re-escapes the content.
45
+ To get around this, use the triple-stash (`{{{ ... }}}`) when
46
+ using a Rails helper.
47
+ See [issue 2](https://github.com/jamesarosen/handlebars-rails/issues/#issue/2).
48
+
49
+ #### Rails Helpers ####
50
+
51
+ Rails helpers obviously do not exist in the client-side JS context.
52
+ This means that if you use `{{{ link_to ... }}}`, it can only be run server-side.
53
+ The solution is to implement a minimal `link_to` in the client-side.
54
+ See [issue 4](https://github.com/jamesarosen/handlebars-rails/issues/#issue/4)
55
+ and [issue 5](https://github.com/jamesarosen/handlebars-rails/issues/#issue/5).
56
+
57
+ ## Credits ##
58
+
59
+ Yehuda Katz did all the heavy lifting to get this off the ground,
60
+ both in terms of writing
61
+ [Handlebars.js](https://github.com/wycats/handlebars.js) and the
62
+ template handler here.
63
+ Additional huge props to Charles Lowell for
64
+ [therubyracer](https://github.com/cowboyd/therubyracer),
65
+ a *sine qua non* for this project.
@@ -0,0 +1,3 @@
1
+ * tests (yikes!)
2
+ * deal with line numbers not matching (may need to be dealt with in
3
+ handlebars proper)
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'handlebars-rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.version = Handlebars::VERSION
8
+ gem.name = 'handlebars-rails'
9
+ gem.files = `git ls-files`.split("\n")
10
+ gem.summary = "Rails Template Handler for Handlebars"
11
+ gem.description = "Use Handlebars.js client- and server-side"
12
+ gem.email = "james.a.rosen@gmail.com"
13
+ gem.homepage = "http://github.com/jamesarosen/arturo"
14
+ gem.authors = ["James A. Rosen", "Yehuda Katz"]
15
+ gem.test_files = []
16
+ gem.require_paths = [".", "lib"]
17
+ gem.has_rdoc = 'false'
18
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
19
+ gem.specification_version = 2
20
+ gem.add_runtime_dependency 'rails', '~> 3.0'
21
+ gem.add_runtime_dependency 'therubyracer', '~> 0.8.0'
22
+ gem.add_development_dependency 'mocha'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'redgreen', '~> 1.2'
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'handlebars-rails/version'
2
+ require 'handlebars-rails/v8'
3
+ require "active_support"
4
+
5
+ module Handlebars
6
+ class TemplateHandler
7
+
8
+ def self.js
9
+ handlebars = File.join(Rails.root, "vendor", "javascripts", "handlebars.js")
10
+
11
+ unless File.exists?(handlebars)
12
+ raise "Could not find handlebars.js. Please copy it to #{handlebars}"
13
+ end
14
+
15
+ Thread.current[:v8_context] ||= begin
16
+ V8::Context.new do |js|
17
+ js.load(handlebars)
18
+ js.eval("Templates = {}")
19
+
20
+ js["puts"] = method(:puts)
21
+
22
+ js.eval(%{
23
+ Handlebars.registerHelper('helperMissing', function(helper) {
24
+ var params = Array.prototype.slice.call(arguments, 1);
25
+ return actionview[helper].apply(actionview, params);
26
+ })
27
+ })
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.call(template)
33
+ # Here, we're sticking the compiled template somewhere in V8 where
34
+ # we can get back to it
35
+ js.eval(%{Templates["#{template.identifier}"] = Handlebars.compile(#{template.source.inspect}) })
36
+
37
+ %{
38
+ js = ::Handlebars::TemplateHandler.js
39
+ js['actionview'] = self
40
+ js.eval("Templates['#{template.identifier}']").call(assigns)
41
+ }
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+ ActiveSupport.on_load(:action_view) do
48
+ ActionView::Template.register_template_handler(:hbs, ::Handlebars::TemplateHandler)
49
+ end
@@ -0,0 +1,29 @@
1
+ require 'v8'
2
+
3
+ # Monkey patches due to bugs in RubyRacer
4
+ class V8::JSError
5
+ def initialize(try, to)
6
+ @to = to
7
+ begin
8
+ super(initialize_unsafe(try))
9
+ rescue Exception => e
10
+ # Original code does not make an Array here
11
+ @boundaries = [Boundary.new(:rbframes => e.backtrace)]
12
+ @value = e
13
+ super("BUG! please report. JSError#initialize failed!: #{e.message}")
14
+ end
15
+ end
16
+
17
+ def parse_js_frames(try)
18
+ raw = @to.rb(try.StackTrace())
19
+ if raw && !raw.empty?
20
+ raw.split("\n")[1..-1].tap do |frames|
21
+ # Original code uses strip!, and the frames are not guaranteed to be strippable
22
+ frames.each {|frame| frame.strip.chomp!(",")}
23
+ end
24
+ else
25
+ []
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,3 @@
1
+ module Handlebars
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handlebars-rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - James A. Rosen
13
+ - Yehuda Katz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-05 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: therubyracer
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 8
46
+ - 0
47
+ version: 0.8.0
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: mocha
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: redgreen
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 1
86
+ - 2
87
+ version: "1.2"
88
+ type: :development
89
+ version_requirements: *id005
90
+ description: Use Handlebars.js client- and server-side
91
+ email: james.a.rosen@gmail.com
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files: []
97
+
98
+ files:
99
+ - .rvmrc
100
+ - Gemfile
101
+ - LICENSE
102
+ - README.md
103
+ - TODO.markdown
104
+ - handlebars-rails.gemspec
105
+ - lib/handlebars-rails.rb
106
+ - lib/handlebars-rails/v8.rb
107
+ - lib/handlebars-rails/version.rb
108
+ has_rdoc: true
109
+ homepage: http://github.com/jamesarosen/arturo
110
+ licenses: []
111
+
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - .
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ requirements: []
135
+
136
+ rubyforge_project:
137
+ rubygems_version: 1.3.7
138
+ signing_key:
139
+ specification_version: 2
140
+ summary: Rails Template Handler for Handlebars
141
+ test_files: []
142
+