Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Created July 30, 2009 15:16
Show Gist options
  • Save jwreagor/158736 to your computer and use it in GitHub Desktop.
Save jwreagor/158736 to your computer and use it in GitHub Desktop.
#
# haml helpers for rendering jQuery tab's -
# GPL3 license (any copy), copyright (c) 2009 cheapRoc aka Justin Reagor
#
module TabHelper
#
# Builds a list of tab menu link id's to iterate over as well as build tab content
# area div's, without the need to pass anything but the name of the link and a second
# call for the content block.
#
# Optional block parameter passed in, which holds the id name for the div. You can
# use this to render partial names.
#
# Optional hash of attributes for the first call's link, and the second's div wrapper tag.
# whatever is left in opts will be passed along to the block during the second call.
#
# Examples:
#
# tab :class => "pickles", "store prices"
#
# => <a class="pickles" href="#store_prices">store prices</a>
#
# tab :div => {:class => "apple"}, :span => {:class => "explosive"} do |name, opts|
# haml_tag :span, name, opts[:span]
# end
#
# => <div class="apple" id="store_prices">
# <span class="explosive">store prices</span>
# </div>
#
def tab(*labels, &block)
opts = Hash === labels.first && labels.delete_at(0) || {}
if block_given?
haml_tag :div, { :id => name = @tabs.pop }.merge(opts.delete(:div)||{}) do
haml_concat block.call(name, opts)
end
else
(@tabs||=[]).unshift(name = labels.first.gsub(" ", "_"))
link_to labels.join(" "), "##{name}", opts.delete(:link)
end
end
#
# Call block for each tab, whilst passing along any tag options
#
def each_tab(opts={}, &block)
@tabs.dup.each { block.call(opts) }
end
#
# Iterates the tabs setup by #tab, and render's them as partials from a subdirectory
#
def render_tabs_from(dir, opts={})
each_tab(opts) do |opts|
tab(opts) do |name, opts|
render({ :partial => "#{dir}#{name}" }.merge(opts[:partial]))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment