handlebars_assets 0.5.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,11 @@
1
+ ## 0.6.1 (2012-07-23)
2
+
3
+ * #26 - Missing require
4
+
5
+ ## 0.6.0 (yanked)
6
+
7
+ * #25 - Normalize partial names to begin with an underscore
8
+
1
9
  ## 0.5.0 (2012-07-10)
2
10
 
3
- * #24 - Remove "templates/" from template names - @cw-moshe
11
+ * #24 - Remove "templates/" from template names and expand partial paths into names - @cw-moshe
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- handlebars_assets (0.5.0)
4
+ handlebars_assets (0.6.1)
5
5
  execjs (>= 1.2.9)
6
6
  sprockets (>= 2.0.3)
7
7
  tilt
data/README.md CHANGED
@@ -87,14 +87,17 @@ If you begin the name of the template with an underscore, it will be recognized
87
87
 
88
88
  Invoke a {{> partial }}
89
89
 
90
- **Important!** Handlebars does not understand nested partials and neither does this engine. No matter how nested, the partial is named from the asset's basename. The following will lead to much frustration (so don't do it :)
90
+ **Important!** Handlebars does not understand nested partials. To support them, partials are named based on their path using `_` instead `/` (skid => slash). So given:
91
91
 
92
92
  templates/
93
+ _form.hbs
93
94
  contacts/
94
95
  _form.hbs
95
96
  todos/
96
97
  _form.hbs
97
98
 
99
+ You will get three partials named `_form`, `_contacts_form`, and `_todos_form`; note that the partials begin with `_`.
100
+
98
101
  # Thanks
99
102
 
100
103
  This gem is standing on the shoulders of giants.
@@ -0,0 +1,25 @@
1
+ module HandlebarsAssets
2
+ # Change config options in an initializer:
3
+ #
4
+ # HandlebarsAssets::Config.path_prefix = 'app/templates'
5
+ #
6
+ # Or in a block:
7
+ #
8
+ # HandlebarsAssets::Config.configure do |config|
9
+ # path_prefix = 'app/templates'
10
+ # end
11
+
12
+ module Config
13
+ extend self
14
+
15
+ def configure
16
+ yield self
17
+ end
18
+
19
+ attr_writer :path_prefix
20
+
21
+ def path_prefix
22
+ @path_prefix ||= 'templates'
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,4 @@
1
+ require 'handlebars_assets/config'
1
2
  require 'tilt'
2
3
 
3
4
  module HandlebarsAssets
@@ -7,24 +8,22 @@ module HandlebarsAssets
7
8
  end
8
9
 
9
10
  def evaluate(scope, locals, &block)
10
- name = basename(scope.logical_path)
11
- relative_path = scope.logical_path.gsub(/^templates\/(.*)$/i, "\\1")
11
+ template_path = TemplatePath.new(scope)
12
+
12
13
  compiled_hbs = Handlebars.precompile(data)
13
14
 
