Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit e8a34c58c7986964cb5d7ba79aa90978db87bfbd 0 parents
@p8 p8 authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Petrik de Heus
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 README
@@ -0,0 +1,33 @@
+TableBuilder
+============
+
+Builder for creating tables inspired by ActionView::Helpers::FormHelper::FormBuilder.
+
+Example
+=======
+
+All tags can have extra html options.
+
+@drummers = [Drummer.new(1, 'John "Stumpy" Pepys'), Drummer.new(2, 'Eric "Stumpy Joe" Childs')]
+
+<% table_for(@drummers, :html => { :id => 'spinal_tap', :class => 'drummers'}) do |t| %>
+ <% t.body_r do |e| %>
+ <%= t.d(h(e.id), :class => 'class') %>
+ <%= t.d(h(e.name)) %>
+ <% end %>
+<% end %>
+
+Produces the following html:
+
+<table class="drummers" id="spinal_tap">
+ <tbody><tr>
+ <td class="class">1</td>
+ <td>John &quot;Stumpy&quot; Pepys</td>
+ </tr><tr>
+ <td class="class">2</td>
+ <td>Eric &quot;Stumpy Joe&quot; Childs</td>
+ </tr></tbody>
+</table>
+
+
+Copyright (c) 2008 Petrik de Heus, released under the MIT license
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the table_builder plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the table_builder plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'TableBuilder'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
4 init.rb
@@ -0,0 +1,4 @@
+# Include hook code here
+require 'table_builder'
+
+ActionView::Base.send :include, TableHelper
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
91 lib/table_builder.rb
@@ -0,0 +1,91 @@
+module TableHelper
+
+ def table_for(objects, *args, &proc)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ html_options = options[:html]
+ builder = options[:builder] || TableBuilder
+
+ concat(tag(:table, html_options, true), proc.binding)
+ yield builder.new(objects || [], self, options, proc)
+ concat('</table>', proc.binding)
+ end
+
+ class TableBuilder
+ include ::ActionView::Helpers::TagHelper
+
+ def initialize(objects, template, options, proc)
+ raise ArgumentError, "TableBuilder expects an Array but found a #{objects.inspect}" unless objects.is_a? Array
+ @objects, @template, @options, @proc = objects, template, options, proc
+ end
+
+ def head(*cols)
+ @num_of_columns = cols.size
+ content_tag(:thead,
+ content_tag(:tr,
+ cols.collect { |c| content_tag(:th, c)}.join('')
+ )
+ )
+ end
+
+ def body(*args, &proc)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = options_from_hash(args)
+ tbody(proc) do
+ @objects.each { |c| yield(c) }
+ end
+ end
+
+ def body_r(*args, &proc)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = options_from_hash(args)
+ tbody(proc) do
+ @objects.each { |c|
+ concat(tag(:tr, options, true), proc)
+ yield(c)
+ concat('</tr>', proc)
+ }
+ end
+ end
+
+ def r(*args, &proc)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = options_from_hash(args)
+ tr(options, proc) do
+ yield
+ end
+ end
+
+ def d(content, *args)
+ options = options_from_hash(args)
+ content_tag(:td, content, options)
+ end
+
+ private
+
+ def options_from_hash(args)
+ args.last.is_a?(Hash) ? args.pop : {}
+ end
+
+ def concat(tag, proc)
+ @template.concat(tag, proc.binding)
+ end
+
+ def content_tag(tag, content, *args)
+ options = options_from_hash(args)
+ @template.content_tag(tag, content, options)
+ end
+
+ def tbody proc
+ concat('<tbody>', proc)
+ yield
+ concat('</tbody>', proc)
+ end
+
+ def tr options, proc
+ concat(tag(:tr, options, true), proc)
+ yield
+ concat('</tr>', proc)
+ end
+ end
+end
4 tasks/table_builder_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :table_builder do
+# # Task goes here
+# end
114 test/table_builder_test.rb
@@ -0,0 +1,114 @@
+require File.join(File.dirname(__FILE__), 'test_helper.rb')
+
+class TableBuilderTest < Test::Unit::TestCase
+ include ActionView::Helpers::TextHelper
+ include ActionView::Helpers::TagHelper
+ include TableHelper
+
+ def setup
+ @drummer1 = Drummer.new(1, 'John "Stumpy" Pepys')
+ @drummer2 = Drummer.new(2, 'Eric "Stumpy Joe" Childs')
+ @drummer3 = Drummer.new(3, 'Peter "James" Bond')
+ @drummer4 = Drummer.new(4, 'Mick Shrimpton (R. J. "Ric" Parnell)')
+ end
+
+ def test_table_for
+ _erbout = ''
+ table_for([], :html => { :id => 'id', :style => 'style', :class => 'class'}) do |t|
+ end
+ expected = %(<table id="id" style="style" class="class">) <<
+ %(</table>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_table_for_without_an_array_raises_error
+ _erbout = ''
+ assert_raises(ArgumentError) do
+ table_for('a') {|t| }
+ end
+ end
+
+ def test_table_for_head
+ _erbout = ''
+ table_for([@drummer1, @drummer2]) do |t|
+ _erbout.concat t.head('Id', 'Name')
+ end
+ expected = %(<table>) <<
+ %(<thead>) <<
+ %(<tr>) <<
+ %(<th>Id</th>) <<
+ %(<th>Name</th>) <<
+ %(</tr>) <<
+ %(</thead>) <<
+ %(</table>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_table_for_body
+ _erbout = ''
+ table_for([@drummer3, @drummer4]) do |t|
+ t.body do |e|
+ t.r do
+ _erbout.concat t.d(e.id)
+ _erbout.concat t.d(e.name)
+ end
+ end
+ end
+ expected = %(<table>) <<
+ %(<tbody>) <<
+ %(<tr>) <<
+ %(<td>3</td>) <<
+ %(<td>Peter "James" Bond</td>) <<
+ %(</tr>) <<
+ %(<tr>) <<
+ %(<td>4</td>) <<
+ %(<td>Mick Shrimpton (R. J. "Ric" Parnell)</td>) <<
+ %(</tr>) <<
+ %(</tbody>) <<
+ %(</table>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_table_for_body_r
+ _erbout = ''
+ table_for([@drummer3, @drummer4]) do |t|
+ t.body_r do |e|
+ _erbout.concat t.d(e.id)
+ _erbout.concat t.d(e.name)
+ end
+ end
+ expected = %(<table>) <<
+ %(<tbody>) <<
+ %(<tr>) <<
+ %(<td>3</td>) <<
+ %(<td>Peter "James" Bond</td>) <<
+ %(</tr>) <<
+ %(<tr>) <<
+ %(<td>4</td>) <<
+ %(<td>Mick Shrimpton (R. J. "Ric" Parnell)</td>) <<
+ %(</tr>) <<
+ %(</tbody>) <<
+ %(</table>)
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_table_for_td_with_options
+ _erbout = ''
+ table_for([@drummer1]) do |t|
+ t.body_r do |e|
+ _erbout.concat t.d(e.name, :class => 'class')
+ end
+ end
+ expected = %(<table>) <<
+ %(<tbody>) <<
+ %(<tr>) <<
+ %(<td class="class">John "Stumpy" Pepys</td>) <<
+ %(</tr>) <<
+ %(</tbody>) <<
+ %(</table>)
+ assert_dom_equal expected, _erbout
+ end
+
+end
+
+class Drummer < Struct.new(:id, :name); end
10 test/test_helper.rb
@@ -0,0 +1,10 @@
+require 'test/unit'
+
+require "rubygems"
+require 'active_support'
+require 'action_pack'
+require 'action_controller'
+require 'action_view'
+require 'action_controller/test_process'
+
+require(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'table_builder.rb')))
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.