From c5dae9f61bd367a19f997da06af62ea57b1b2ba6 Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Mon, 24 Jan 2011 18:31:19 -0500 Subject: [PATCH 1/7] ignoring files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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 From 5a82e52d7449979825b0842a9f7f1c94549b4362 Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Mon, 24 Jan 2011 18:31:47 -0500 Subject: [PATCH 2/7] setting up basic tests for tabs_helper --- Gemfile | 4 ++ Gemfile.lock | 83 ++++++++++++++++++++++++++ Rakefile | 15 +++++ jquery_ui_rails_helpers.gemspec | 27 +++++++++ lib/helpers/accordions_helper.rb | 50 ++++++++++++++++ lib/helpers/tabs_helper.rb | 60 +++++++++++++++++++ lib/jquery_ui_rails_helpers.rb | 9 +++ lib/jquery_ui_rails_helpers/version.rb | 3 + notes/accordion.rb | 48 +++++++++++++++ notes/misc.rb | 16 +++++ notes/notes.txt | 31 ++++++++++ notes/tab.rb | 59 ++++++++++++++++++ test/test_helper.rb | 61 +++++++++++++++++++ test/unit/helper/tabs_helper_test.rb | 39 ++++++++++++ 14 files changed, 505 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile create mode 100644 jquery_ui_rails_helpers.gemspec create mode 100644 lib/helpers/accordions_helper.rb create mode 100644 lib/helpers/tabs_helper.rb create mode 100644 lib/jquery_ui_rails_helpers.rb create mode 100644 lib/jquery_ui_rails_helpers/version.rb create mode 100644 notes/accordion.rb create mode 100644 notes/misc.rb create mode 100644 notes/notes.txt create mode 100644 notes/tab.rb create mode 100644 test/test_helper.rb create mode 100644 test/unit/helper/tabs_helper_test.rb 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..301ec1b --- /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.14) + 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/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/lib/helpers/accordions_helper.rb b/lib/helpers/accordions_helper.rb new file mode 100644 index 0000000..74f93fe --- /dev/null +++ b/lib/helpers/accordions_helper.rb @@ -0,0 +1,50 @@ +# module JqueryUiRailsHelpers + + module AccordionsHelper + def accordions_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render + end + + class AccordionsRenderer + + def initialize( options={}, &block ) + raise ArgumentError, "Missing block" unless block_given? + + @template = eval( 'self', block.binding ) + @options = options + @accordions = [] + + 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 = @accordions.collect do |accordion| + accordion_head(accordion) << accordion_body(accordion) + end.join + content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def accordion_head(accordion) + content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0] + end + + def accordion_body(accordion) + content_tag :div, capture( &accordion[3] ) + end + + def method_missing( *args, &block ) + @template.send( *args, &block ) + end + + end + 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..11febd5 --- /dev/null +++ b/lib/helpers/tabs_helper.rb @@ -0,0 +1,60 @@ +# http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates +# module JqueryUiRailsHelpers + + module TabsHelper + def verticle_tabs_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw TabsHelper::TabsRenderer.new( *options, &block ).render + end + + 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 ] + end + + def render + content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def render_tabs + content_tag :ul do + result = @tabs.collect do |tab| + content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) + end.join + raw(result) + 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 + +# end \ No newline at end of file diff --git a/lib/jquery_ui_rails_helpers.rb b/lib/jquery_ui_rails_helpers.rb new file mode 100644 index 0000000..383c08c --- /dev/null +++ b/lib/jquery_ui_rails_helpers.rb @@ -0,0 +1,9 @@ +require 'action_view' +require 'helpers/tabs_helper' +require 'helpers/accordions_helper' + +module JqueryUiRailsHelpers +end + +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..f1351ba --- /dev/null +++ b/lib/jquery_ui_rails_helpers/version.rb @@ -0,0 +1,3 @@ +module JqueryUiRailsHelpers + VERSION = "0.0.1" +end diff --git a/notes/accordion.rb b/notes/accordion.rb new file mode 100644 index 0000000..463aa59 --- /dev/null +++ b/notes/accordion.rb @@ -0,0 +1,48 @@ +module AccordionsHelper + + def accordions_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render + end + + class AccordionsRenderer + + def initialize( options={}, &block ) + raise ArgumentError, "Missing block" unless block_given? + + @template = eval( 'self', block.binding ) + @options = options + @accordions = [] + + 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 = @accordions.collect do |accordion| + accordion_head(accordion) << accordion_body(accordion) + end.join + content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def accordion_head(accordion) + content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0] + end + + def accordion_body(accordion) + content_tag :div, capture( &accordion[3] ) + end + + def method_missing( *args, &block ) + @template.send( *args, &block ) + end + + end + +end \ No newline at end of file diff --git a/notes/misc.rb b/notes/misc.rb new file mode 100644 index 0000000..6d3ce29 --- /dev/null +++ b/notes/misc.rb @@ -0,0 +1,16 @@ + + 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 \ No newline at end of file diff --git a/notes/notes.txt b/notes/notes.txt new file mode 100644 index 0000000..0fee31c --- /dev/null +++ b/notes/notes.txt @@ -0,0 +1,31 @@ +http://technicalpickles.com/posts/helper-testing-using-actionview-testcase/ +http://blog.costan.us/2010/08/helper-testing-in-rails-3.html +http://www.slideshare.net/noelrap/how-to-test-everything +http://rails-nutshell.labs.oreilly.com/ch11.html +https://gist.github.com/52976 +https://github.com/vigetlabs/helper_me_test/ +http://tj.stank.us/2009/01/26/testing-html-strings-returned-from-helpers.html + + # Unfortunately, there is currently an abstraction leak between AC::Base + # and AV::Base which requires having the URL helpers in both AC and AV. + # To do this safely at runtime for tests, we need to bump up the helper serial + # to that the old AV subclass isn't cached. + actionpack-3.0.3/lib/action_dispatch/testing/assertions/routing.rb + +<%= tabs_for do |tab| %> + <% tab.create('project', 'Project') do %> + <%= render :partial => 'form', :locals => {:project => @project} %> + <% end %> + <% tab.create('status_updates', 'Status Updates') do %> + <%= render :partial => 'status_updates', :locals => {:project => @project} %> + <% end if current_user.try(:admin?) %> + <% tab.create('budget_updates', 'Budget Updates') do %> + <%= render :partial => 'budget_updates', :locals => {:project => @project} %> + <% end if current_user.try(:admin?) %> + <% tab.create('principle_investigator', 'Principle Investigator') do %> + <%= render :partial => 'principle_investigators', :locals => {:project => @project} %> + <% end if current_user.try(:admin?) %> + <% tab.create('investigators', 'Investigators') do %> + <%= render :partial => 'investigators', :locals => {:project => @project} %> + <% end if current_user.try(:admin?) %> +<% end %> \ No newline at end of file diff --git a/notes/tab.rb b/notes/tab.rb new file mode 100644 index 0000000..407f238 --- /dev/null +++ b/notes/tab.rb @@ -0,0 +1,59 @@ +# http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates + +module TabsHelper + + def verticle_tabs_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw TabsHelper::TabsRenderer.new( *options, &block ).render + end + + 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 ] + end + + def render + content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def render_tabs + content_tag :ul do + result = @tabs.collect do |tab| + content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) + end.join + raw(result) + 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/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..59d400a --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,61 @@ +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' + +# https://gist.github.com/52976 +# module HelpersSelectorAsssertions +# def assert_select_in(html, *args, &block) +# node = HTML::Document.new(html).root +# assert_select(*args.unshift(node), &block) +# end +# end +# ActionView::TestCase.send(:include, HelpersSelectorAsssertions) + +class ActionView::TestCase + +# Take care of 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 + + # setup :setup_with_controller + # + # def setup_with_controller + # @controller = ActionView::TestCase::TestController.new + # def @controller._routes + # Module.new do + # def self.url_helpers + # Module.new + # end + # end + # end + # @request = @controller.request + # @output_buffer = ActiveSupport::SafeBuffer.new + # @rendered = '' + # make_test_case_available_to_view! + # say_no_to_protect_against_forgery! + # end + +end \ No newline at end of file diff --git a/test/unit/helper/tabs_helper_test.rb b/test/unit/helper/tabs_helper_test.rb new file mode 100644 index 0000000..04068a9 --- /dev/null +++ b/test/unit/helper/tabs_helper_test.rb @@ -0,0 +1,39 @@ +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 one set of tabs" do + setup do + @tabs = tabs_for do |tab| + tab.create('information', 'Information') { "Information blurbage." } + tab.create('about_us', 'About Us') { "About us blurbage." } + end + end + + should 'have proper dom structure' do + render :text => @tabs + assert_select "div[id='tabs']" + assert_select "div[id='tabs'] ul" + assert_select "div[id='tabs'] ul li", 2 + assert_select "div[id='tabs'] div[id='information']" + assert_select "div[id='tabs'] div[id='about_us']" + + assert_select "div[id='tabs']" do |outer_divs| + outer_divs.each do |outer_div| + assert_select outer_div, "ul", 1 do |uls| + assert_select uls[0], "li", 2 + end + end + end + end + end + +end \ No newline at end of file From 525e5498a86c63f36c9e607953edbcc21bf2ff83 Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Mon, 24 Jan 2011 18:38:09 -0500 Subject: [PATCH 3/7] more stuff --- lib/helpers/accordions_helper.rb | 73 +++++++++--------- lib/helpers/javascripts_helper.rb | 19 +++++ lib/helpers/tabs_helper.rb | 107 +++++++++++++-------------- lib/jquery_ui_rails_helpers.rb | 2 + notes/notes.txt | 10 ++- test/unit/helper/tabs_helper_test.rb | 16 +--- 6 files changed, 119 insertions(+), 108 deletions(-) create mode 100644 lib/helpers/javascripts_helper.rb diff --git a/lib/helpers/accordions_helper.rb b/lib/helpers/accordions_helper.rb index 74f93fe..d613174 100644 --- a/lib/helpers/accordions_helper.rb +++ b/lib/helpers/accordions_helper.rb @@ -1,50 +1,47 @@ -# module JqueryUiRailsHelpers +module AccordionsHelper + def accordions_for( *options, &block ) + raise ArgumentError, "Missing block" unless block_given? + raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render + end - module AccordionsHelper - def accordions_for( *options, &block ) - raise ArgumentError, "Missing block" unless block_given? - raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render - end + class AccordionsRenderer - class AccordionsRenderer + def initialize( options={}, &block ) + raise ArgumentError, "Missing block" unless block_given? - def initialize( options={}, &block ) - raise ArgumentError, "Missing block" unless block_given? + @template = eval( 'self', block.binding ) + @options = options + @accordions = [] - @template = eval( 'self', block.binding ) - @options = options - @accordions = [] + yield self + end - 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 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 = @accordions.collect do |accordion| + accordion_head(accordion) << accordion_body(accordion) + end.join + content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) ) + 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, link_to(accordion[1], '#'), :id => accordion[0] + end - def accordion_head(accordion) - content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0] - end + def accordion_body(accordion) + content_tag :div, capture( &accordion[3] ) + end - def accordion_body(accordion) - content_tag :div, capture( &accordion[3] ) - end + def method_missing( *args, &block ) + @template.send( *args, &block ) + end - def method_missing( *args, &block ) - @template.send( *args, &block ) - end + end +end - end - end - -# end \ No newline at end of file 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 index 11febd5..896e142 100644 --- a/lib/helpers/tabs_helper.rb +++ b/lib/helpers/tabs_helper.rb @@ -1,60 +1,53 @@ # http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates # module JqueryUiRailsHelpers - module TabsHelper - def verticle_tabs_for( *options, &block ) - raise ArgumentError, "Missing block" unless block_given? - raw TabsHelper::TabsRenderer.new( *options, &block ).render - end - - 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 ] - end - - def render - content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) - end - - private # --------------------------------------------------------------------------- - - def render_tabs - content_tag :ul do - result = @tabs.collect do |tab| - content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) - end.join - raw(result) - 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 - -# end \ No newline at end of file +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 ] + end + + def render + content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) + end + + private # --------------------------------------------------------------------------- + + def render_tabs + content_tag :ul do + result = @tabs.collect do |tab| + content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) + end.join + raw(result) + 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/lib/jquery_ui_rails_helpers.rb b/lib/jquery_ui_rails_helpers.rb index 383c08c..a7d6dd4 100644 --- a/lib/jquery_ui_rails_helpers.rb +++ b/lib/jquery_ui_rails_helpers.rb @@ -1,9 +1,11 @@ require 'action_view' +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/notes/notes.txt b/notes/notes.txt index 0fee31c..e1d60d8 100644 --- a/notes/notes.txt +++ b/notes/notes.txt @@ -28,4 +28,12 @@ http://tj.stank.us/2009/01/26/testing-html-strings-returned-from-helpers.html <% tab.create('investigators', 'Investigators') do %> <%= render :partial => 'investigators', :locals => {:project => @project} %> <% end if current_user.try(:admin?) %> -<% end %> \ No newline at end of file +<% end %> + +assert_select "div[id='tabs']" do |outer_divs| + outer_divs.each do |outer_div| + assert_select outer_div, "ul", 1 do |uls| + assert_select uls[0], "li", 2 + end + end +end diff --git a/test/unit/helper/tabs_helper_test.rb b/test/unit/helper/tabs_helper_test.rb index 04068a9..3dfa223 100644 --- a/test/unit/helper/tabs_helper_test.rb +++ b/test/unit/helper/tabs_helper_test.rb @@ -20,19 +20,11 @@ class TabsHelperTest < ActionView::TestCase should 'have proper dom structure' do render :text => @tabs - assert_select "div[id='tabs']" - assert_select "div[id='tabs'] ul" + 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='information']" - assert_select "div[id='tabs'] div[id='about_us']" - - assert_select "div[id='tabs']" do |outer_divs| - outer_divs.each do |outer_div| - assert_select outer_div, "ul", 1 do |uls| - assert_select uls[0], "li", 2 - end - end - end + assert_select "div[id='tabs'] div[id='information']", 1 + assert_select "div[id='tabs'] div[id='about_us']", 1 end end From 09727cd7d21e356ec719497fe220351f68878f70 Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Wed, 26 Jan 2011 01:04:28 -0500 Subject: [PATCH 4/7] ommiting crap --- test/test_helper.rb | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 59d400a..7baea4a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,18 +14,9 @@ $:.unshift File.expand_path("../../lib", __FILE__) require 'jquery_ui_rails_helpers' -# https://gist.github.com/52976 -# module HelpersSelectorAsssertions -# def assert_select_in(html, *args, &block) -# node = HTML::Document.new(html).root -# assert_select(*args.unshift(node), &block) -# end -# end -# ActionView::TestCase.send(:include, HelpersSelectorAsssertions) - class ActionView::TestCase -# Take care of RuntimeError: +# 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 @@ -40,22 +31,4 @@ def self.url_helpers end end - # setup :setup_with_controller - # - # def setup_with_controller - # @controller = ActionView::TestCase::TestController.new - # def @controller._routes - # Module.new do - # def self.url_helpers - # Module.new - # end - # end - # end - # @request = @controller.request - # @output_buffer = ActiveSupport::SafeBuffer.new - # @rendered = '' - # make_test_case_available_to_view! - # say_no_to_protect_against_forgery! - # end - end \ No newline at end of file From 2dd2e12150ba2e5b502afc20d9a7eededa40e40d Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Wed, 26 Jan 2011 02:16:21 -0500 Subject: [PATCH 5/7] boom --- helpers/accordions_helper.rb | 56 -------------------- helpers/tabs_helper.rb | 54 -------------------- lib/helpers/tabs_helper.rb | 18 +++++-- notes/accordion.rb | 48 ------------------ notes/misc.rb | 16 ------ notes/notes.txt | 39 -------------- notes/tab.rb | 59 ---------------------- test/unit/helper/accordions_helper_test.rb | 39 ++++++++++++++ test/unit/helper/tabs_helper_test.rb | 41 ++++++++++++--- 9 files changed, 89 insertions(+), 281 deletions(-) delete mode 100644 helpers/accordions_helper.rb delete mode 100644 helpers/tabs_helper.rb delete mode 100644 notes/accordion.rb delete mode 100644 notes/misc.rb delete mode 100644 notes/notes.txt delete mode 100644 notes/tab.rb create mode 100644 test/unit/helper/accordions_helper_test.rb diff --git a/helpers/accordions_helper.rb b/helpers/accordions_helper.rb deleted file mode 100644 index 09ed43f..0000000 --- a/helpers/accordions_helper.rb +++ /dev/null @@ -1,56 +0,0 @@ -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 - end - - class AccordionsRenderer - - def initialize( options={}, &block ) - raise ArgumentError, "Missing block" unless block_given? - - @template = eval( 'self', block.binding ) - @options = options - @accordions = [] - - 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 - end - - private # --------------------------------------------------------------------------- - - def accordion_head(accordion) - content_tag :h3, :id => accordion[0] do - link_to accordion[1], '#' - end - end - - def accordion_body(accordion) - content_tag :div do - capture( &accordion[3] ) - end - end - - def method_missing( *args, &block ) - @template.send( *args, &block ) - end - - end - -end 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/lib/helpers/tabs_helper.rb b/lib/helpers/tabs_helper.rb index 896e142..657dbae 100644 --- a/lib/helpers/tabs_helper.rb +++ b/lib/helpers/tabs_helper.rb @@ -21,7 +21,11 @@ def initialize( options={}, &block ) def create( tab_id, tab_text, options={}, &block ) raise "Block needed for TabsRenderer#CREATE" unless block_given? - @tabs << [ tab_id, tab_text, options, block ] + @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 @@ -33,7 +37,11 @@ def render def render_tabs content_tag :ul do result = @tabs.collect do |tab| - content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) + 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 @@ -41,7 +49,11 @@ def render_tabs def render_bodies @tabs.collect do |tab| - content_tag( :div, capture( &tab[3] ), tab[2].merge( :id => tab[0] ) ) + 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 diff --git a/notes/accordion.rb b/notes/accordion.rb deleted file mode 100644 index 463aa59..0000000 --- a/notes/accordion.rb +++ /dev/null @@ -1,48 +0,0 @@ -module AccordionsHelper - - def accordions_for( *options, &block ) - raise ArgumentError, "Missing block" unless block_given? - raw AccordionsHelper::AccordionsRenderer.new( *options, &block ).render - end - - class AccordionsRenderer - - def initialize( options={}, &block ) - raise ArgumentError, "Missing block" unless block_given? - - @template = eval( 'self', block.binding ) - @options = options - @accordions = [] - - 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 = @accordions.collect do |accordion| - accordion_head(accordion) << accordion_body(accordion) - end.join - content_tag( :div, raw(content), { :id => :accordions }.merge( @options ) ) - end - - private # --------------------------------------------------------------------------- - - def accordion_head(accordion) - content_tag :h3, link_to(accordion[1], '#'), :id => accordion[0] - end - - def accordion_body(accordion) - content_tag :div, capture( &accordion[3] ) - end - - def method_missing( *args, &block ) - @template.send( *args, &block ) - end - - end - -end \ No newline at end of file diff --git a/notes/misc.rb b/notes/misc.rb deleted file mode 100644 index 6d3ce29..0000000 --- a/notes/misc.rb +++ /dev/null @@ -1,16 +0,0 @@ - - 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 \ No newline at end of file diff --git a/notes/notes.txt b/notes/notes.txt deleted file mode 100644 index e1d60d8..0000000 --- a/notes/notes.txt +++ /dev/null @@ -1,39 +0,0 @@ -http://technicalpickles.com/posts/helper-testing-using-actionview-testcase/ -http://blog.costan.us/2010/08/helper-testing-in-rails-3.html -http://www.slideshare.net/noelrap/how-to-test-everything -http://rails-nutshell.labs.oreilly.com/ch11.html -https://gist.github.com/52976 -https://github.com/vigetlabs/helper_me_test/ -http://tj.stank.us/2009/01/26/testing-html-strings-returned-from-helpers.html - - # Unfortunately, there is currently an abstraction leak between AC::Base - # and AV::Base which requires having the URL helpers in both AC and AV. - # To do this safely at runtime for tests, we need to bump up the helper serial - # to that the old AV subclass isn't cached. - actionpack-3.0.3/lib/action_dispatch/testing/assertions/routing.rb - -<%= tabs_for do |tab| %> - <% tab.create('project', 'Project') do %> - <%= render :partial => 'form', :locals => {:project => @project} %> - <% end %> - <% tab.create('status_updates', 'Status Updates') do %> - <%= render :partial => 'status_updates', :locals => {:project => @project} %> - <% end if current_user.try(:admin?) %> - <% tab.create('budget_updates', 'Budget Updates') do %> - <%= render :partial => 'budget_updates', :locals => {:project => @project} %> - <% end if current_user.try(:admin?) %> - <% tab.create('principle_investigator', 'Principle Investigator') do %> - <%= render :partial => 'principle_investigators', :locals => {:project => @project} %> - <% end if current_user.try(:admin?) %> - <% tab.create('investigators', 'Investigators') do %> - <%= render :partial => 'investigators', :locals => {:project => @project} %> - <% end if current_user.try(:admin?) %> -<% end %> - -assert_select "div[id='tabs']" do |outer_divs| - outer_divs.each do |outer_div| - assert_select outer_div, "ul", 1 do |uls| - assert_select uls[0], "li", 2 - end - end -end diff --git a/notes/tab.rb b/notes/tab.rb deleted file mode 100644 index 407f238..0000000 --- a/notes/tab.rb +++ /dev/null @@ -1,59 +0,0 @@ -# http://forum.jquery.com/topic/jquery-datepicker-pick-multiple-dates - -module TabsHelper - - def verticle_tabs_for( *options, &block ) - raise ArgumentError, "Missing block" unless block_given? - raw TabsHelper::TabsRenderer.new( *options, &block ).render - end - - 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 ] - end - - def render - content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) - end - - private # --------------------------------------------------------------------------- - - def render_tabs - content_tag :ul do - result = @tabs.collect do |tab| - content_tag( :li, link_to( content_tag( :span, raw(tab[1]) ), "##{tab[0]}" ) ) - end.join - raw(result) - 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/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 + +#
+#

