Skip to content

Instantly share code, notes, and snippets.

@calas
Created July 20, 2009 12:45
Show Gist options
  • Save calas/150302 to your computer and use it in GitHub Desktop.
Save calas/150302 to your computer and use it in GitHub Desktop.
# MOST CODE AND IDEAS TAKEN FROM:
# http://www.igvita.com/2007/03/15/block-helpers-and-dry-views-in-rails/
module SidebarHelper
# Only need this helper once, it will provide an interface to convert a block into a partial.
# 1. Capture is a Rails helper which will 'capture' the output of a block into a variable
# 2. Merge the 'body' variable into our options hash
# 3. Render the partial with the given options hash. Just like calling the partial directly.
def block_to_partial(partial_name, options = {}, &block)
options.merge!(:body => capture(&block))
concat(render(partial_name, options), &block)
end
# General sidebox.
#
# Renders a div with sidebox css class in the sidebar with an optional h3
# title
def sidebox(title_or_options = {}, options = {}, &block)
if title_or_options.is_a?(Hash)
options.merge!(title_or_options)
else
options.merge!(:box_title => title_or_options)
end
content_for(:sidebar) do
block_to_partial('shared/sidebox', options, &block)
end
end
# Renders a +sidebox+ with a list of (link) items. Default title is the
# translation of the actions key
def actions_box(box_title=:actions, options = {}, &block)
sidebox(options.merge(:box_title => box_title)) do
content_tag(:ul, :class => :actions, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment