Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/handlebars/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def register_helper(name = nil, function = nil, **helpers, &block)
case f
when Proc
attach(n, &f)
evaluate("registerHelper('#{n}', #{n})")
evaluate("registerRbHelper('#{n}', #{n})")
when String, Symbol
evaluate("Handlebars.registerHelper('#{n}', #{f})")
evaluate("registerJsHelper('#{n}', #{f})")
end
end
end
Expand Down
17 changes: 13 additions & 4 deletions lib/handlebars/engine/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ var template = (spec) => {
var registerPartial = Handlebars.registerPartial.bind(Handlebars);
var unregisterPartial = Handlebars.unregisterPartial.bind(Handlebars);

var registerHelper = (...args) => {
const fn = args[args.length - 1];
var registerJsHelper = Handlebars.registerHelper.bind(Handlebars);

var registerRbHelper = (name, fn) => {
function wrapper(...args) {
// Ruby cannot access the `this` context, so pass it as the first argument.
args.unshift(this);
const { ...options } = args[args.length-1];
Object.entries(options).forEach(([key, value]) => {
if (typeof value === "function") {
// functions are cannot be passed back to Ruby
options[key] = "function";
}
});
args[args.length-1] = options
return fn(...args);
}
args[args.length - 1] = wrapper;
return Handlebars.registerHelper(...args);
return registerJsHelper(name, wrapper);
};

var unregisterHelper = Handlebars.unregisterHelper.bind(Handlebars);
Expand Down
12 changes: 10 additions & 2 deletions spec/handlebars/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
describe "the options" do
it "includes the main block function" do
opts = include(
"fn" => kind_of(MiniRacer::JavaScriptFunction),
"fn" => "function", # kind_of(MiniRacer::JavaScriptFunction),
)
args = [anything, any_args, opts]
render
Expand All @@ -264,7 +264,7 @@

it "includes the else block function" do
opts = include(
"inverse" => kind_of(MiniRacer::JavaScriptFunction),
"inverse" => "function", # kind_of(MiniRacer::JavaScriptFunction),
)
args = [anything, any_args, opts]
render
Expand All @@ -279,6 +279,14 @@
<<~JS
function (...args) {
args.unshift(this);
const { ...options } = args[args.length-1];
Object.entries(options).forEach(([key, value]) => {
if (typeof value === "function") {
// functions are cannot be passed back to Ruby
options[key] = "function";
}
});
args[args.length-1] = options
return tester(...args);
}
JS
Expand Down
Loading