Skip to content

Commit 3fdaa3d

Browse files
author
CodeOfficer
committed
Gemifying, adding a new create_ajax method on the TabsRenderer, and making it work for ruby 1.8 & 1.9
1 parent b31798b commit 3fdaa3d

File tree

14 files changed

+384
-82
lines changed

14 files changed

+384
-82
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bundle
2+
.rvmrc
3+
*.gem

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in ui_helpers.gemspec
4+
gemspec

Gemfile.lock

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
PATH
2+
remote: .
3+
specs:
4+
jquery_ui_rails_helpers (0.0.1)
5+
rails (~> 3.0.0)
6+
shoulda (~> 3.0.0.beta2)
7+
8+
GEM
9+
remote: http://rubygems.org/
10+
specs:
11+
abstract (1.0.0)
12+
actionmailer (3.0.3)
13+
actionpack (= 3.0.3)
14+
mail (~> 2.2.9)
15+
actionpack (3.0.3)
16+
activemodel (= 3.0.3)
17+
activesupport (= 3.0.3)
18+
builder (~> 2.1.2)
19+
erubis (~> 2.6.6)
20+
i18n (~> 0.4)
21+
rack (~> 1.2.1)
22+
rack-mount (~> 0.6.13)
23+
rack-test (~> 0.5.6)
24+
tzinfo (~> 0.3.23)
25+
activemodel (3.0.3)
26+
activesupport (= 3.0.3)
27+
builder (~> 2.1.2)
28+
i18n (~> 0.4)
29+
activerecord (3.0.3)
30+
activemodel (= 3.0.3)
31+
activesupport (= 3.0.3)
32+
arel (~> 2.0.2)
33+
tzinfo (~> 0.3.23)
34+
activeresource (3.0.3)
35+
activemodel (= 3.0.3)
36+
activesupport (= 3.0.3)
37+
activesupport (3.0.3)
38+
arel (2.0.7)
39+
builder (2.1.2)
40+
erubis (2.6.6)
41+
abstract (>= 1.0.0)
42+
i18n (0.5.0)
43+
mail (2.2.15)
44+
activesupport (>= 2.3.6)
45+
i18n (>= 0.4.0)
46+
mime-types (~> 1.16)
47+
treetop (~> 1.4.8)
48+
mime-types (1.16)
49+
polyglot (0.3.1)
50+
rack (1.2.1)
51+
rack-mount (0.6.13)
52+
rack (>= 1.0.0)
53+
rack-test (0.5.7)
54+
rack (>= 1.0)
55+
rails (3.0.3)
56+
actionmailer (= 3.0.3)
57+
actionpack (= 3.0.3)
58+
activerecord (= 3.0.3)
59+
activeresource (= 3.0.3)
60+
activesupport (= 3.0.3)
61+
bundler (~> 1.0)
62+
railties (= 3.0.3)
63+
railties (3.0.3)
64+
actionpack (= 3.0.3)
65+
activesupport (= 3.0.3)
66+
rake (>= 0.8.7)
67+
thor (~> 0.14.4)
68+
rake (0.8.7)
69+
shoulda (3.0.0.beta2)
70+
shoulda-context (~> 1.0.0.beta1)
71+
shoulda-matchers (~> 1.0.0.beta1)
72+
shoulda-context (1.0.0.beta1)
73+
shoulda-matchers (1.0.0.beta1)
74+
thor (0.14.6)
75+
treetop (1.4.9)
76+
polyglot (>= 0.3.1)
77+
tzinfo (0.3.24)
78+
79+
PLATFORMS
80+
ruby
81+
82+
DEPENDENCIES
83+
jquery_ui_rails_helpers!

Rakefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

helpers/tabs_helper.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

jquery_ui_rails_helpers.gemspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "jquery_ui_rails_helpers/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "jquery_ui_rails_helpers"
7+
s.version = JqueryUiRailsHelpers::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.summary = "jQuery UI Rails Helpers"
10+
s.authors = ["CodeOfficer"]
11+
s.email = ["codeofficer@gmail.com"]
12+
s.homepage = "http://www.codeofficer.com/"
13+
s.description = "jQuery UI Rails Helpers"
14+
15+
s.add_dependency("rails", "~> 3.0.0")
16+
s.add_dependency("shoulda", "~> 3.0.0.beta2")
17+
18+
s.files = `git ls-files`.split("\n")
19+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21+
s.require_paths = ["lib"]
22+
23+
s.rubyforge_project = "jquery_ui_rails_helpers"
24+
end
25+
26+
27+
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
module AccordionsHelper
2-
32
def accordions_for( *options, &block )
43
raise ArgumentError, "Missing block" unless block_given?
5-
6-
accordions = AccordionsHelper::AccordionsRenderer.new( *options, &block )
7-
accordions_html = accordions.render
8-
concat accordions_html
4+
raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render
95
end
10-
6+
117
class AccordionsRenderer
12-
8+
139
def initialize( options={}, &block )
1410
raise ArgumentError, "Missing block" unless block_given?
1511

@@ -19,38 +15,33 @@ def initialize( options={}, &block )
1915

2016
yield self
2117
end
22-
18+
2319
def create( accordion_id, accordion_text, options={}, &block )
2420
raise "Block needed for AccordionsRenderer#CREATE" unless block_given?
2521
@accordions << [ accordion_id, accordion_text, options, block ]
2622
end
27-
28-
def render
29-
content_tag :div, { :id => :accordions }.merge( @options ) do
30-
@accordions.collect do |accordion|
31-
accordion_head(accordion) + accordion_body(accordion)
32-
end.join
33-
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 ) )
3429
end
35-
36-
private # ---------------------------------------------------------------------------
37-
30+
31+
private # ---------------------------------------------------------------------------
32+
3833
def accordion_head(accordion)
39-
content_tag :h3, :id => accordion[0] do
40-
link_to accordion[1], '#'
41-
end
34+
content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0]
4235
end
43-
36+
4437
def accordion_body(accordion)
45-
content_tag :div do
46-
capture( &accordion[3] )
47-
end
38+
content_tag :div, capture( &accordion[3] )
4839
end
49-
40+
5041
def method_missing( *args, &block )
5142
@template.send( *args, &block )
5243
end
53-
54-
end
5544

45+
end
5646
end
47+

lib/helpers/javascripts_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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, capture( &tab[3] ), tab[2].merge( :id => tab[0] ) )
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

lib/jquery_ui_rails_helpers.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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)

0 commit comments

Comments
 (0)