Skip to content

Commit

Permalink
Neatly add row_attributes to DefaultTable.
Browse files Browse the repository at this point in the history
Move most logic into ::Base via include ::Extensions::RowAttributes.
Update Presenters::Default to add attributes to trs.
Add simple 'feature' spec in Presenters::Default spec.
  • Loading branch information
siassaj committed Jun 30, 2014
1 parent 73ead6a commit 81a1d66
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
3 changes: 2 additions & 1 deletion lib/table_cloth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Presenters

module Extensions
autoload :Actions, "table_cloth/extensions/actions"
autoload :RowAttributes, "table_cloth/extensions/row_attributes"
end

class << self
Expand All @@ -33,4 +34,4 @@ def config

ActiveSupport.on_load(:action_view) do
include TableCloth::ActionViewExtension
end
end
16 changes: 1 addition & 15 deletions lib/table_cloth/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TableCloth
class Base
include TableCloth::Extensions::Actions
include TableCloth::Extensions::RowAttributes

NoPresenterError = Class.new(Exception)

Expand Down Expand Up @@ -59,21 +60,6 @@ def config
Configuration.new
end
end

def row_attributes(*args, &block)
@tr_options ||= {}
options = args.extract_options! || {}
options[:proc] = block if block_given?
@tr_options = options
end

def tr_options
@tr_options ||= {}
if superclass.respond_to? :tr_options
@tr_options = superclass.tr_options.merge(@tr_options)
end
@tr_options
end
end
end
end
36 changes: 36 additions & 0 deletions lib/table_cloth/extensions/row_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module TableCloth
module Extensions
module RowAttributes

extend ActiveSupport::Concern

module ClassMethods

def row_attributes(*args, &block)
@tr_options ||= {}
options = args.extract_options! || {}
options[:proc] = block if block_given?
@tr_options = options
end

def tr_options
@tr_options ||= {}
if superclass.respond_to? :tr_options
@tr_options = superclass.tr_options.merge(@tr_options)
end
@tr_options
end

def tr_options_for(object)
options = tr_options
if options.include?(:proc)
result = options[:proc].call(object) || {}
options.except(:proc).merge(result)
else
options
end
end
end
end
end
end
13 changes: 3 additions & 10 deletions lib/table_cloth/presenters/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def thead_row
end

def row_for_object(object)
ElementFactory::Element.new(:tr, tag_options(:tr).merge(tr_options(object))).tap do |row|
tr_options = table.class.tr_options_for(object)

ElementFactory::Element.new(:tr, tag_options(:tr).merge(tr_options)).tap do |row|
columns.each do |column|
row << column_for_object(column, object)
end
Expand All @@ -59,15 +61,6 @@ def column_for_object(column, object)

ElementFactory::Element.new(:td, tag_options(:td).merge(td_options))
end

def tr_options(object)
options = table.class.tr_options.clone
if (block = options.delete(:proc))
results = block.call(object) || {}
options.merge!(results)
end
options
end
end
end
end
8 changes: 7 additions & 1 deletion spec/lib/presenters/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
expect(tbody.css('tr').size).to be 3
end

it "creates a row with attributes from row_attributes" do
tbody = Nokogiri::HTML(subject.tbody.to_s)
expect(tbody.css('tr:first-child').first.attribute("class").value).to eq("quazimodo-is-awesome")
expect(tbody.css('tr:first-child').first.attribute("name").value).to eq("What a handsome quazimodo")
end

context 'escaped values' do
let(:objects) do
FactoryGirl.build_list(:dummy_model, 1,
Expand Down Expand Up @@ -82,4 +88,4 @@
end
end
end
end
end
7 changes: 5 additions & 2 deletions spec/support/dummy_table.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class DummyTable < TableCloth::Base
row_attributes name: "What a handsome quazimodo" do |obj|
{class: "quazimodo-is-awesome"} if obj.present?
end
column :id, th_options: {class: "th_options_class"}
column :name
column :email
Expand All @@ -7,5 +10,5 @@ class DummyTable < TableCloth::Base
class DummyTableUnlessAdmin < TableCloth::Base
column :id, unless: :admin?
column :name
column :email
end
column :email
end

0 comments on commit 81a1d66

Please sign in to comment.