Skip to content

Instantly share code, notes, and snippets.

@kuboon
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuboon/6b3084e1b87adb9f8fd3 to your computer and use it in GitHub Desktop.
Save kuboon/6b3084e1b87adb9f8fd3 to your computer and use it in GitHub Desktop.
omit
module ApplicationHelper
def omit(val = nil, &block)
unless block_given?
raise 'no parent block' if @_omit.empty?
@_omit[-1] = false unless val.nil?
return val
end
raise "Don't pass a value with block" if val
@_omit ||= []
@_omit.push true
ret = capture(&block)
return nil if @_omit.pop
@_omit[-1] = false unless @_omit.empty?
ret
end
end
RSpec.describe ApplicationHelper do
describe 'omit' do
subject { Haml::Engine.new(haml).def_method(self, :render); render }
context 'empty block' do
let(:haml) { <<HAML
= omit do
%p
HAML
}
it { is_expected.to eq "\n" }
end
context 'block with nil' do
let(:haml) { <<HAML
= omit do
%p= omit nil
HAML
}
it { is_expected.to eq "\n" }
end
context 'block with non-nil value' do
let(:haml) { <<HAML
= omit do
%p= omit 'hoge'
HAML
}
it { is_expected.to eq "<p>hoge</p>\n" }
end
context 'block with empty block' do
let(:haml) { <<HAML
= omit do
= omit do
%span= omit nil
%p= omit nil
HAML
}
it { is_expected.to eq "\n" }
end
context 'block with non-empty block' do
let(:haml) { <<HAML
= omit do
= omit do
%span= omit 'hoge'
%p= omit nil
HAML
}
it { is_expected.to eq "<span>hoge</span><p></p>\n" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment