Created
December 25, 2009 02:04
-
-
Save ydnar/263465 to your computer and use it in GitHub Desktop.
Implementation of the wrapper pattern for Rails / ActionView
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
# Implementation of the wrapper pattern, borrowed from Template Toolkit: | |
# http://template-toolkit.org/docs/manual/Directives.html#section_WRAPPER | |
# | |
# The method captures a block and passes its content to a partial template. | |
# The partial template calls <%= yield %> to render the block content, e.g.: | |
# | |
# <% wrapper "shared/widget", { :title => "Hello" } do %> | |
# ...some ERb... | |
# <% end %> | |
# | |
# In shared/_widget.html.erb: | |
# | |
# <div class="widget"> | |
# <h3><%= title %></h3> | |
# <div class="content"> | |
# <%= yield %> | |
# </div> | |
# </div> | |
module WrapperHelper | |
def wrapper(template_name, local_assigns = {}, content = nil, &block) | |
content = capture(&block) if block_given? | |
instance_variable_set(:@content_for_layout, content) | |
concat render_partial(:partial => template_name, :locals => local_assigns) | |
nil | |
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
require 'spec_helper' | |
describe WrapperHelper do | |
before do | |
helper.stub!(:render_partial) | |
helper.stub!(:capture) | |
helper.stub!(:concat) | |
end | |
describe "#wrapper" do | |
context "without a block" do | |
it "assigns @content_for_layout to the supplied value" do | |
helper.should_receive(:instance_variable_set).with(:@content_for_layout, "content") | |
helper.wrapper("template", nil, "content") | |
end | |
it "renders the requested partial template and locals" do | |
local_assigns = {:foo => "bar"} | |
helper.should_receive(:render_partial).with(:partial => "template", :locals => local_assigns) | |
helper.wrapper("template", local_assigns, "content") | |
end | |
it "concatenates the rendered content" do | |
helper.stub!(:render_partial).and_return("wrapped content") | |
helper.should_receive(:concat).with("wrapped content") | |
helper.wrapper("template", nil, "content") | |
end | |
it "returns nil" do | |
helper.wrapper("template", nil, "content").should be_nil | |
end | |
end | |
context "with a block" do | |
it "captures the block" do | |
helper.should_receive(:capture) | |
helper.wrapper("template") { "content" } | |
end | |
it "assigns @content_for_layout the captured block" do | |
helper.should_receive(:capture).and_return("content") | |
helper.should_receive(:instance_variable_set).with(:@content_for_layout, "content") | |
helper.wrapper("template") { "content" } | |
end | |
it "renders the requested partial template" do | |
helper.should_receive(:render_partial).with(:partial => "template", :locals => {}) | |
helper.wrapper("template") { "content" } | |
end | |
it "concatenates the rendered content" do | |
helper.stub!(:render_partial).and_return("wrapped content") | |
helper.should_receive(:concat).with("wrapped content") | |
helper.wrapper("template") { "content" } | |
end | |
it "returns nil" do | |
helper.wrapper("template") { "content" }.should be_nil | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment