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 2 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
14 changes: 10 additions & 4 deletions app/helpers/opal_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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
end
end

def javascript_include_tag(*sources)
Expand Down
35 changes: 29 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 Down