Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opal_tag should mirror the javascript_tag api #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions app/helpers/opal_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
require 'opal/sprockets'

module OpalHelper
def opal_tag(opal_code = nil, &block)
opal_code ||= capture(&block)
def opal_tag(opal_code_or_options = nil, html_options = {}, &block)
if block_given?
html_options = opal_code_or_options if opal_code_or_options.is_a?(Hash)
opal_code_or_options = capture(&block)
end

compiler_options = Opal::Config.compiler_options.merge(requirable: false)
compiler = Opal::Compiler.new(opal_code, compiler_options)
compiler = Opal::Compiler.new(opal_code_or_options, compiler_options)
js_code = compiler.compile
javascript_tag js_code
javascript_tag html_options do
js_code.html_safe
end
end

def javascript_include_tag(*sources)
options = sources.extract_options!.symbolize_keys
debug = options[:debug] != false
debug = options.delete(:debug) != false
skip_loader = options.delete(:skip_opal_loader)
force_opal_loader_tag = options.delete(:force_opal_loader_tag) || debug

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

if force_opal_loader_tag
script_tags << super(source, options)
script_tags << "\n".html_safe + javascript_tag(loading_code)
script_tags << "\n".html_safe + javascript_tag(loading_code, options)
else
script_tags << super(source, options.merge(onload: loading_code))
end
Expand Down
40 changes: 34 additions & 6 deletions spec/helpers/opal_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,38 @@
let(:helper) { view }

describe '#opal_tag' do
it 'compiles to js' do
allow(helper).to receive(:javascript_tag) { |code| code }
ruby_code = 'puts 5'

expect(Opal::Compiler).to receive(:new)
let(:ruby_code) { 'puts 5' }
let(:compiled_ruby_code) { 'self.$puts(5)' }
let(:html_options) { { async: true } }
before do
allow(helper).to receive(:javascript_tag).and_call_original
allow(Opal::Compiler).to receive(:new)
.with(ruby_code, hash_including(requirable: false))
.and_call_original
end

context 'when the ruby code is passed inline' do
it 'compiles the ruby code to js' do
expect(helper.opal_tag(ruby_code)).to include(compiled_ruby_code)
end

it 'passes the html_options to the javascript_tag' do
helper.opal_tag(ruby_code, html_options)
expect(helper).to have_received(:javascript_tag).with(html_options)
end
end

context 'when the ruby code is passed as a block' do
it 'compiles the block to js' do
expect(helper.opal_tag { ruby_code }).to include(compiled_ruby_code)
end

expect(helper.opal_tag(ruby_code)).to include('self.$puts(5)')
it 'uses the options as the first argument' do
aggregate_failures do
expect(helper.opal_tag(html_options) { ruby_code }).to include(compiled_ruby_code)
expect(helper).to have_received(:javascript_tag).with(html_options)
end
end
end
end

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

loading_code_with_options_in_script_tag = [
%(<script defer="defer">), %(//<![CDATA[), loading_code, %(//]]>), %(</script>),
].join("\n")

expect(helper.javascript_include_tag('application', debug: true)).to include(loading_code_in_script_tag)
expect(helper.javascript_include_tag('application', debug: true)).not_to include(escaped_loading_code)
expect(helper.javascript_include_tag('application', debug: true, defer: true)).not_to include(escaped_loading_code)

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