Created
February 27, 2012 21:39
-
-
Save joshmvandercom/1927280 to your computer and use it in GitHub Desktop.
Passing a block into a helper
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
# Creates a Bootstrap dropdown | |
# Ex: | |
# = dropdown_to "Auctions", auctions_path do | |
# %li= link_to 'Upcoming Auctions', auctions_path | |
# %li= link_to 'Past Auctions', past_auctions_path | |
# | |
# Returns: | |
# <li class="dropdown"> | |
# <a class="dropdown-toggle" data-toggle="dropdown" href="/auctions"> | |
# <span>Auction</span> | |
# <b class="caret"></b> | |
# </a> | |
# <ul class="dropdown-menu"> | |
# <li><a href="/auctions">Upcoming Auctions</a></li> | |
# <li><a href="/auctions/past">Past Auctions</a></li> | |
# </ul> | |
# </li> | |
def dropdown_to(title, path, options = {}, &block) | |
link_options = options.has_key?(:link) && options[:link].is_a?(Hash) ? options[:link] : {} | |
dropdown_options = options.has_key?(:dropdown) && options[:dropdown].is_a?(Hash) ? options[:dropdown] : {} | |
li_options = options.has_key?(:li) && options[:li].is_a?(Hash) ? options[:li] : {} | |
link = content_tag(:a, { :href => path, :class => 'dropdown-toggle', :data => { :toggle => 'dropdown' } }.merge(link_options) ) do | |
content_tag(:span, title).concat( content_tag(:b, '', :class => 'caret') ) | |
end | |
dropdown_list = content_tag(:ul, { :class => 'dropdown-menu' }.merge(dropdown_options) ) do | |
capture &block if block_given? | |
end | |
content_tag(:li, { :class => 'dropdown' }.merge(li_options) ) do | |
link.concat(dropdown_list) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment