├── .gitignore ├── CHANGELOG ├── Gemfile ├── Gemfile.lock ├── README.textile ├── Rakefile ├── jquery_ui_rails_helpers.gemspec ├── lib ├── helpers │ ├── accordions_helper.rb │ ├── javascripts_helper.rb │ └── tabs_helper.rb ├── jquery_ui_rails_helpers.rb └── jquery_ui_rails_helpers │ └── version.rb └── test ├── test_helper.rb └── unit └── helper ├── accordions_helper_test.rb └── tabs_helper_test.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle 2 | .rvmrc 3 | *.gem -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | == 01-24-2011 2 | 3 | * rewrote for rails 3 && (ruby 1.8 || ruby 1.9), added tests 4 | 5 | == 03-07-2009 6 | 7 | * big change: removed the 2nd param for concat, rails no longer requires the binding be passed. 8 | * added a helper for creating accordions 9 | 10 | == 09-04-2008 11 | 12 | * big change: renamed classes, and helper 'tabs_for' now requires a block 13 | 14 | == 08-28-2008 15 | 16 | * added the ability to call TabsRenderer.new with a block parameter 17 | 18 | == 06-22-2008 initial import 19 | 20 | * TabsRenderer added -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | jquery_ui_rails_helpers (0.0.2) 5 | 6 | GEM 7 | remote: http://rubygems.org/ 8 | specs: 9 | abstract (1.0.0) 10 | actionmailer (3.0.7) 11 | actionpack (= 3.0.7) 12 | mail (~> 2.2.15) 13 | actionpack (3.0.7) 14 | activemodel (= 3.0.7) 15 | activesupport (= 3.0.7) 16 | builder (~> 2.1.2) 17 | erubis (~> 2.6.6) 18 | i18n (~> 0.5.0) 19 | rack (~> 1.2.1) 20 | rack-mount (~> 0.6.14) 21 | rack-test (~> 0.5.7) 22 | tzinfo (~> 0.3.23) 23 | activemodel (3.0.7) 24 | activesupport (= 3.0.7) 25 | builder (~> 2.1.2) 26 | i18n (~> 0.5.0) 27 | activerecord (3.0.7) 28 | activemodel (= 3.0.7) 29 | activesupport (= 3.0.7) 30 | arel (~> 2.0.2) 31 | tzinfo (~> 0.3.23) 32 | activeresource (3.0.7) 33 | activemodel (= 3.0.7) 34 | activesupport (= 3.0.7) 35 | activesupport (3.0.7) 36 | arel (2.0.10) 37 | builder (2.1.2) 38 | erubis (2.6.6) 39 | abstract (>= 1.0.0) 40 | i18n (0.5.0) 41 | mail (2.2.19) 42 | activesupport (>= 2.3.6) 43 | i18n (>= 0.4.0) 44 | mime-types (~> 1.16) 45 | treetop (~> 1.4.8) 46 | mime-types (1.16) 47 | polyglot (0.3.1) 48 | rack (1.2.2) 49 | rack-mount (0.6.14) 50 | rack (>= 1.0.0) 51 | rack-test (0.5.7) 52 | rack (>= 1.0) 53 | rails (3.0.7) 54 | actionmailer (= 3.0.7) 55 | actionpack (= 3.0.7) 56 | activerecord (= 3.0.7) 57 | activeresource (= 3.0.7) 58 | activesupport (= 3.0.7) 59 | bundler (~> 1.0) 60 | railties (= 3.0.7) 61 | railties (3.0.7) 62 | actionpack (= 3.0.7) 63 | activesupport (= 3.0.7) 64 | rake (>= 0.8.7) 65 | thor (~> 0.14.4) 66 | rake (0.9.0) 67 | shoulda (3.0.0.beta2) 68 | shoulda-context (~> 1.0.0.beta1) 69 | shoulda-matchers (~> 1.0.0.beta1) 70 | shoulda-context (1.0.0.beta1) 71 | shoulda-matchers (1.0.0.beta2) 72 | thor (0.14.6) 73 | treetop (1.4.9) 74 | polyglot (>= 0.3.1) 75 | tzinfo (0.3.27) 76 | 77 | PLATFORMS 78 | ruby 79 | 80 | DEPENDENCIES 81 | jquery_ui_rails_helpers! 82 | rails (>= 3.0.0) 83 | shoulda (~> 3.0.0.beta2) 84 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. What Is It? 2 | 3 | These are some view helpers I use in Rails to better integrate jQuery UI into my sites. 4 | 5 | I hope you find them useful. 6 | 7 | h2. TabsHelper 8 | 9 | This helper simplifies the code required to use the jQuery UI Tab plugin. 10 | 11 |
12 | <% tabs_for do |tab| %>
13 | <% tab.create('tab_one', 'Tab 1') do %>
14 | # ... insert tab contents
15 | <% end %>
16 | <% tab.create('tab_two', 'Tab 2') do %>
17 | # ... insert tab contents
18 | <% end %>
19 | <% end %>
20 |
21 |
22 | The above will generate this HTML in your view:
23 |
24 |
25 |
37 |
38 |
39 | Tabs will be rendered in the order you create them.
40 |
41 | You can easily render a tab conditionally by appending your condition to the end of
42 | the 'create' block as such ...
43 |
44 |
45 | <% tab.create('profile_tab', 'Your Profile') do %>
46 | # ... insert tab contents
47 | <% end unless @current_user.nil? %>
48 |
49 |
50 | You can pass HTML options to either the parent DIV or any individual tab's
51 | DIV as you like ...
52 |
53 |
54 | <% tabs_for(:class => 'zippy') do |tab| %>
55 | <% tab.create('tab_one', 'Tab 1', :style => 'background: #FFF') do %>
56 | # ... insert tab contents
57 | <% end %>
58 | <% end %>
59 |
60 |
61 | The default DOM ID for the parent div is ... id="tabs" ... unless you pass in an HTML
62 | option with a different value.
63 |
64 | h2. AccordionsHelper
65 |
66 | This helper simplifies the code required to use JQuery UIs Accordion plugin.
67 |
68 | Usage is identical to the Tabs helper.
69 |
70 |
71 | <% accordions_for do |accordion| %>
72 | <% accordion.create("dom_id", "accordion_title") do %>
73 | # ... insert accordion contents
74 | <% end %>
75 | <% end %>
76 |
77 |
78 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'bundler'
2 | require 'rake'
3 | require 'rake/testtask'
4 | Bundler::GemHelper.install_tasks
5 |
6 | desc 'Default: run unit tests.'
7 | task :default => :test
8 |
9 | desc 'Test the simple_form plugin.'
10 | Rake::TestTask.new(:test) do |t|
11 | t.libs << 'lib'
12 | t.libs << 'test'
13 | t.pattern = 'test/**/*_test.rb'
14 | t.verbose = true
15 | end
--------------------------------------------------------------------------------
/jquery_ui_rails_helpers.gemspec:
--------------------------------------------------------------------------------
1 | # -*- encoding: utf-8 -*-
2 |
3 | $:.push File.expand_path("../lib", __FILE__)
4 | require "jquery_ui_rails_helpers/version"
5 |
6 | Gem::Specification.new do |s|
7 | s.name = "jquery_ui_rails_helpers"
8 | s.version = JqueryUiRailsHelpers::VERSION
9 | s.platform = Gem::Platform::RUBY
10 | s.summary = "jQuery UI Rails Helpers"
11 | s.authors = ["CodeOfficer"]
12 | s.email = ["codeofficer@gmail.com"]
13 | s.homepage = "http://www.codeofficer.com/"
14 | s.description = "jQuery UI Rails Helpers"
15 |
16 | # s.add_development_dependency("rails")
17 | # s.add_development_dependency("shoulda")
18 |
19 | s.files = `git ls-files`.split("\n")
20 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22 | s.require_paths = ["lib"]
23 |
24 | s.rubyforge_project = "jquery_ui_rails_helpers"
25 | end
26 |
--------------------------------------------------------------------------------
/lib/helpers/accordions_helper.rb:
--------------------------------------------------------------------------------
1 | module AccordionsHelper
2 | def accordions_for( *options, &block )
3 | raise ArgumentError, "Missing block" unless block_given?
4 | raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render
5 | end
6 |
7 | class AccordionsRenderer
8 |
9 | def initialize( options={}, &block )
10 | raise ArgumentError, "Missing block" unless block_given?
11 |
12 | @template = eval( 'self', block.binding )
13 | @options = options
14 | @accordions = []
15 |
16 | yield self
17 | end
18 |
19 | def create( accordion_id, accordion_text, options={}, &block )
20 | raise "Block needed for AccordionsRenderer#CREATE" unless block_given?
21 | @accordions << [ accordion_id, accordion_text, options, block ]
22 | end
23 |
24 | def render
25 | content = @accordions.collect do |accordion|
26 | accordion_head(accordion) << accordion_body(accordion)
27 | end.join
28 | content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) )
29 | end
30 |
31 | private # ---------------------------------------------------------------------------
32 |
33 | def accordion_head(accordion)
34 | content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0]
35 | end
36 |
37 | def accordion_body(accordion)
38 | content_tag( :div, &accordion[3] )
39 | end
40 |
41 | def method_missing( *args, &block )
42 | @template.send( *args, &block )
43 | end
44 |
45 | end
46 | end
47 |
48 |
--------------------------------------------------------------------------------
/lib/helpers/javascripts_helper.rb:
--------------------------------------------------------------------------------
1 | module JavascriptsHelper
2 |
3 | def stylesheet(*args)
4 | content_for(:head) { stylesheet_link_tag(*args) }
5 | end
6 |
7 | def javascript(*args)
8 | content_for(:head) { javascript_include_tag(*args) }
9 | end
10 |
11 | def field_id_for_js(f, attribute)
12 | "#{f.object_name}[#{attribute.to_s.sub(/\?$/,"")}]".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
13 | end
14 |
15 | def field_name_for_js(f, attribute)
16 | "#{f.object_name}[#{attribute.to_s.sub(/\?$/,"")}]"
17 | end
18 |
19 | end
--------------------------------------------------------------------------------
/lib/helpers/tabs_helper.rb:
--------------------------------------------------------------------------------
1 | # http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates
2 | # module JqueryUiRailsHelpers
3 |
4 | module TabsHelper
5 | def tabs_for( *options, &block )
6 | raise ArgumentError, "Missing block" unless block_given?
7 | raw TabsHelper::TabsRenderer.new( *options, &block ).render
8 | end
9 |
10 | class TabsRenderer
11 |
12 | def initialize( options={}, &block )
13 | raise ArgumentError, "Missing block" unless block_given?
14 |
15 | @template = eval( 'self', block.binding )
16 | @options = options
17 | @tabs = []
18 |
19 | yield self
20 | end
21 |
22 | def create( tab_id, tab_text, options={}, &block )
23 | raise "Block needed for TabsRenderer#CREATE" unless block_given?
24 | @tabs << [ tab_id, tab_text, options, block, {:ajax => false} ]
25 | end
26 |
27 | def create_ajax( link, tab_text, options={})
28 | @tabs << [ link, tab_text, options, nil, {:ajax => true} ]
29 | end
30 |
31 | def render
32 | content_tag( :div, raw([render_tabs, render_bodies].join), { :id => :tabs }.merge( @options ) )
33 | end
34 |
35 | private # ---------------------------------------------------------------------------
36 |
37 | def render_tabs
38 | content_tag :ul do
39 | result = @tabs.collect do |tab|
40 | if tab[4][:ajax]
41 | content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "#{tab[0]}" ) )
42 | else
43 | content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) )
44 | end
45 | end.join
46 | raw(result)
47 | end
48 | end
49 |
50 | def render_bodies
51 | @tabs.collect do |tab|
52 | if tab[4][:ajax]
53 | # there are no divs for ajaxed tabs
54 | else
55 | content_tag( :div, tab[2].merge( :id => tab[0] ), & tab[3])
56 | end
57 | end.join.to_s
58 | end
59 |
60 | def method_missing( *args, &block )
61 | @template.send( *args, &block )
62 | end
63 |
64 | end
65 | end
66 |
--------------------------------------------------------------------------------
/lib/jquery_ui_rails_helpers.rb:
--------------------------------------------------------------------------------
1 | require 'action_view'
2 | require "jquery_ui_rails_helpers/version"
3 | require 'helpers/javascripts_helper'
4 | require 'helpers/tabs_helper'
5 | require 'helpers/accordions_helper'
6 |
7 | module JqueryUiRailsHelpers
8 | end
9 |
10 | ActionView::Base.send(:include, JavascriptsHelper)
11 | ActionView::Base.send(:include, TabsHelper)
12 | ActionView::Base.send(:include, AccordionsHelper)
--------------------------------------------------------------------------------
/lib/jquery_ui_rails_helpers/version.rb:
--------------------------------------------------------------------------------
1 | module JqueryUiRailsHelpers
2 | VERSION = "0.0.2"
3 | end
4 |
--------------------------------------------------------------------------------
/test/test_helper.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler'
3 |
4 | Bundler.setup
5 |
6 | require 'test/unit'
7 | require 'shoulda'
8 | require 'action_controller'
9 | require 'action_controller/test_case'
10 | require 'action_view'
11 | require 'action_view/template'
12 | require 'action_view/test_case'
13 |
14 | $:.unshift File.expand_path("../../lib", __FILE__)
15 | require 'jquery_ui_rails_helpers'
16 |
17 | class ActionView::TestCase
18 |
19 | # Take care of the RuntimeError:
20 | # In order to use #url_for, you must include routing helpers explicitly.
21 | # For instance, `include Rails.application.routes.url_helpers
22 | setup :shhhhhh_url_helpers
23 |
24 | def shhhhhh_url_helpers
25 | def @controller._routes
26 | Module.new do
27 | def self.url_helpers
28 | Module.new
29 | end
30 | end
31 | end
32 | end
33 |
34 | end
--------------------------------------------------------------------------------
/test/unit/helper/accordions_helper_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 |
3 | class AccordionsHelperTest < ActionView::TestCase
4 |
5 | context "creating accordions without a block syntax" do
6 | should 'raises an exception' do
7 | assert_raise ArgumentError do
8 | @accordions = accordions_for
9 | end
10 | end
11 | end
12 |
13 | context "creating one set of accordions" do
14 | setup do
15 | @accordions = accordions_for do |accordion|
16 | accordion.create('accordion_one', 'One') { "Accordion One." }
17 | accordion.create('accordion_two', 'Two') { "Accordion Two." }
18 | end
19 | end
20 |
21 | should 'have proper dom structure' do
22 | render :text => @accordions
23 | assert_select "div[id='accordions']", 1
24 | assert_select "div[id='accordions'] h3[id='accordion_one']", 1
25 | assert_select "div[id='accordions'] h3[id='accordion_one'] a", {:count => 1, :text => "One"}
26 | assert_select "div[id='accordions'] h3[id='accordion_two']", 1
27 | assert_select "div[id='accordions'] h3[id='accordion_two'] a", {:count => 1, :text => "Two"}
28 | assert_select "div[id='accordions'] div", 2
29 | end
30 | end
31 |
32 | end
33 |
34 | #
40 |
--------------------------------------------------------------------------------
/test/unit/helper/tabs_helper_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 |
3 | class TabsHelperTest < ActionView::TestCase
4 |
5 | context "creating tabs without a block syntax" do
6 | should 'raises an exception' do
7 | assert_raise ArgumentError do
8 | @tabs = tabs_for
9 | end
10 | end
11 | end
12 |
13 | context "creating two tabs" do
14 | setup do
15 | @tabs = tabs_for do |tab|
16 | tab.create('tab_one', 'One') { "Tab One." }
17 | tab.create('tab_two', 'Two') { "Tab Two." }
18 | end
19 | end
20 |
21 | should 'have proper dom structure' do
22 | render :text => @tabs
23 | assert_select "div[id='tabs']", 1
24 | assert_select "div[id='tabs'] ul", 1
25 | assert_select "div[id='tabs'] ul li", 2
26 | assert_select "div[id='tabs'] div[id='tab_one']", 1
27 | assert_select "div[id='tabs'] div[id='tab_two']", 1
28 | end
29 | end
30 |
31 | context "creating custom tabs" do
32 | setup do
33 | @tabs = tabs_for(:id => "my_tabs") do |tab|
34 | tab.create_ajax('http://www.codeofficer.com/', 'Ajax')
35 | end
36 | end
37 |
38 | should 'allow overriding the outter divs id' do
39 | render :text => @tabs
40 | assert_select "div[id='my_tabs']", 1
41 | end
42 |
43 | should 'allow ajaxed tabs' do
44 | render :text => @tabs
45 | assert_select "div[id='my_tabs'] ul li a[href='http://www.codeofficer.com/']", 1
46 | assert_select "div[id='my_tabs'] div", 0
47 | end
48 | end
49 |
50 | end
51 |
52 | #
--------------------------------------------------------------------------------