Skip to content

Commit 15191e6

Browse files
authored
Merge pull request #131 from dscataglini/mirror-javascript_tag-api
opal_tag should mirror the javascript_tag api
2 parents 1959949 + 744ed6b commit 15191e6

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

app/helpers/opal_helper.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
require 'opal/sprockets'
22

33
module OpalHelper
4-
def opal_tag(opal_code = nil, &block)
5-
opal_code ||= capture(&block)
4+
def opal_tag(opal_code_or_options = nil, html_options = {}, &block)
5+
if block_given?
6+
html_options = opal_code_or_options if opal_code_or_options.is_a?(Hash)
7+
opal_code_or_options = capture(&block)
8+
end
9+
610
compiler_options = Opal::Config.compiler_options.merge(requirable: false)
7-
compiler = Opal::Compiler.new(opal_code, compiler_options)
11+
compiler = Opal::Compiler.new(opal_code_or_options, compiler_options)
812
js_code = compiler.compile
9-
javascript_tag js_code
13+
javascript_tag html_options do
14+
js_code.html_safe
15+
end
1016
end
1117

1218
def javascript_include_tag(*sources)
1319
options = sources.extract_options!.symbolize_keys
14-
debug = options[:debug] != false
20+
debug = options.delete(:debug) != false
1521
skip_loader = options.delete(:skip_opal_loader)
1622
force_opal_loader_tag = options.delete(:force_opal_loader_tag) || debug
1723

@@ -24,7 +30,7 @@ def javascript_include_tag(*sources)
2430

2531
if force_opal_loader_tag
2632
script_tags << super(source, options)
27-
script_tags << "\n".html_safe + javascript_tag(loading_code)
33+
script_tags << "\n".html_safe + javascript_tag(loading_code, options)
2834
else
2935
script_tags << super(source, options.merge(onload: loading_code))
3036
end

spec/helpers/opal_helper_spec.rb

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,38 @@
66
let(:helper) { view }
77

88
describe '#opal_tag' do
9-
it 'compiles to js' do
10-
allow(helper).to receive(:javascript_tag) { |code| code }
11-
ruby_code = 'puts 5'
12-
13-
expect(Opal::Compiler).to receive(:new)
9+
let(:ruby_code) { 'puts 5' }
10+
let(:compiled_ruby_code) { 'self.$puts(5)' }
11+
let(:html_options) { { async: true } }
12+
before do
13+
allow(helper).to receive(:javascript_tag).and_call_original
14+
allow(Opal::Compiler).to receive(:new)
1415
.with(ruby_code, hash_including(requirable: false))
1516
.and_call_original
17+
end
18+
19+
context 'when the ruby code is passed inline' do
20+
it 'compiles the ruby code to js' do
21+
expect(helper.opal_tag(ruby_code)).to include(compiled_ruby_code)
22+
end
23+
24+
it 'passes the html_options to the javascript_tag' do
25+
helper.opal_tag(ruby_code, html_options)
26+
expect(helper).to have_received(:javascript_tag).with(html_options)
27+
end
28+
end
29+
30+
context 'when the ruby code is passed as a block' do
31+
it 'compiles the block to js' do
32+
expect(helper.opal_tag { ruby_code }).to include(compiled_ruby_code)
33+
end
1634

17-
expect(helper.opal_tag(ruby_code)).to include('self.$puts(5)')
35+
it 'uses the options as the first argument' do
36+
aggregate_failures do
37+
expect(helper.opal_tag(html_options) { ruby_code }).to include(compiled_ruby_code)
38+
expect(helper).to have_received(:javascript_tag).with(html_options)
39+
end
40+
end
1841
end
1942
end
2043

@@ -32,8 +55,13 @@
3255
%(<script>), %(//<![CDATA[), loading_code, %(//]]>), %(</script>),
3356
].join("\n")
3457

58+
loading_code_with_options_in_script_tag = [
59+
%(<script defer="defer">), %(//<![CDATA[), loading_code, %(//]]>), %(</script>),
60+
].join("\n")
61+
3562
expect(helper.javascript_include_tag('application', debug: true)).to include(loading_code_in_script_tag)
3663
expect(helper.javascript_include_tag('application', debug: true)).not_to include(escaped_loading_code)
64+
expect(helper.javascript_include_tag('application', debug: true, defer: true)).not_to include(escaped_loading_code)
3765

3866
expect(helper.javascript_include_tag('application', debug: false)).to include(escaped_loading_code)
3967
expect(helper.javascript_include_tag('application', debug: false)).not_to include(loading_code_in_script_tag)

0 commit comments

Comments
 (0)