14
- if name.start_with?('_')
15
- partial_name = relative_path.gsub(/\//, '_').gsub(/__/, '_').dump
15
+ if template_path.is_partial?
16
16
  <<-PARTIAL
17
17
  (function() {
18
- Handlebars.registerPartial(#{partial_name}, Handlebars.template(#{compiled_hbs}));
18
+ Handlebars.registerPartial(#{template_path.name}, Handlebars.template(#{compiled_hbs}));
19
19
  }).call(this);
20
20
  PARTIAL
21
21
  else
22
- template_name = relative_path.dump
23
22
  <<-TEMPLATE
24
23
  (function() {
25
24
  this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
26
- this.HandlebarsTemplates[#{template_name}] = Handlebars.template(#{compiled_hbs});
27
- return HandlebarsTemplates[#{template_name}];
25
+ this.HandlebarsTemplates[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
26
+ return HandlebarsTemplates[#{template_path.name}];
28
27
  }).call(this);
29
28
  TEMPLATE
30
29
  end
@@ -32,10 +31,41 @@ module HandlebarsAssets
32
31
 
33
32
  protected
34
33
 
35
- def basename(path)
36
- path.gsub(%r{.*/}, '')
37
- end
38
-
39
34
  def prepare; end
35
+
36
+
37
+ class TemplatePath
38
+ def initialize(scope)
39
+ self.template_path = scope.logical_path
40
+ end
41
+
42
+ def is_partial?
43
+ template_path.gsub(%r{.*/}, '').start_with?('_')
44
+ end
45
+
46
+ def name
47
+ is_partial? ? partial_name : template_name
48
+ end
49
+
50
+ private
51
+
52
+ attr_accessor :template_path
53
+
54
+ def forced_underscore_name
55
+ '_' + relative_path
56
+ end
57
+
58
+ def relative_path
59
+ template_path.gsub(/^#{HandlebarsAssets::Config.path_prefix}\/(.*)$/i, "\\1")
60
+ end
61
+
62
+ def partial_name
63
+ forced_underscore_name.gsub(/\//, '_').gsub(/__/, '_').dump
64
+ end
65
+
66
+ def template_name
67
+ relative_path.dump
68
+ end
69
+ end
40
70
  end
41
71
  end
@@ -1,3 +1,3 @@
1
1
  module HandlebarsAssets
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'handlebars_assets/config'
2
3
 
3
4
  module HandlebarsAssets
4
5
  class TiltHandlebarsTest < Test::Unit::TestCase
@@ -13,11 +14,11 @@ module HandlebarsAssets
13
14
  end.new
14
15
  end
15
16
 
16
- def hbs_compiled
17
+ def hbs_compiled(template_name)
17
18
  <<END_EXPECTED
18
19
  (function() {
19
20
  this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
20
- this.HandlebarsTemplates["x11"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
21
+ this.HandlebarsTemplates["#{template_name}"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
21
22
  helpers = helpers || Handlebars.helpers;
22
23
  var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
23
24
 
@@ -29,31 +30,84 @@ module HandlebarsAssets
29
30
  else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "handlebars", { hash: {} }); }
30
31
  buffer += escapeExpression(stack1);
31
32
  return buffer;});
32
- return HandlebarsTemplates["x11"];
33
+ return HandlebarsTemplates["#{template_name}"];
34
+ }).call(this);
35
+ END_EXPECTED
36
+ end
37
+
38
+ def hbs_compiled_partial(partial_name)
39
+ <<END_EXPECTED
40
+ (function() {
41
+ Handlebars.registerPartial(\"#{partial_name}\", Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
42
+ helpers = helpers || Handlebars.helpers;
43
+ var buffer = \"\", stack1, foundHelper, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;
44
+
45
+
46
+ buffer += \"This is \";
47
+ foundHelper = helpers.handlebars;
48
+ stack1 = foundHelper || depth0.handlebars;
49
+ if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
50
+ else if(stack1=== undef) { stack1 = helperMissing.call(depth0, \"handlebars\", { hash: {} }); }
51
+ buffer += escapeExpression(stack1);
52
+ return buffer;}));
33
53
  }).call(this);
34
54
  END_EXPECTED
35
55
  end
36
56
 
37
57
  def test_render
38
58
  root = '/myapp/app/assets/templates'
39
- file = 'x11.hbs'
59
+ file = 'test_render.hbs'
40
60
  scope = make_scope root, file
41
61
 
42
62
  template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "This is {{handlebars}}" }
43
63
 
44
- assert_equal hbs_compiled, template.render(scope, {})
64
+ assert_equal hbs_compiled('test_render'), template.render(scope, {})
45
65
  end
46
66
 
47
67
  # Sprockets does not add nested root paths (i.e.
48
68
  # app/assets/javascripts/templates is rooted at app/assets/javascripts)
49
69
  def test_template_misnaming
50
70
  root = '/myapp/app/assets/javascripts'
51
- file = 'templates/x11.hbs'
71
+ file = 'templates/test_template_misnaming.hbs'
52
72
  scope = make_scope root, file
53
73
 
54
74
  template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "This is {{handlebars}}" }
55
75
 
56
- assert_equal hbs_compiled, template.render(scope, {})
76
+ assert_equal hbs_compiled('test_template_misnaming'), template.render(scope, {})
77
+ end
78
+
79
+ def test_path_prefix
80
+ root = '/myapp/app/assets/javascripts'
81
+ file = 'app/templates/test_path_prefix.hbs'
82
+ scope = make_scope root, file
83
+
84
+ HandlebarsAssets::Config.path_prefix = 'app/templates'
85
+
86
+ template = HandlebarsAssets::TiltHandlebars.new(scope.pathname.to_s) { "This is {{handlebars}}" }
87
+
88
+ assert_equal hbs_compiled('test_path_prefix'), template.render(scope, {})
89
+
90
+ HandlebarsAssets::Config.path_prefix = nil
91
+ end
92
+
93
+ def test_underscore_partials
94
+ root = '/myapp/app/assets/javascripts'
95
+ file1 = 'app/templates/_test_underscore.hbs'
96
+ scope1 = make_scope root, file1
97
+ file2 = 'app/templates/some/thing/_test_underscore.hbs'
98
+ scope2 = make_scope root, file2
99
+
100
+ HandlebarsAssets::Config.path_prefix = 'app/templates'
101
+
102
+ template1 = HandlebarsAssets::TiltHandlebars.new(scope1.pathname.to_s) { "This is {{handlebars}}" }
103
+
104
+ assert_equal hbs_compiled_partial('_test_underscore'), template1.render(scope1, {})
105
+
106
+ template2 = HandlebarsAssets::TiltHandlebars.new(scope2.pathname.to_s) { "This is {{handlebars}}" }
107
+
108
+ assert_equal hbs_compiled_partial('_some_thing_test_underscore'), template2.render(scope2, {})
109
+
110
+ HandlebarsAssets::Config.path_prefix = nil
57
111
  end
58
112
  end
59
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-10 00:00:00.000000000 Z
12
+ date: 2012-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: execjs
@@ -93,6 +93,7 @@ files:
93
93
  - Rakefile
94
94
  - handlebars_assets.gemspec
95
95
  - lib/handlebars_assets.rb
96
+ - lib/handlebars_assets/config.rb
96
97
  - lib/handlebars_assets/engine.rb
97
98
  - lib/handlebars_assets/handlebars.rb
98
99
  - lib/handlebars_assets/tilt_handlebars.rb