Skip to content
Browse files

Make presenters work with new separation of columns.

  • Loading branch information...
1 parent 28fed6c commit 0e130a152187c9997c8f5b46db4eec0caaee32b3 @bobbytables committed Dec 15, 2012
View
15 lib/table_cloth/presenter.rb
@@ -4,13 +4,12 @@ class Presenter
:table
def initialize(objects, table, view)
- @view_context = view
- @table_definition = table
@objects = objects
+ @table_definition = table
+ @view_context = view
@table = table_definition.new(objects, view)
end
- # Short hand so your fingers don't hurt
def v
view_context
end
@@ -27,14 +26,20 @@ def render_rows
raise NoMethodError, "You must override the .rows method"
end
+ def columns
+ @columns ||= table.class.columns.map do |name, options|
+ options[:class].new(name, options[:options])
+ end
+ end
+
def column_names
- @column_names ||= table.columns.each_with_object([]) do |(column_name, column), names|
+ @column_names ||= columns.each_with_object([]) do |column, names|
names << column.human_name
end
end
def row_values(object)
- table.columns.each_with_object([]) do |(key, column), values|
+ columns.each_with_object([]) do |column, values|
values << column.value(object, view_context, table)
end
end
View
2 lib/table_cloth/presenters/default.rb
@@ -15,7 +15,7 @@ def render_rows
def render_row(object)
wrapper_tag :tr do
- v.raw table.columns.inject('') {|tds, (key, column)| tds + render_td(column, object) }
+ v.raw columns.inject('') {|tds, column| tds + render_td(column, object) }
end
end
View
18 spec/factories/columns.rb
@@ -0,0 +1,18 @@
+FactoryGirl.define do
+ factory :column, class: "TableCloth::Column" do
+ name :name
+ options({})
+
+ factory :if_column do
+ name :name
+ options({ if: :admin? })
+ end
+
+ factory :unless_column do
+ name :name
+ options({ unless: :moderator? })
+ end
+
+ initialize_with { new(name, options) }
+ end
+end
View
9 spec/lib/presenter_spec.rb
@@ -15,9 +15,9 @@
expect(subject).to have(3).columns
end
- it "returns hashes of the column detail" do
+ it "returns them as columns" do
subject.columns.each do |column|
- expect(column).to be_kind_of Hash
+ expect(column).to be_kind_of TableCloth::Column
end
end
end
@@ -29,11 +29,6 @@
it 'returns all names' do
subject.column_names.should =~ ["Id", "Name", "Email"]
end
-
- it 'uses a name given to it' do
- dummy_table.column :email, label: 'Email Address'
- subject.column_names.should include 'Email Address'
- end
end
it 'returns all values for a row' do
View
24 spec/lib/presenters/default_spec.rb
@@ -2,16 +2,10 @@
describe TableCloth::Presenters::Default do
let(:dummy_table) { Class.new(DummyTable) }
- let(:dummy_model) do
- DummyModel.new.tap do |d|
- d.id = 1
- d.email = 'robert@example.com'
- d.name = 'robert'
- end
- end
+ let(:dummy_model) { FactoryGirl.build(:dummy_model) }
let(:objects) do
- 3.times.map {|n| dummy_model.dup.tap {|dm| dm.id = n+1 } }
+ 3.times.map {|n| FactoryGirl.build(:dummy_model) }
end
let(:view_context) { ActionView::Base.new }
@@ -59,7 +53,7 @@
let(:objects) do
model = DummyModel.new.tap do |d|
d.id = 1
- d.email = 'robert@creativequeries.com'
+ d.email = 'robert@example.com'
d.name = '<script>alert("Im in your columns, snatching your main thread.")</script>'
end
@@ -143,23 +137,13 @@
doc.at_xpath('//td')[:class].should include 'email_column'
end
- context 'actions column' do
- let(:dummy_table) { Class.new(DummyTableWithActions) }
-
- specify 'actions column has a class set' do
- doc = Nokogiri::HTML(subject.render_table)
- td = doc.at_css('td:last')
- td[:class].should include "actions"
- end
- end
-
context 'by value of row column' do
let(:dummy_table) { Class.new(DummyTableWithValueOptions) }
specify 'column has options because of value' do
doc = Nokogiri::HTML(subject.render_table)
td = doc.at_xpath('//td')
- expect(td.text).to include "robert@creativequeries.com"
+ expect(td.text).to include "robert@example.com"
expect(td[:class]).to eq("special-class")
end
end
View
2 spec/support/dummy_table_with_value_options.rb
@@ -1,5 +1,5 @@
class DummyTableWithValueOptions < TableCloth::Base
column :email do
- ["robert@creativequeries.com", {class: "special-class"}]
+ ["robert@example.com", {class: "special-class"}]
end
end

0 comments on commit 0e130a1

Please sign in to comment.
Something went wrong with that request. Please try again.