Permalink
Browse files

Define attributes on the cell

  • Loading branch information...
1 parent 77ca4b2 commit 4db4bd87537dd2a14b0cccf9eb689a1c46bbf078 Joe Fiorini committed Jan 16, 2010
Showing with 28 additions and 12 deletions.
  1. +6 −1 lib/totally_tabular/column.rb
  2. +1 −1 lib/totally_tabular/table_view.rb
  3. +21 −10 spec/lib/totally_tabular/table_view_spec.rb
@@ -1,11 +1,12 @@
module TotallyTabular
class Column
- attr_accessor :name, :template, :header_attributes, :row_attributes, :definition
+ attr_accessor :name, :template, :header_attributes, :row_attributes, :definition, :cell_attributes
def initialize(name, &block)
@name = name
@header_attributes = {}
@row_attributes = {}
+ @cell_attributes = {}
@definition = block
end
@@ -21,5 +22,9 @@ def row_attributes!(attributes={})
@row_attributes = attributes
end
+ def cell_attributes!(attributes={})
+ @cell_attributes = attributes
+ end
+
end
end
@@ -41,7 +41,7 @@ def build_rows(collection, columns)
row.render(columns.map do |column|
column.instance_exec(item, &column.definition)
row.attributes!(column.row_attributes)
- @helper.content_tag(:td, column.template.call(item, row))
+ @helper.content_tag(:td, column.template.call(item, row), column.cell_attributes)
end.join)
end
end
@@ -63,13 +63,13 @@ def selector(string, selector)
o3 = OpenStruct.new(:name => "Gary", :coolness => "Eh.", :age => 26)
o4 = OpenStruct.new(:name => "Josh", :coolness => "Lame.", :age => 26)
@table_view = TableView.new([o, o2, o3, o4]) do |t|
- t.define_column("Name") do
- template! do |o, row|
+ t.define_column("Name") do |o|
+ template! do
"%s is %s" % [o.name, o.coolness]
end
end
- t.define_column("Age") do
- template! do |o, row|
+ t.define_column("Age") do |o|
+ template! do
o.age
end
end
@@ -94,9 +94,9 @@ def selector(string, selector)
it "allows defining class on header" do
t = TableView.new([1]) do
- define_column("Blah") do
+ define_column("Blah") do |o|
header_attributes!(:class => 'blahdiddy')
- template! do |o, row|
+ template! do
end
end
end
@@ -106,9 +106,9 @@ def selector(string, selector)
it "allows defining a class on row" do
t = TableView.new([1]) do
- define_column("Blah") do
+ define_column("Blah") do |o|
row_attributes!(:class => 'diddy')
- template! do |o, row|
+ template! do
end
end
end
@@ -129,6 +129,17 @@ def selector(string, selector)
selector(t.render, "table tr.unread").length.should == 1
end
+ it "allows defining a class on the cell" do
+ t = TableView.new([1]) do
+ define_column("Blah") do |o|
+ cell_attributes!(:class => 'diddy')
+ template! {}
+ end
+ end
+
+ selector(t.render, "table td.diddy").length.should == 1
+ end
+
it "should take a block and give a table class" do
table = TableView::Table.new
TableView::Table.stub!(:new).and_return(table)
@@ -155,8 +166,8 @@ def selector(string, selector)
it "should allow defining the column body when defining the column" do
t = TableView.new(["Judge"]) do
- define_column("Name") do
- template! do |item, row|
+ define_column("Name") do |item|
+ template! do
"My name is %s." % item
end
end

0 comments on commit 4db4bd8

Please sign in to comment.