Last active
August 29, 2015 14:22
-
-
Save henrik/39a7d1e0cf4694d6b5ad to your computer and use it in GitHub Desktop.
Fake helpers for http://thepugautomatic.com/2014/03/draper/#comment-2059556508
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe MyPresenter, "#my_method" do | |
let(:view) { helper_instance } | |
it "works" do | |
actual = MyPresenter.new("whatever").my_method(view) | |
expect(actual).to include "whatever" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MiscHelpers | |
def helper_instance(helper_module = nil) | |
Class.new { | |
include helper_module if helper_module | |
include FakeHelpers | |
}.new | |
end | |
module FakeHelpers | |
def capture | |
@concatenated = [] | |
yielded = yield | |
@concatenated << yielded if yielded.is_a?(String) | |
@concatenated.join | |
end | |
def concat(something) | |
@concatenated << something | |
end | |
def tag(name) | |
"<#{name}>" | |
end | |
def content_tag(name, value_or_attributes, opts={}, &block) | |
if block_given? | |
attributes = value_or_attributes.map { |n, v| %{#{n}="#{v}"} }.join(" ") | |
attributes = attributes.present? ? " #{attributes}" : "" | |
value = yield | |
else | |
attributes = nil | |
value = value_or_attributes | |
end | |
"<#{name}#{attributes}>#{value}</#{name}>" | |
end | |
def link_to(name, url, html_opts = nil) | |
"<a href='#{url}'>#{name}</a>" | |
end | |
def mail_to(email) | |
"<a href='mailto:#{email}'>#{email}</a>" | |
end | |
def asset_path(file) | |
"/assets/#{file}" | |
end | |
def simple_format(text) | |
"<p>%s</p>" % text.gsub("\n", "<br>") | |
end | |
def truncate(text, opts = {}) | |
text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment