diff --git a/.gitignore b/.gitignore index e69de29..35e5213 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +.bundle +.rvmrc +*.gem \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4f5030d --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in ui_helpers.gemspec +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..7de5f45 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,83 @@ +PATH + remote: . + specs: + jquery_ui_rails_helpers (0.0.1) + rails (~> 3.0.0) + shoulda (~> 3.0.0.beta2) + +GEM + remote: http://rubygems.org/ + specs: + abstract (1.0.0) + actionmailer (3.0.3) + actionpack (= 3.0.3) + mail (~> 2.2.9) + actionpack (3.0.3) + activemodel (= 3.0.3) + activesupport (= 3.0.3) + builder (~> 2.1.2) + erubis (~> 2.6.6) + i18n (~> 0.4) + rack (~> 1.2.1) + rack-mount (~> 0.6.13) + rack-test (~> 0.5.6) + tzinfo (~> 0.3.23) + activemodel (3.0.3) + activesupport (= 3.0.3) + builder (~> 2.1.2) + i18n (~> 0.4) + activerecord (3.0.3) + activemodel (= 3.0.3) + activesupport (= 3.0.3) + arel (~> 2.0.2) + tzinfo (~> 0.3.23) + activeresource (3.0.3) + activemodel (= 3.0.3) + activesupport (= 3.0.3) + activesupport (3.0.3) + arel (2.0.7) + builder (2.1.2) + erubis (2.6.6) + abstract (>= 1.0.0) + i18n (0.5.0) + mail (2.2.15) + activesupport (>= 2.3.6) + i18n (>= 0.4.0) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.16) + polyglot (0.3.1) + rack (1.2.1) + rack-mount (0.6.13) + rack (>= 1.0.0) + rack-test (0.5.7) + rack (>= 1.0) + rails (3.0.3) + actionmailer (= 3.0.3) + actionpack (= 3.0.3) + activerecord (= 3.0.3) + activeresource (= 3.0.3) + activesupport (= 3.0.3) + bundler (~> 1.0) + railties (= 3.0.3) + railties (3.0.3) + actionpack (= 3.0.3) + activesupport (= 3.0.3) + rake (>= 0.8.7) + thor (~> 0.14.4) + rake (0.8.7) + shoulda (3.0.0.beta2) + shoulda-context (~> 1.0.0.beta1) + shoulda-matchers (~> 1.0.0.beta1) + shoulda-context (1.0.0.beta1) + shoulda-matchers (1.0.0.beta1) + thor (0.14.6) + treetop (1.4.9) + polyglot (>= 0.3.1) + tzinfo (0.3.24) + +PLATFORMS + ruby + +DEPENDENCIES + jquery_ui_rails_helpers! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..18f188d --- /dev/null +++ b/Rakefile @@ -0,0 +1,15 @@ +require 'bundler' +require 'rake' +require 'rake/testtask' +Bundler::GemHelper.install_tasks + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the simple_form plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end \ No newline at end of file diff --git a/helpers/tabs_helper.rb b/helpers/tabs_helper.rb deleted file mode 100644 index 8b4d624..0000000 --- a/helpers/tabs_helper.rb +++ /dev/null @@ -1,54 +0,0 @@ -module TabsHelper - - def tabs_for( *options, &block ) - raise ArgumentError, "Missing block" unless block_given? - - tabs = TabsHelper::TabsRenderer.new( *options, &block ) - tabs_html = tabs.render - concat tabs_html - end - - class TabsRenderer - - def initialize( options={}, &block ) - raise ArgumentError, "Missing block" unless block_given? - - @template = eval( 'self', block.binding ) - @options = options - @tabs = [] - - yield self - 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.join - end - end - - def render_bodies - @tabs.collect do |tab| - content_tag( :div, capture( &tab[3] ), tab[2].merge( :id => tab[0] ) ) - end.join.to_s - end - - def method_missing( *args, &block ) - @template.send( *args, &block ) - end - - end - -end diff --git a/jquery_ui_rails_helpers.gemspec b/jquery_ui_rails_helpers.gemspec new file mode 100644 index 0000000..58725b4 --- /dev/null +++ b/jquery_ui_rails_helpers.gemspec @@ -0,0 +1,27 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "jquery_ui_rails_helpers/version" + +Gem::Specification.new do |s| + s.name = "jquery_ui_rails_helpers" + s.version = JqueryUiRailsHelpers::VERSION + s.platform = Gem::Platform::RUBY + s.summary = "jQuery UI Rails Helpers" + s.authors = ["CodeOfficer"] + s.email = ["codeofficer@gmail.com"] + s.homepage = "http://www.codeofficer.com/" + s.description = "jQuery UI Rails Helpers" + + s.add_dependency("rails", "~> 3.0.0") + s.add_dependency("shoulda", "~> 3.0.0.beta2") + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.rubyforge_project = "jquery_ui_rails_helpers" +end + + + diff --git a/helpers/accordions_helper.rb b/lib/helpers/accordions_helper.rb similarity index 54% rename from helpers/accordions_helper.rb rename to lib/helpers/accordions_helper.rb index 09ed43f..d613174 100644 --- a/helpers/accordions_helper.rb +++ b/lib/helpers/accordions_helper.rb @@ -1,15 +1,11 @@ module AccordionsHelper - def accordions_for( *options, &block ) raise ArgumentError, "Missing block" unless block_given? - - accordions = AccordionsHelper::AccordionsRenderer.new( *options, &block ) - accordions_html = accordions.render - concat accordions_html + raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render end - + class AccordionsRenderer - + def initialize( options={}, &block ) raise ArgumentError, "Missing block" unless block_given? @@ -19,38 +15,33 @@ def initialize( options={}, &block ) yield self end - + def create( accordion_id, accordion_text, options={}, &block ) raise "Block needed for AccordionsRenderer#CREATE" unless block_given? @accordions << [ accordion_id, accordion_text, options, block ] end - - def render - content_tag :div, { :id => :accordions }.merge( @options ) do - @accordions.collect do |accordion| - accordion_head(accordion) + accordion_body(accordion) - end.join - end + + def render + content = @accordions.collect do |accordion| + accordion_head(accordion) << accordion_body(accordion) + end.join + content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) ) end - - private # --------------------------------------------------------------------------- - + + private # --------------------------------------------------------------------------- + def accordion_head(accordion) - content_tag :h3, :id => accordion[0] do - link_to accordion[1], '#' - end + content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0] end - + def accordion_body(accordion) - content_tag :div do - capture( &accordion[3] ) - end + content_tag :div, capture( &accordion[3] ) end - + def method_missing( *args, &block ) @template.send( *args, &block ) end - - end + end end + diff --git a/lib/helpers/javascripts_helper.rb b/lib/helpers/javascripts_helper.rb new file mode 100644 index 0000000..fe2302f --- /dev/null +++ b/lib/helpers/javascripts_helper.rb @@ -0,0 +1,19 @@ +module JavascriptsHelper + + def stylesheet(*args) + content_for(:head) { stylesheet_link_tag(*args) } + end + + def javascript(*args) + content_for(:head) { javascript_include_tag(*args) } + end + + def field_id_for_js(f, attribute) + "#{f.object_name}[#{attribute.to_s.sub(/\?$/,"")}]".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def field_name_for_js(f, attribute) + "#{f.object_name}[#{attribute.to_s.sub(/\?$/,"")}]" + end + +end \ No newline at end of file diff --git a/lib/helpers/tabs_helper.rb b/lib/helpers/tabs_helper.rb new file mode 100644 index 0000000..90663dd --- /dev/null +++ b/lib/helpers/tabs_helper.rb @@ -0,0 +1,65 @@ +# http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates +# module JqueryUiRailsHelpers + +module TabsHelper + def tabs_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw TabsHelper::TabsRenderer.new( *options, &block ).render + end + + class TabsRenderer + + def initialize( options={}, &block ) + raise ArgumentError, "Missing block" unless block_given? + + @template = eval( 'self', block.binding ) + @options = options + @tabs = [] + + yield self + 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, {:ajax => false} ] + end + + def create_ajax( link, tab_text, options={}) + @tabs << [ link, tab_text, options, nil, {:ajax => true} ] + end + + def render + content_tag( :div, raw([render_tabs, render_bodies].join), { :id => :tabs }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def render_tabs + content_tag :ul do + result = @tabs.collect do |tab| + if tab[4][:ajax] + content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "#{tab[0]}" ) ) + else + content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) + end + end.join + raw(result) + end + end + + def render_bodies + @tabs.collect do |tab| + if tab[4][:ajax] + # there are no divs for ajaxed tabs + else + content_tag( :div, capture( &tab[3] ), tab[2].merge( :id => tab[0] ) ) + end + end.join.to_s + end + + def method_missing( *args, &block ) + @template.send( *args, &block ) + end + + end +end diff --git a/lib/jquery_ui_rails_helpers.rb b/lib/jquery_ui_rails_helpers.rb new file mode 100644 index 0000000..bdb7974 --- /dev/null +++ b/lib/jquery_ui_rails_helpers.rb @@ -0,0 +1,12 @@ +require 'action_view' +require "jquery_ui_rails_helpers/version" +require 'helpers/javascripts_helper' +require 'helpers/tabs_helper' +require 'helpers/accordions_helper' + +module JqueryUiRailsHelpers +end + +ActionView::Base.send(:include, JavascriptsHelper) +ActionView::Base.send(:include, TabsHelper) +ActionView::Base.send(:include, AccordionsHelper) \ No newline at end of file diff --git a/lib/jquery_ui_rails_helpers/version.rb b/lib/jquery_ui_rails_helpers/version.rb new file mode 100644 index 0000000..cc17960 --- /dev/null +++ b/lib/jquery_ui_rails_helpers/version.rb @@ -0,0 +1,3 @@ +module JqueryUiRailsHelpers + VERSION = "0.0.2" +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..7baea4a --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,34 @@ +require 'rubygems' +require 'bundler' + +Bundler.setup + +require 'test/unit' +require 'shoulda' +require 'action_controller' +require 'action_controller/test_case' +require 'action_view' +require 'action_view/template' +require 'action_view/test_case' + +$:.unshift File.expand_path("../../lib", __FILE__) +require 'jquery_ui_rails_helpers' + +class ActionView::TestCase + +# Take care of the RuntimeError: +# In order to use #url_for, you must include routing helpers explicitly. +# For instance, `include Rails.application.routes.url_helpers + setup :shhhhhh_url_helpers + + def shhhhhh_url_helpers + def @controller._routes + Module.new do + def self.url_helpers + Module.new + end + end + end + end + +end \ No newline at end of file diff --git a/test/unit/helper/accordions_helper_test.rb b/test/unit/helper/accordions_helper_test.rb new file mode 100644 index 0000000..9728bcf --- /dev/null +++ b/test/unit/helper/accordions_helper_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class AccordionsHelperTest < ActionView::TestCase + + context "creating accordions without a block syntax" do + should 'raises an exception' do + assert_raise ArgumentError do + @accordions = accordions_for + end + end + end + + context "creating one set of accordions" do + setup do + @accordions = accordions_for do |accordion| + accordion.create('accordion_one', 'One') { "Accordion One." } + accordion.create('accordion_two', 'Two') { "Accordion Two." } + end + end + + should 'have proper dom structure' do + render :text => @accordions + assert_select "div[id='accordions']", 1 + assert_select "div[id='accordions'] h3[id='accordion_one']", 1 + assert_select "div[id='accordions'] h3[id='accordion_one'] a", {:count => 1, :text => "One"} + assert_select "div[id='accordions'] h3[id='accordion_two']", 1 + assert_select "div[id='accordions'] h3[id='accordion_two'] a", {:count => 1, :text => "Two"} + assert_select "div[id='accordions'] div", 2 + end + end + +end + +#
diff --git a/test/unit/helper/tabs_helper_test.rb b/test/unit/helper/tabs_helper_test.rb new file mode 100644 index 0000000..d88e2db --- /dev/null +++ b/test/unit/helper/tabs_helper_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' + +class TabsHelperTest < ActionView::TestCase + + context "creating tabs without a block syntax" do + should 'raises an exception' do + assert_raise ArgumentError do + @tabs = tabs_for + end + end + end + + context "creating two tabs" do + setup do + @tabs = tabs_for do |tab| + tab.create('tab_one', 'One') { "Tab One." } + tab.create('tab_two', 'Two') { "Tab Two." } + end + puts @tabs + end + + should 'have proper dom structure' do + render :text => @tabs + assert_select "div[id='tabs']", 1 + assert_select "div[id='tabs'] ul", 1 + assert_select "div[id='tabs'] ul li", 2 + assert_select "div[id='tabs'] div[id='tab_one']", 1 + assert_select "div[id='tabs'] div[id='tab_two']", 1 + end + end + + context "creating custom tabs" do + setup do + @tabs = tabs_for(:id => "my_tabs") do |tab| + tab.create_ajax('http://www.codeofficer.com/', 'Ajax') + end + end + + should 'allow overriding the outter divs id' do + render :text => @tabs + assert_select "div[id='my_tabs']", 1 + end + + should 'allow ajaxed tabs' do + render :text => @tabs + assert_select "div[id='my_tabs'] ul li a[href='http://www.codeofficer.com/']", 1 + assert_select "div[id='my_tabs'] div", 0 + end + end + +end + +# \ No newline at end of file