Created
November 3, 2009 22:31
-
-
Save amiel/225514 to your computer and use it in GitHub Desktop.
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
module HelperClasses | |
# TabCreator | |
# | |
# generate html for tabs using partials and run the correct javascripts | |
# | |
# Example: | |
# <% HelperClasses::TabCreator.new 'account_tabs', self do |tabs| %> # => you must pass self so that TabCreator will have access to the template helpers | |
# <%= tabs.item 'General Settings', :partial => 'general_info_form' %> | |
# <% tabs.item 'Photo' do %> # => renders block, id will be #photo_tab | |
# foobar | |
# <% end %> | |
# <%= tabs.item 'Subscription', :partial => 'subscriptions/overview' %> # => renders 'subscription/overview' but the id will be #subscription_overview_tab | |
# <% end %> | |
# | |
# Would generate something like | |
# <div id="account_tabs" class="tab-container"> | |
# <ul class="tabs"> | |
# <li><a href="#general_info_form_tab"><span>General Settings</span></a></li> | |
# <li><a href="#photo_tab"><span>Photo</span></a></li> | |
# <li><a href="#subscriptions_overview_tab"><span>Subscription</span></a></li> | |
# </ul> | |
# | |
# <div id="general_info_form_tab"> | |
# <%= render :partial => 'general_info_form' %> | |
# </div> | |
# | |
# <div id="photo_tab"> | |
# foobar | |
# </div> | |
# | |
# <div id="subscriptions_overview_tab"> | |
# <%= render :partial => 'subscriptions/overview' %> | |
# </div> | |
class TabCreator | |
def initialize(domid, template, options = {}, &block) | |
@template = template | |
@tabs = Array.new | |
initialize_tabs "##{domid}", options unless options[:auto_js] == false | |
tab_content = capture { yield self } | |
tabs = @tabs.collect do |tab| | |
content_tag :li, tab[:content], tab[:options] | |
end.join "\n" | |
content = content_tag(:ul, tabs, :class => 'tabs') + tab_content | |
concat content_tag(:div, content, :id => domid, :class => 'tab-container') | |
end | |
def item(title, render_options = {}, content_options = {}, &block) | |
render_options[:partial] ||= sanitize_to_id(title.to_s.underscore) | |
domid = "#{sanitize_to_id(render_options[:partial])}_tab" | |
@tabs << { :content => link_to(content_tag(:span, t(".#{title}")), "##{domid}"), :options => content_options } | |
content = if block_given? | |
# render_options are not used, so we might as well allow the user to specify them as the second argument | |
content_options.merge! render_options | |
content_options.delete(:partial) | |
capture(&block) | |
else | |
render(render_options) | |
end | |
panel_options = { :id => domid }.merge(content_options) | |
panel_options.merge_dom_class!('ui-tabs-hide') if content_options.delete(:hide) | |
content = content_tag(:div, content, panel_options) | |
return block_given? ? concat(content) : content | |
end | |
def tab_only(options = {}, &block) | |
@tabs << { :content => capture(&block), :options => options } | |
return nil | |
end | |
def method_missing(*args, &block) | |
@template.send(*args, &block) | |
end | |
private | |
def initialize_tabs(selector, options = {}) | |
content_for(:dom_ready) { "TabsHelper.tabs(#{selector.to_json}, #{options_for_javascript options});" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment