Permalink
Browse files

Add column jury class to do logic for whether or not columns are avai…

…lable.
  • Loading branch information...
1 parent 8c4303a commit b77748e9f01b70542353cc9df6c715984661a849 @bobbytables committed Dec 15, 2012
Showing with 61 additions and 0 deletions.
  1. +1 −0 lib/table_cloth.rb
  2. +25 −0 lib/table_cloth/column_jury.rb
  3. +35 −0 spec/lib/column_jury_spec.rb
View
@@ -8,6 +8,7 @@ module TableCloth
autoload :Configuration, 'table_cloth/configuration'
autoload :Builder, 'table_cloth/builder'
autoload :Column, 'table_cloth/column'
+ autoload :ColumnJury, 'table_cloth/column_jury'
autoload :Presenter, 'table_cloth/presenter'
autoload :ActionViewExtension, 'table_cloth/action_view_extension'
@@ -0,0 +1,25 @@
+module TableCloth
+ class ColumnJury
+ attr_reader :column, :table
+
+ def initialize(column, table)
+ @column, @table = column, table
+ end
+
+ def available?
+ if options[:if] && options[:if].is_a?(Symbol)
+ return !!table.send(options[:if])
+ end
+
+ if options[:unless] && options[:unless].is_a?(Symbol)
+ return !table.send(options[:unless])
+ end
+
+ true
+ end
+
+ def options
+ column.options
+ end
+ end
+end
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe TableCloth::ColumnJury do
+ let(:dummy_table) { stub(:table, admin?: true, moderator?: false) }
+
+ subject { TableCloth::ColumnJury.new(column, dummy_table) }
+
+ context 'conditions' do
+ context 'if' do
+ let(:column) { FactoryGirl.build(:if_column) }
+
+ specify 'the column is available when condition returns true' do
+ expect(subject).to be_available
+ end
+
+ it 'the column is not available when condition returns false' do
+ dummy_table.stub admin?: false
+ expect(subject).not_to be_available
+ end
+ end
+
+ context 'unless' do
+ let(:column) { FactoryGirl.build(:unless_column) }
+
+ specify 'the column is available when condition returns false' do
+ expect(subject).to be_available
+ end
+
+ specify 'the column is not available when condition returns true' do
+ dummy_table.stub moderator?: true
+ expect(subject).not_to be_available
+ end
+ end
+ end
+end

0 comments on commit b77748e

Please sign in to comment.