Skip to content
Browse files

Move column names responsibolity into presenter.

  • Loading branch information...
1 parent 0abcde6 commit f3b837da9b83a254a037e55b9f478aee970ed4a4 @bobbytables committed Dec 15, 2012
Showing with 37 additions and 72 deletions.
  1. +0 −6 lib/table_cloth/base.rb
  2. +6 −4 lib/table_cloth/presenter.rb
  3. +1 −61 spec/lib/base_spec.rb
  4. +29 −0 spec/lib/presenter_spec.rb
  5. +1 −1 spec/support/dummy_table.rb
View
6 lib/table_cloth/base.rb
@@ -11,12 +11,6 @@ def initialize(collection, view)
@view = view
end
- def column_names
- @column_names ||= columns.each_with_object([]) do |(column_name, column), names|
- names << column.human_name
- end
- end
-
def columns
@columns ||= self.class.columns.each_with_object({}) do |(column_name, column), columns|
columns[column_name] = column if column.available?(self)
View
10 lib/table_cloth/presenter.rb
@@ -4,10 +4,10 @@ class Presenter
:table
def initialize(objects, table, view)
- @view_context = view
+ @view_context = view
@table_definition = table
- @objects = objects
- @table = table_definition.new(objects, view)
+ @objects = objects
+ @table = table_definition.new(objects, view)
end
# Short hand so your fingers don't hurt
@@ -28,7 +28,9 @@ def render_rows
end
def column_names
- table.column_names
+ @column_names ||= table.columns.each_with_object([]) do |(column_name, column), names|
+ names << column.human_name
+ end
end
def row_values(object)
View
62 spec/lib/base_spec.rb
@@ -37,40 +37,6 @@
expect(column.options[:proc]).to be_kind_of(Proc)
end
- context ".column_names" do
- before(:each) { table_instance.stub admin?: false, awesome?: true }
-
- it 'returns all names' do
- subject.column :name, :email
- table_instance.column_names.should =~ ['Name', 'Email']
- end
-
- it 'includes actions when given' do
- subject.actions { action { } }
- table_instance.column_names.should include 'Actions'
- end
-
- it 'does not include actions if all action conditions fail' do
- subject.actions do
- action(if: :admin?) { '/' }
- end
- table_instance.column_names.should_not include 'Actions'
- end
-
- it 'include actions when only partial are available' do
- subject.actions do
- action(if: :admin?) { '/' }
- action(if: :awesome?) { '/' }
- end
- table_instance.column_names.should include 'Actions'
- end
-
- it 'uses a name given to it' do
- subject.column :email, label: 'Email Address'
- table_instance.column_names.should include 'Email Address'
- end
- end
-
context "custom" do
let(:custom_column) do
Class.new(TableCloth::Column) do
@@ -87,32 +53,6 @@ def value(object, view)
end
end
- context 'conditions' do
- context 'if' do
- subject { DummyTable.new([dummy_model], view_context) }
-
- it 'includes the id column when admin' do
- subject.column_names.should include 'Id'
- end
-
- it 'exclused the id column when an admin' do
- subject.stub admin?: false
- subject.column_names.should_not include 'Id'
- end
- end
-
- context 'unless' do
- subject { DummyTableUnlessAdmin.new([dummy_model], view_context) }
- before(:each) do
- subject.stub admin?: false
- end
-
- it 'includes the id when not an admin' do
- subject.column_names.should include 'Id'
- end
- end
- end
-
context 'presenters' do
it 'has a presenter method' do
subject.should respond_to :presenter
@@ -126,7 +66,7 @@ def value(object, view)
action { 'Delete' }
end
- subject.columns[:actions].should have(2).actions
+ expect(subject.columns[:actions]).to be_kind_of TableCloth::Columns::Action
end
end
View
29 spec/lib/presenter_spec.rb
@@ -10,6 +10,35 @@
let(:view_context) { ActionView::Base.new }
subject { TableCloth::Presenter.new(objects, dummy_table, view_context) }
+
+ context ".column_names" do
+ let(:table_instance) { dummy_table.new(objects, view_context) }
+ before(:each) { table_instance.stub admin?: false, awesome?: true }
+
+ it 'returns all names' do
+ dummy_table.column :name, :email
+ subject.column_names.should =~ ["Id", "Name", "Email"]
+ end
+
+ it 'includes actions when given' do
+ dummy_table.actions { action { } }
+ subject.column_names.should include 'Actions'
+ end
+
+ it 'include actions when only partial are available' do
+ dummy_table.actions do
+ action(if: :admin?) { '/' }
+ action(if: :awesome?) { '/' }
+ end
+ subject.column_names.should include 'Actions'
+ 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
subject.row_values(dummy_model).should == [dummy_model.id, dummy_model.name, dummy_model.email]
end
View
2 spec/support/dummy_table.rb
@@ -1,5 +1,5 @@
class DummyTable < TableCloth::Base
- column :id, if: :admin?
+ column :id
column :name
column :email

0 comments on commit f3b837d

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