One

+#
Accordion Two.
+#

Two

+#
Accordion One.
+#
diff --git a/test/unit/helper/tabs_helper_test.rb b/test/unit/helper/tabs_helper_test.rb index 3dfa223..7aa3d37 100644 --- a/test/unit/helper/tabs_helper_test.rb +++ b/test/unit/helper/tabs_helper_test.rb @@ -10,11 +10,11 @@ class TabsHelperTest < ActionView::TestCase end end - context "creating one set of tabs" do + context "creating two tabs" do setup do @tabs = tabs_for do |tab| - tab.create('information', 'Information') { "Information blurbage." } - tab.create('about_us', 'About Us') { "About us blurbage." } + tab.create('tab_one', 'One') { "Tab One." } + tab.create('tab_two', 'Two') { "Tab Two." } end end @@ -23,9 +23,38 @@ class TabsHelperTest < ActionView::TestCase 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='information']", 1 - assert_select "div[id='tabs'] div[id='about_us']", 1 + assert_select "div[id='tabs'] div[id='tab_one']", 1 + assert_select "div[id='tabs'] div[id='tab_two']", 1 end end -end \ No newline at end of file + 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 + +#
+# +#
Tab One.
+#
Tab Two.
+#
\ No newline at end of file From baddd47d48ba6fb7a2defd52c099c948af223f49 Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Wed, 26 Jan 2011 03:30:04 -0500 Subject: [PATCH 6/7] fucking brilliant, fixed a ruby 1.9.2 bug --- Gemfile.lock | 2 +- lib/helpers/tabs_helper.rb | 2 +- lib/jquery_ui_rails_helpers.rb | 1 + test/unit/helper/tabs_helper_test.rb | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 301ec1b..7de5f45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,7 +40,7 @@ GEM erubis (2.6.6) abstract (>= 1.0.0) i18n (0.5.0) - mail (2.2.14) + mail (2.2.15) activesupport (>= 2.3.6) i18n (>= 0.4.0) mime-types (~> 1.16) diff --git a/lib/helpers/tabs_helper.rb b/lib/helpers/tabs_helper.rb index 657dbae..90663dd 100644 --- a/lib/helpers/tabs_helper.rb +++ b/lib/helpers/tabs_helper.rb @@ -29,7 +29,7 @@ def create_ajax( link, tab_text, options={}) end def render - content_tag( :div, raw([render_tabs, render_bodies]), { :id => :tabs }.merge( @options ) ) + content_tag( :div, raw([render_tabs, render_bodies].join), { :id => :tabs }.merge( @options ) ) end private # --------------------------------------------------------------------------- diff --git a/lib/jquery_ui_rails_helpers.rb b/lib/jquery_ui_rails_helpers.rb index a7d6dd4..bdb7974 100644 --- a/lib/jquery_ui_rails_helpers.rb +++ b/lib/jquery_ui_rails_helpers.rb @@ -1,4 +1,5 @@ require 'action_view' +require "jquery_ui_rails_helpers/version" require 'helpers/javascripts_helper' require 'helpers/tabs_helper' require 'helpers/accordions_helper' diff --git a/test/unit/helper/tabs_helper_test.rb b/test/unit/helper/tabs_helper_test.rb index 7aa3d37..d88e2db 100644 --- a/test/unit/helper/tabs_helper_test.rb +++ b/test/unit/helper/tabs_helper_test.rb @@ -16,6 +16,7 @@ class TabsHelperTest < ActionView::TestCase tab.create('tab_one', 'One') { "Tab One." } tab.create('tab_two', 'Two') { "Tab Two." } end + puts @tabs end should 'have proper dom structure' do From 2efadac357489249252f467c323925c0c74a034e Mon Sep 17 00:00:00 2001 From: CodeOfficer Date: Wed, 26 Jan 2011 03:31:29 -0500 Subject: [PATCH 7/7] bump --- lib/jquery_ui_rails_helpers/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jquery_ui_rails_helpers/version.rb b/lib/jquery_ui_rails_helpers/version.rb index f1351ba..cc17960 100644 --- a/lib/jquery_ui_rails_helpers/version.rb +++ b/lib/jquery_ui_rails_helpers/version.rb @@ -1,3 +1,3 @@ module JqueryUiRailsHelpers - VERSION = "0.0.1" + VERSION = "0.0.2" end