Permalink
Browse files

Add default presenter for table rendering.

  • Loading branch information...
1 parent c0303aa commit bf4ad23db7c2160d6c59e69bb840790790dc69b7 @bobbytables committed Sep 28, 2012
View
@@ -1,9 +1,13 @@
require 'action_view'
require 'table_cloth/version'
-require 'table_cloth/configuration'
-require 'table_cloth/base'
-require 'table_cloth/column'
-require 'table_cloth/presenter'
module TableCloth
+ autoload :Configuration, 'table_cloth/configuration'
+ autoload :Base, 'table_cloth/base'
+ autoload :Column, 'table_cloth/column'
+ autoload :Presenter, 'table_cloth/presenter'
+
+ module Presenters
+ autoload :Default, 'table_cloth/presenters/default'
+ end
end
View
@@ -20,5 +20,7 @@ def presenter(klass)
@presenter = klass
end
end
+
+ presenter ::TableCloth::Presenters::Default
end
end
@@ -8,20 +8,20 @@ def initialize(objects, table, view)
@objects = objects
end
- def render
- raise NoMethodError, "You must override the .render method"
+ def v
+ view_context
end
- def container
- raise NoMethodError, "You must override the .container method"
+ def render_table
+ raise NoMethodError, "You must override the .render method"
end
- def header
+ def render_header
raise NoMethodError, "You must override the .header method"
end
- def row
- raise NoMethodError, "You must override the .row method"
+ def render_rows
+ raise NoMethodError, "You must override the .rows method"
end
def column_names
@@ -0,0 +1,43 @@
+module TableCloth
+ module Presenters
+ class Default < ::TableCloth::Presenter
+ def render_table
+ v.content_tag :table do
+ render_header + render_rows
+ end
+ end
+
+ def render_rows
+ v.content_tag :tbody do
+ body = rows.inject('') do |r, values|
+ r + render_row(values)
+ end
+
+ v.raw(body)
+ end
+ end
+
+ def render_row(values)
+ v.content_tag :tr do
+ row = values.inject('') do |tds, value|
+ tds + v.content_tag(:td, value)
+ end
+
+ v.raw(row)
+ end
+ end
+
+ def render_header
+ v.content_tag :thead do
+ v.content_tag :tr do
+ names = column_names.inject('') do |tags, name|
+ tags + v.content_tag(:th, name)
+ end
+
+ v.raw(names)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,77 @@
+require 'spec_helper'
+
+describe TableCloth::Presenters::Default do
+ let(:dummy_table) { DummyTable }
+ let(:dummy_model) do
+ DummyModel.new.tap do |d|
+ d.id = 1
+ d.email = 'robert@example.com'
+ d.name = 'robert'
+ end
+ end
+
+ let(:objects) do
+ 3.times.map do |n|
+ num = n+1
+ DummyModel.new.tap do |d|
+ d.id = num # Wat
+ d.email = "robert#{num}@example.com"
+ d.name = "robert#{num}"
+ end
+ end
+ end
+
+ let(:view_context) { ActionView::Base.new }
+ subject { TableCloth::Presenters::Default.new(objects, dummy_table, view_context) }
+
+ it 'creates a table head' do
+ header = subject.render_header
+ doc = Nokogiri::HTML(header)
+
+ thead = doc.xpath('//thead')
+ thead.should be_present
+
+ tr = thead.xpath('//tr')
+ tr.should be_present
+
+ th = tr.xpath('//th')
+ th.should be_present
+
+ th.length.should == 3
+ end
+
+ it 'creates rows' do
+ rows = subject.render_rows
+ doc = Nokogiri::HTML(rows)
+
+ tbody = doc.xpath('//tbody')
+ tbody.should be_present
+
+ tbody.xpath('//tr').each_with_index do |row, row_index|
+ row.xpath('/td').each_with_index do |td, td_index|
+ case td_index
+ when 0
+ object.id.to_s == td.text
+ when 1
+ object.name.should == td.text
+ when 2
+ object.email.should == td.text
+ end
+ end
+ end
+ end
+
+ it 'creates an entire table' do
+ doc = Nokogiri::HTML(subject.render_table)
+ table = doc.xpath('//table')
+ table.should be_present
+
+ thead = table.xpath('//thead')
+ thead.should be_present
+
+ tbody = table.at_xpath('//tbody')
+ tbody.should be_present
+
+ tbody.css('tr').length.should == 3
+ end
+end
View
@@ -1,5 +1,7 @@
require 'table_cloth'
require 'awesome_print'
+require 'nokogiri'
+require 'pry'
Dir['./spec/support/**/*.rb'].each {|f| require f }
View
@@ -19,6 +19,8 @@ Gem::Specification.new do |gem|
gem.add_development_dependency('rspec', '~> 2.11')
gem.add_development_dependency('awesome_print')
-
+ gem.add_development_dependency('nokogiri')
+ gem.add_development_dependency('pry')
+
gem.add_dependency('actionpack', '~> 3.2')
end

0 comments on commit bf4ad23

Please sign in to comment.