Skip to content

Commit 4ee07c4

Browse files
authored
Merge pull request #26 from gi/issue-25-ruby-helper-function-options
[#25] engine: fix serialization of helper options functions to ruby
2 parents e310cc6 + cd1218b commit 4ee07c4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

lib/handlebars/engine.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def register_helper(name = nil, function = nil, **helpers, &block)
8484
case f
8585
when Proc
8686
attach(n, &f)
87-
evaluate("registerHelper('#{n}', #{n})")
87+
evaluate("registerRbHelper('#{n}', #{n})")
8888
when String, Symbol
89-
evaluate("Handlebars.registerHelper('#{n}', #{f})")
89+
evaluate("registerJsHelper('#{n}', #{f})")
9090
end
9191
end
9292
end

lib/handlebars/engine/init.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ var template = (spec) => {
1616
var registerPartial = Handlebars.registerPartial.bind(Handlebars);
1717
var unregisterPartial = Handlebars.unregisterPartial.bind(Handlebars);
1818

19-
var registerHelper = (...args) => {
20-
const fn = args[args.length - 1];
19+
var registerJsHelper = Handlebars.registerHelper.bind(Handlebars);
20+
21+
var registerRbHelper = (name, fn) => {
2122
function wrapper(...args) {
23+
// Ruby cannot access the `this` context, so pass it as the first argument.
2224
args.unshift(this);
25+
const { ...options } = args[args.length-1];
26+
Object.entries(options).forEach(([key, value]) => {
27+
if (typeof value === "function") {
28+
// functions are cannot be passed back to Ruby
29+
options[key] = "function";
30+
}
31+
});
32+
args[args.length-1] = options
2333
return fn(...args);
2434
}
25-
args[args.length - 1] = wrapper;
26-
return Handlebars.registerHelper(...args);
35+
return registerJsHelper(name, wrapper);
2736
};
2837

2938
var unregisterHelper = Handlebars.unregisterHelper.bind(Handlebars);

spec/handlebars/engine_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
describe "the options" do
256256
it "includes the main block function" do
257257
opts = include(
258-
"fn" => kind_of(MiniRacer::JavaScriptFunction),
258+
"fn" => "function", # kind_of(MiniRacer::JavaScriptFunction),
259259
)
260260
args = [anything, any_args, opts]
261261
render
@@ -264,7 +264,7 @@
264264

265265
it "includes the else block function" do
266266
opts = include(
267-
"inverse" => kind_of(MiniRacer::JavaScriptFunction),
267+
"inverse" => "function", # kind_of(MiniRacer::JavaScriptFunction),
268268
)
269269
args = [anything, any_args, opts]
270270
render
@@ -279,6 +279,14 @@
279279
<<~JS
280280
function (...args) {
281281
args.unshift(this);
282+
const { ...options } = args[args.length-1];
283+
Object.entries(options).forEach(([key, value]) => {
284+
if (typeof value === "function") {
285+
// functions are cannot be passed back to Ruby
286+
options[key] = "function";
287+
}
288+
});
289+
args[args.length-1] = options
282290
return tester(...args);
283291
}
284292
JS

0 commit comments

Comments
 (0)