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

Add Sinatra::HamlHelpers to sinatra-contrib #1960

Merged
merged 7 commits into from
Dec 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions sinatra-contrib/lib/sinatra/contrib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Common
# Other extensions you don't want to be loaded unless needed.
module Custom
register :Reloader, 'sinatra/reloader'

helpers :HamlHelpers, 'sinatra/haml_helpers'
end

##
Expand Down
50 changes: 50 additions & 0 deletions sinatra-contrib/lib/sinatra/haml_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'sinatra/base'
require 'sinatra/capture'

module Sinatra
# = Sinatra::HamlHelpers
#
# This extension provides some of the helper methods that existed in Haml 5
# but were removed in Haml 6. To use this in your app, just +register+ it:
#
# require 'sinatra/base'
# require 'sinatra/haml_helpers'
#
# class Application < Sinatra::Base
# register Sinatra::HamlHelpers
#
# # now you can use the helpers in your views
# get '/' do
# haml_code = <<~HAML
# %p
# != surround "(", ")" do
# %a{ href: "https://example.org/" } example.org
# HAML
# haml haml_code
# end
# end
#
module HamlHelpers
include Sinatra::Capture

def surround(front, back = front, &block)
"#{front}#{_capture_haml(&block).chomp}#{back}\n"
end

def precede(str, &block)
"#{str}#{_capture_haml(&block).chomp}\n"
end

def succeed(str, &block)
"#{_capture_haml(&block).chomp}#{str}\n"
end

def _capture_haml(*args, &block)
capture(*args, &block)
end
end

helpers HamlHelpers
end
77 changes: 77 additions & 0 deletions sinatra-contrib/spec/haml_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'haml'
require 'spec_helper'
require 'sinatra/haml_helpers'

RSpec.describe Sinatra::HamlHelpers do
describe "#surround" do
it "renders correctly" do
mock_app do
helpers Sinatra::HamlHelpers
get "/" do
haml_code = <<~HAML
%p
!= surround "(", ")" do
%a{ href: "https://example.org/" } surrounded
HAML
haml haml_code
end
end

get "/"
html_code = <<~HTML
<p>
(<a href='https://example.org/'>surrounded</a>)
</p>
HTML
expect(body).to eq(html_code)
end
end

describe "#precede" do
it "renders correctly" do
mock_app do
helpers Sinatra::HamlHelpers
get "/" do
haml_code = <<~HAML
%p
!= precede "* " do
%a{ href: "https://example.org/" } preceded
HAML
haml haml_code
end
end

get "/"
html_code = <<~HTML
<p>
* <a href='https://example.org/'>preceded</a>
</p>
HTML
expect(body).to eq(html_code)
end
end

describe "#succeed" do
it "renders correctly" do
mock_app do
helpers Sinatra::HamlHelpers
get "/" do
haml_code = <<~HAML
%p
!= succeed "." do
%a{ href: "https://example.org/" } succeeded
HAML
haml haml_code
end
end

get "/"
html_code = <<~HTML
<p>
<a href='https://example.org/'>succeeded</a>.
</p>
HTML
expect(body).to eq(html_code)
end
end
end