Skip to content

Commit

Permalink
Initial Import!
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Jones committed Jun 22, 2008
0 parents commit a41e3a1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Empty file added .gitignore
Empty file.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== 06-22-2008 initial import

* TabsRenderer added
66 changes: 66 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
JQuery UI Rails Helpers Readme
----------------------------------------------------------------

* Web: http://www.codeofficer.com/
* Email: spam*@*codeofficer*dot*com
* JQuery: http://www.jquery.com/
* JQuery UI: http://ui.jquery.com/

These are some view helpers I use in Rails to better integrate JQuery UI in my sites.

Maybe you will find them useful as well.

These include ...


TabsRenderer
----------------------------------------------------------------

This helper simplifies the code required to use JQuery UIs Tabs plugin.

There are 3 steps to rendering a tab ... initialize, create and render.

<% tabs = TabsRenderer.new(self) %>
<% tabs.create('tab_one', 'Tab 1') do %>
# ... insert tab contents
<% end %>
<% tabs.create('tab_two', 'Tab 2') do %>
# ... insert tab contents
<% end %>
<%= tabs.render %>

The above will generate this HTML in your view:

<div id="tabs">
<ul>
<li><a href="#tab_one"><span>Tab 1</span></a></li>
<li><a href="#tab_two"><span>Tab 2</span></a></li>
</ul>
<div id="tab_one">
# ... insert tab contents
</div>
<div id="tab_two">
# ... insert tab contents
</div>
</div>

Tabs will be rendered in the order you create them.

You can easily render a tab conditionally by appending your condition to the end of
the 'create' block as such ...

<% tabs.create('profile_tab', 'Your Profile') do %>
# ... insert tab contents
<% end unless @current_user.nil? %>

You can pass HTML options to either the parent DIV or any individual tab's
DIV as you like ...

<% tabs = TabsRenderer.new(self, :class = 'flora') %>
<% tabs.create('tab_one', 'Tab 1', :style => 'background: #FFF') do %>
# ... insert tab contents
<% end %>
<%= tabs.render %>

The default DOM ID for the parent div is ... id="tabs" ... unless you pass in an HTML
option with a different value.
38 changes: 38 additions & 0 deletions helpers/tabs_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class TabsRenderer

def initialize(template, options={})
@template = template
@options = options
@tabs = []
end

def create(tab_id, tab_text, options={}, &block)
raise "Block needed for TabsRenderer#CREATE" unless block_given?
@tabs << [tab_id, tab_text, options, block]
end

def render
content_tag(:div, (render_tabs + render_bodies), {:id => :tabs}.merge(@options))
end

private # ---------------------------------------------------------------------------

def render_tabs
content_tag :ul do
@tabs.collect do |tab|
content_tag(:li, link_to(content_tag(:span, tab[1]), "##{tab[0]}") )
end
end
end

def render_bodies
@tabs.collect do |tab|
content_tag(:div, capture(&tab[3]), tab[2].merge(:id => tab[0]))
end.to_s
end

def method_missing(*args, &block)
@template.send(*args, &block)
end

end

0 comments on commit a41e3a1

Please sign in to comment.