Permalink
Browse files

Merge master & Fix conflicts.

  • Loading branch information...
2 parents 06713df + db0b655 commit 50630cdfc95c355492d057cb7d04c62a6f0519c0 @bobbytables committed Dec 24, 2012
View
@@ -1,6 +1,7 @@
*.gem
*.rbc
.bundle
+coverage
.config
.yardoc
Gemfile.lock
View
@@ -8,19 +8,14 @@ module TableCloth
autoload :Configuration, 'table_cloth/configuration'
autoload :Builder, 'table_cloth/builder'
autoload :Column, 'table_cloth/column'
- autoload :Action, 'table_cloth/action'
- autoload :Actions, 'table_cloth/actions'
+ autoload :ColumnJury, 'table_cloth/column_jury'
autoload :Presenter, 'table_cloth/presenter'
autoload :ActionViewExtension, 'table_cloth/action_view_extension'
module Presenters
autoload :Default, 'table_cloth/presenters/default'
end
- module Columns
- autoload :Action, 'table_cloth/columns/action'
- end
-
def config_for(type)
Configuration.config_for(type).dup
end
View
@@ -1,23 +0,0 @@
-module TableCloth
- class Actions
- attr_reader :column, :options
-
- def initialize(options={}, &block)
- @options = options
- @column = Columns::Action.new(:actions, options)
-
- block.arity > 0 ? block.call(self) : instance_eval(&block)
- end
-
- def action(*args, &block)
- options = args.extract_options! || {}
- options[:proc] = block if block_given?
-
- column.actions << Action.new(options)
- end
-
- def all
- column.actions
- end
- end
-end
View
@@ -11,16 +11,6 @@ def initialize(collection, view)
@view = view
end
- def columns
- @columns ||= self.class.columns.each_with_object({}) do |(column_name, column), columns|
- columns[column_name] = column if column.available?(self)
- end
- end
-
- def has_actions?
- self.class.has_actions?
- end
-
class << self
def presenter(klass=nil)
return @presenter = klass if klass
@@ -40,7 +30,7 @@ def column(*args, &block)
column_class = options.delete(:using) || Column
args.each do |name|
- add_column name, column_class.new(name, options)
+ add_column(class: column_class, options: options, name: name)
end
end
@@ -53,22 +43,9 @@ def columns
@columns
end
- def add_column(name, column)
+ def add_column(options)
@columns ||= {}
- @columns[name] = column
- end
-
- def actions(options={}, &block)
- if block_given?
- actions = Actions.new(options, &block)
- columns[:actions] = actions.column
- end
-
- columns[:actions].actions
- end
-
- def has_actions?
- columns[:actions].present?
+ @columns[options[:name]] = options
end
end
end
View
@@ -18,17 +18,5 @@ def value(object, view, table=nil)
def human_name
options[:label] || name.to_s.humanize
end
-
- def available?(table)
- 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
end
end
@@ -1,12 +1,12 @@
module TableCloth
- class Action
- attr_reader :action, :options
-
- def initialize(options)
- @options = options
+ class ColumnJury
+ attr_reader :column, :table
+
+ def initialize(column, table)
+ @column, @table = column, table
end
- def available?(table)
+ def available?
if options[:if] && options[:if].is_a?(Symbol)
return !!table.send(options[:if])
end
@@ -17,5 +17,9 @@ def available?(table)
true
end
+
+ def options
+ column.options
+ end
end
end
@@ -1,24 +0,0 @@
-module TableCloth
- module Columns
- class Action < Column
- def value(object, view_context, table)
- actions_html = actions.each_with_object('') do |action, links|
- if action.available?(table)
- links << "\n"
- links << (view_context.instance_exec(object, view_context, &action.options[:proc]) || "")
- end
- end
-
- view_context.raw(actions_html)
- end
-
- def actions
- @actions ||= []
- end
-
- def available?(table)
- actions.any? {|a| a.available?(table) }
- end
- end
- end
-end
@@ -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,26 @@ def render_rows
raise NoMethodError, "You must override the .rows method"
end
+ def columns
+ @columns ||= table.class.columns.map do |name, options|
+ column = options[:class].new(name, options[:options])
+
+ if ColumnJury.new(column, table).available?
+ column
+ else
+ nil
+ end
+ end.compact
+ 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
@@ -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
@@ -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
@@ -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
View
@@ -1,28 +0,0 @@
-require "spec_helper"
-
-describe TableCloth::Actions do
- subject { TableCloth::Actions }
-
- context "block syntax" do
- it "accepts a block" do
- actions = subject.new do
- action { 'Edit' }
- end
-
- expect(actions.column).to have(1).actions
- end
-
- it "accepts options with a block" do
- actions = subject.new(class: "actions") do
- action { "Edit" }
- end
-
- expect(actions.options[:class]).to eq("actions")
- end
-
- it ".all returns all actions for the column" do
- actions = subject.new { action { "Edit" } }
- expect(actions.all.length).to be 1
- end
- end
-end
View
@@ -16,7 +16,8 @@
end
it 'column accepts options' do
- expect { subject.column :n, {option: 'value'} }.not_to raise_error
+ subject.column :name, option: "value"
+ expect(subject.columns[:name][:options][:option]).to eq("value")
end
it '.columns returns all columns' do
@@ -33,22 +34,16 @@
subject.column(:name) { 'Wee' }
column = subject.columns[:name]
- expect(column.options[:proc]).to be_present
- expect(column.options[:proc]).to be_kind_of(Proc)
+ expect(column[:options][:proc]).to be_present
+ expect(column[:options][:proc]).to be_kind_of(Proc)
end
context "custom" do
- let(:custom_column) do
- Class.new(TableCloth::Column) do
- def value(object, view)
- "AN EMAIL!"
- end
- end
- end
+ let(:custom_column) { stub(:custom, value: "AN EMAIL") }
it '.column can take a custom column' do
subject.column :email, using: custom_column
- subject.columns[:email].value(dummy_model, view_context).should == "AN EMAIL!"
+ expect(subject.columns[:email][:class]).to eq(custom_column)
end
end
end
@@ -59,17 +54,6 @@ def value(object, view)
end
end
- context 'actions' do
- it "takes a block" do
- subject.actions do
- action { 'Edit' }
- action { 'Delete' }
- end
-
- expect(subject.columns[:actions]).to be_kind_of TableCloth::Columns::Action
- end
- end
-
context "configuration" do
subject { Class.new(TableCloth::Base) }
let(:sibling1_class) { Class.new(subject) }
@@ -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
Oops, something went wrong.

0 comments on commit 50630cd

Please sign in to comment.