method PageHeaderHelper
def page_header(title, subtitle = nil, &contents)
PageHeader.new(self, title, subtitle, &contents)
end
class PageHeader
attr_reader :context, :title, :subtitle
def initialize(context, title, subtitle = nil)
@context = context
@title = title
@subtitle = subtitle
end
def to_s
context.content_tag(:div, class: "page-header") do
context.content_tag(:h1) do
context.h(title) + subtitle_html
end
end
end
def subtitle_html
context.content_tag(:small, subtitle) if subtitle
end
end
end
Now, your view looks like this:
<%= page_header "Posts", "by Date" %>
And your test looks like this:
require "spec_helper"
describe PageHeaderHelper do
describe "#page_header" do
it "instantiates a PageHeader with the current view context and given arguments"
end
describe PageHeaderHelper::PageHeader do
describe "#subtitle_html" do
it "has some specific behaviour that is helpful to test independantly"
end
end
end