Skip to content

Commit

Permalink
Include specs to ensure that columns aren't included when unavailable.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbytables committed Dec 17, 2012
1 parent 0e130a1 commit 6d1d929
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/table_cloth/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ def render_rows

def columns
@columns ||= table.class.columns.map do |name, options|
options[:class].new(name, options[:options])
end
column = options[:class].new(name, options[:options])

if ColumnJury.new(column, table).available?
column
else
nil
end
end.compact
end

def column_names
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/dummy_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FactoryGirl.define do
factory :dummy_table, class: "TableCloth::Base" do
initialize_with do
Class.new(TableCloth::Base).tap do |klass|
attributes.each do |key,attribute|
klass.column *[key, attribute]
end
end
end
end
end
19 changes: 18 additions & 1 deletion spec/lib/presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,31 @@
expect(column).to be_kind_of TableCloth::Column
end
end

context "that are unavaialble" do
let(:dummy_table) { FactoryGirl.build(:dummy_table, email: {if: :admin?}) }
let(:table_instance) { dummy_table.new(objects, view_context) }
before(:each) do
table_instance.stub admin?: false
subject.stub table: table_instance
end

specify "are not returned" do
expect(subject).to have(0).columns
end

specify "name is not returned" do
expect(subject.column_names).not_to include "email"
end
end
end

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
subject.column_names.should =~ ["Id", "Name", "Email"]
subject.column_names.should == ["Id", "Name", "Email"]
end
end

Expand Down

0 comments on commit 6d1d929

Please sign in to comment.