table_cloth 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -1
 - data/lib/table_cloth/base.rb +8 -7
 - data/lib/table_cloth/column.rb +1 -1
 - data/lib/table_cloth/configurable_elements.rb +21 -0
 - data/lib/table_cloth/configuration.rb +1 -13
 - data/lib/table_cloth/presenter.rb +12 -7
 - data/lib/table_cloth/presenters/default.rb +10 -17
 - data/lib/table_cloth/version.rb +1 -1
 - data/lib/table_cloth.rb +1 -0
 - data/spec/lib/column_spec.rb +1 -1
 - data/spec/lib/presenters/default_spec.rb +55 -1
 - metadata +3 -2
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,6 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # Table Cloth
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            Table Cloth gives you an easy to use DSL for creating and rendering tables in rails.
         
     | 
| 
      
 4 
     | 
    
         
            +
            It's new, so if you want a feature or have an improvement? Make an issue!
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Follow me! [@robertoross](http://twitter.com/robertoross)
         
     | 
| 
       4 
7 
     | 
    
         | 
| 
       5 
8 
     | 
    
         
             
            [](https://travis-ci.org/bobbytables/table_cloth)
         
     | 
| 
       6 
9 
     | 
    
         | 
| 
         @@ -142,4 +145,4 @@ config.td.valign = 'top' 
     | 
|
| 
       142 
145 
     | 
    
         
             
            3. CREATE A SPEC.
         
     | 
| 
       143 
146 
     | 
    
         
             
            4. Commit your changes (`git commit -am 'Add some feature'`)
         
     | 
| 
       144 
147 
     | 
    
         
             
            5. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
       145 
     | 
    
         
            -
            6. Create new Pull Request
         
     | 
| 
      
 148 
     | 
    
         
            +
            6. Create new Pull Request
         
     | 
    
        data/lib/table_cloth/base.rb
    CHANGED
    
    | 
         @@ -8,17 +8,14 @@ module TableCloth 
     | 
|
| 
       8 
8 
     | 
    
         
             
                end
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                def column_names
         
     | 
| 
       11 
     | 
    
         
            -
                  columns. 
     | 
| 
       12 
     | 
    
         
            -
                    names << column.human_name 
     | 
| 
      
 11 
     | 
    
         
            +
                  @column_names ||= columns.each_with_object([]) do |(column_name, column), names|
         
     | 
| 
      
 12 
     | 
    
         
            +
                    names << column.human_name
         
     | 
| 
       13 
13 
     | 
    
         
             
                  end
         
     | 
| 
       14 
14 
     | 
    
         
             
                end
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
                def columns
         
     | 
| 
       17 
     | 
    
         
            -
                  self.class.columns. 
     | 
| 
       18 
     | 
    
         
            -
                    if column.available?(self)
         
     | 
| 
       19 
     | 
    
         
            -
                      columns[column_name] = column
         
     | 
| 
       20 
     | 
    
         
            -
                    end
         
     | 
| 
       21 
     | 
    
         
            -
                    columns
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @columns ||=self.class.columns.each_with_object({}) do |(column_name, column), columns|
         
     | 
| 
      
 18 
     | 
    
         
            +
                    columns[column_name] = column if column.available?(self)
         
     | 
| 
       22 
19 
     | 
    
         
             
                  end
         
     | 
| 
       23 
20 
     | 
    
         
             
                end
         
     | 
| 
       24 
21 
     | 
    
         | 
| 
         @@ -27,6 +24,10 @@ module TableCloth 
     | 
|
| 
       27 
24 
     | 
    
         
             
                end
         
     | 
| 
       28 
25 
     | 
    
         | 
| 
       29 
26 
     | 
    
         
             
                class << self
         
     | 
| 
      
 27 
     | 
    
         
            +
                  def config
         
     | 
| 
      
 28 
     | 
    
         
            +
                    @config ||= Class.new { include ConfigurableElements }
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
       30 
31 
     | 
    
         
             
                  def presenter(klass=nil)
         
     | 
| 
       31 
32 
     | 
    
         
             
                    if klass
         
     | 
| 
       32 
33 
     | 
    
         
             
                      @presenter = klass
         
     | 
    
        data/lib/table_cloth/column.rb
    CHANGED
    
    
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module TableCloth
         
     | 
| 
      
 2 
     | 
    
         
            +
              module ConfigurableElements
         
     | 
| 
      
 3 
     | 
    
         
            +
                extend ActiveSupport::Concern
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                included do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  cattr_accessor :table
         
     | 
| 
      
 7 
     | 
    
         
            +
                  cattr_accessor :thead
         
     | 
| 
      
 8 
     | 
    
         
            +
                  cattr_accessor :th
         
     | 
| 
      
 9 
     | 
    
         
            +
                  cattr_accessor :tbody
         
     | 
| 
      
 10 
     | 
    
         
            +
                  cattr_accessor :tr
         
     | 
| 
      
 11 
     | 
    
         
            +
                  cattr_accessor :td
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  self.table = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 14 
     | 
    
         
            +
                  self.thead = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 15 
     | 
    
         
            +
                  self.th    = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 16 
     | 
    
         
            +
                  self.tbody = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 17 
     | 
    
         
            +
                  self.tr    = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 18 
     | 
    
         
            +
                  self.td    = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,18 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            module TableCloth
         
     | 
| 
       2 
2 
     | 
    
         
             
              class Configuration
         
     | 
| 
       3 
     | 
    
         
            -
                 
     | 
| 
       4 
     | 
    
         
            -
                cattr_accessor :thead
         
     | 
| 
       5 
     | 
    
         
            -
                cattr_accessor :th
         
     | 
| 
       6 
     | 
    
         
            -
                cattr_accessor :tbody
         
     | 
| 
       7 
     | 
    
         
            -
                cattr_accessor :tr
         
     | 
| 
       8 
     | 
    
         
            -
                cattr_accessor :td
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
                self.table = ActiveSupport::OrderedOptions.new
         
     | 
| 
       11 
     | 
    
         
            -
                self.thead = ActiveSupport::OrderedOptions.new
         
     | 
| 
       12 
     | 
    
         
            -
                self.th    = ActiveSupport::OrderedOptions.new
         
     | 
| 
       13 
     | 
    
         
            -
                self.tbody = ActiveSupport::OrderedOptions.new
         
     | 
| 
       14 
     | 
    
         
            -
                self.tr    = ActiveSupport::OrderedOptions.new
         
     | 
| 
       15 
     | 
    
         
            -
                self.td    = ActiveSupport::OrderedOptions.new
         
     | 
| 
      
 3 
     | 
    
         
            +
                include ConfigurableElements
         
     | 
| 
       16 
4 
     | 
    
         | 
| 
       17 
5 
     | 
    
         
             
                class << self
         
     | 
| 
       18 
6 
     | 
    
         
             
                  def configure(&block)
         
     | 
| 
         @@ -32,22 +32,27 @@ module TableCloth 
     | 
|
| 
       32 
32 
     | 
    
         
             
                end
         
     | 
| 
       33 
33 
     | 
    
         | 
| 
       34 
34 
     | 
    
         
             
                def row_values(object)
         
     | 
| 
       35 
     | 
    
         
            -
                   
     | 
| 
       36 
     | 
    
         
            -
                    values << column.value(object, view_context, table) 
     | 
| 
      
 35 
     | 
    
         
            +
                  table.columns.each_with_object([]) do |(key, column), values|
         
     | 
| 
      
 36 
     | 
    
         
            +
                    values << column.value(object, view_context, table)
         
     | 
| 
       37 
37 
     | 
    
         
             
                  end
         
     | 
| 
       38 
38 
     | 
    
         
             
                end
         
     | 
| 
       39 
39 
     | 
    
         | 
| 
       40 
40 
     | 
    
         
             
                def rows
         
     | 
| 
       41 
     | 
    
         
            -
                  objects. 
     | 
| 
       42 
     | 
    
         
            -
                    row << row_values(object) 
     | 
| 
      
 41 
     | 
    
         
            +
                  objects.each_with_object([]) do |object, row|
         
     | 
| 
      
 42 
     | 
    
         
            +
                    row << row_values(object)
         
     | 
| 
       43 
43 
     | 
    
         
             
                  end
         
     | 
| 
       44 
44 
     | 
    
         
             
                end
         
     | 
| 
       45 
45 
     | 
    
         | 
| 
       46 
     | 
    
         
            -
                def wrapper_tag(type, value=nil, &block)
         
     | 
| 
      
 46 
     | 
    
         
            +
                def wrapper_tag(type, value=nil, options={}, &block)
         
     | 
| 
      
 47 
     | 
    
         
            +
                  table_config = table.class.config.send(type).to_hash
         
     | 
| 
      
 48 
     | 
    
         
            +
                  tag_options = TableCloth.config_for(type)
         
     | 
| 
      
 49 
     | 
    
         
            +
                  tag_options.merge!(table_config)
         
     | 
| 
      
 50 
     | 
    
         
            +
                  tag_options.merge!(options)
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
       47 
52 
     | 
    
         
             
                  content = if block_given?
         
     | 
| 
       48 
     | 
    
         
            -
                    v.content_tag(type,  
     | 
| 
      
 53 
     | 
    
         
            +
                    v.content_tag(type, tag_options, &block)
         
     | 
| 
       49 
54 
     | 
    
         
             
                  else
         
     | 
| 
       50 
     | 
    
         
            -
                    v.content_tag(type, value,  
     | 
| 
      
 55 
     | 
    
         
            +
                    v.content_tag(type, value, tag_options)
         
     | 
| 
       51 
56 
     | 
    
         
             
                  end
         
     | 
| 
       52 
57 
     | 
    
         
             
                end
         
     | 
| 
       53 
58 
     | 
    
         
             
              end
         
     | 
| 
         @@ -9,32 +9,25 @@ module TableCloth 
     | 
|
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                  def render_rows
         
     | 
| 
       11 
11 
     | 
    
         
             
                    wrapper_tag :tbody do
         
     | 
| 
       12 
     | 
    
         
            -
                       
     | 
| 
       13 
     | 
    
         
            -
                        r + render_row(values)
         
     | 
| 
       14 
     | 
    
         
            -
                      end
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
                      v.raw(body)
         
     | 
| 
      
 12 
     | 
    
         
            +
                      v.raw objects.inject('') {|r, object| r + render_row(object) }
         
     | 
| 
       17 
13 
     | 
    
         
             
                    end
         
     | 
| 
       18 
14 
     | 
    
         
             
                  end
         
     | 
| 
       19 
15 
     | 
    
         | 
| 
       20 
     | 
    
         
            -
                  def render_row( 
     | 
| 
      
 16 
     | 
    
         
            +
                  def render_row(object)
         
     | 
| 
       21 
17 
     | 
    
         
             
                    wrapper_tag :tr do
         
     | 
| 
       22 
     | 
    
         
            -
                       
     | 
| 
       23 
     | 
    
         
            -
                        tds + wrapper_tag(:td, value)
         
     | 
| 
       24 
     | 
    
         
            -
                      end
         
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
                      v.raw(row)
         
     | 
| 
      
 18 
     | 
    
         
            +
                      v.raw table.columns.inject('') {|tds, (key, column)| tds + render_td(column, object) }
         
     | 
| 
       27 
19 
     | 
    
         
             
                    end
         
     | 
| 
       28 
20 
     | 
    
         
             
                  end
         
     | 
| 
       29 
21 
     | 
    
         | 
| 
      
 22 
     | 
    
         
            +
                  def render_td(column, object)
         
     | 
| 
      
 23 
     | 
    
         
            +
                    td_options = column.options.delete(:td_options) || {}
         
     | 
| 
      
 24 
     | 
    
         
            +
                    wrapper_tag(:td, column.value(object, view_context, table), td_options)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
       30 
27 
     | 
    
         
             
                  def render_header
         
     | 
| 
       31 
28 
     | 
    
         
             
                    wrapper_tag :thead do
         
     | 
| 
       32 
     | 
    
         
            -
                      wrapper_tag 
     | 
| 
       33 
     | 
    
         
            -
                         
     | 
| 
       34 
     | 
    
         
            -
                          tags + wrapper_tag(:th, name)
         
     | 
| 
       35 
     | 
    
         
            -
                        end
         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
                        v.raw(names)
         
     | 
| 
      
 29 
     | 
    
         
            +
                      wrapper_tag :tr do
         
     | 
| 
      
 30 
     | 
    
         
            +
                        v.raw column_names.inject('') {|tags, name| tags + wrapper_tag(:th, name) }
         
     | 
| 
       38 
31 
     | 
    
         
             
                      end
         
     | 
| 
       39 
32 
     | 
    
         
             
                    end
         
     | 
| 
       40 
33 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/table_cloth/version.rb
    CHANGED
    
    
    
        data/lib/table_cloth.rb
    CHANGED
    
    
    
        data/spec/lib/column_spec.rb
    CHANGED
    
    
| 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'spec_helper'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            describe TableCloth::Presenters::Default do
         
     | 
| 
       4 
     | 
    
         
            -
              let(:dummy_table) { DummyTable }
         
     | 
| 
      
 4 
     | 
    
         
            +
              let(:dummy_table) { Class.new(DummyTable) }
         
     | 
| 
       5 
5 
     | 
    
         
             
              let(:dummy_model) do
         
     | 
| 
       6 
6 
     | 
    
         
             
                DummyModel.new.tap do |d|
         
     | 
| 
       7 
7 
     | 
    
         
             
                  d.id    = 1
         
     | 
| 
         @@ -113,5 +113,59 @@ describe TableCloth::Presenters::Default do 
     | 
|
| 
       113 
113 
     | 
    
         
             
                it 'td has a class attached' do
         
     | 
| 
       114 
114 
     | 
    
         
             
                  doc.at_xpath('//td')[:class].should include 'td'
         
     | 
| 
       115 
115 
     | 
    
         
             
                end
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
              end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
              context 'specific configuration' do
         
     | 
| 
      
 120 
     | 
    
         
            +
                let(:doc) { Nokogiri::HTML(subject.render_table) }
         
     | 
| 
      
 121 
     | 
    
         
            +
                let(:dummy_table) do
         
     | 
| 
      
 122 
     | 
    
         
            +
                  Class.new(TableCloth::Base) do
         
     | 
| 
      
 123 
     | 
    
         
            +
                    column :email, td_options: { class: 'email_column' }
         
     | 
| 
      
 124 
     | 
    
         
            +
                  end
         
     | 
| 
      
 125 
     | 
    
         
            +
                end
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                it 'td has a class set' do
         
     | 
| 
      
 128 
     | 
    
         
            +
                  doc.at_xpath('//td')[:class].should include 'email_column'
         
     | 
| 
      
 129 
     | 
    
         
            +
                end
         
     | 
| 
      
 130 
     | 
    
         
            +
              end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
              context 'table configuration' do
         
     | 
| 
      
 133 
     | 
    
         
            +
                let(:doc) { Nokogiri::HTML(subject.render_table) }
         
     | 
| 
      
 134 
     | 
    
         
            +
                let(:dummy_table) do
         
     | 
| 
      
 135 
     | 
    
         
            +
                  Class.new(TableCloth::Base) do
         
     | 
| 
      
 136 
     | 
    
         
            +
                    column :email
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
                    config.table.class = 'table2'
         
     | 
| 
      
 139 
     | 
    
         
            +
                    config.thead.class = 'thead2'
         
     | 
| 
      
 140 
     | 
    
         
            +
                    config.th.class    = 'th2'
         
     | 
| 
      
 141 
     | 
    
         
            +
                    config.tbody.class = 'tbody2'
         
     | 
| 
      
 142 
     | 
    
         
            +
                    config.tr.class    = 'tr2'
         
     | 
| 
      
 143 
     | 
    
         
            +
                    config.td.class    = 'td2'
         
     | 
| 
      
 144 
     | 
    
         
            +
                  end
         
     | 
| 
      
 145 
     | 
    
         
            +
                end
         
     | 
| 
      
 146 
     | 
    
         
            +
             
     | 
| 
      
 147 
     | 
    
         
            +
                it 'tables have a class attached' do
         
     | 
| 
      
 148 
     | 
    
         
            +
                  doc.at_xpath('//table')[:class].should include 'table2'
         
     | 
| 
      
 149 
     | 
    
         
            +
                end
         
     | 
| 
      
 150 
     | 
    
         
            +
             
     | 
| 
      
 151 
     | 
    
         
            +
                it 'thead has a class attached' do
         
     | 
| 
      
 152 
     | 
    
         
            +
                  doc.at_xpath('//thead')[:class].should include 'thead2'
         
     | 
| 
      
 153 
     | 
    
         
            +
                end
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
                it 'th has a class attached' do
         
     | 
| 
      
 156 
     | 
    
         
            +
                  doc.at_xpath('//th')[:class].should include 'th2'
         
     | 
| 
      
 157 
     | 
    
         
            +
                end
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
                it 'tbody has a class attached' do
         
     | 
| 
      
 160 
     | 
    
         
            +
                  doc.at_xpath('//tbody')[:class].should include 'tbody2'
         
     | 
| 
      
 161 
     | 
    
         
            +
                end
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                it 'tr has a class attached' do
         
     | 
| 
      
 164 
     | 
    
         
            +
                  doc.at_xpath('//tr')[:class].should include 'tr2'
         
     | 
| 
      
 165 
     | 
    
         
            +
                end
         
     | 
| 
      
 166 
     | 
    
         
            +
             
     | 
| 
      
 167 
     | 
    
         
            +
                it 'td has a class attached' do
         
     | 
| 
      
 168 
     | 
    
         
            +
                  doc.at_xpath('//td')[:class].should include 'td2'
         
     | 
| 
      
 169 
     | 
    
         
            +
                end
         
     | 
| 
       116 
170 
     | 
    
         
             
              end
         
     | 
| 
       117 
171 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: table_cloth
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.2
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2012-10- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-10-11 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rspec
         
     | 
| 
         @@ -147,6 +147,7 @@ files: 
     | 
|
| 
       147 
147 
     | 
    
         
             
            - lib/table_cloth/builder.rb
         
     | 
| 
       148 
148 
     | 
    
         
             
            - lib/table_cloth/column.rb
         
     | 
| 
       149 
149 
     | 
    
         
             
            - lib/table_cloth/columns/action.rb
         
     | 
| 
      
 150 
     | 
    
         
            +
            - lib/table_cloth/configurable_elements.rb
         
     | 
| 
       150 
151 
     | 
    
         
             
            - lib/table_cloth/configuration.rb
         
     | 
| 
       151 
152 
     | 
    
         
             
            - lib/table_cloth/presenter.rb
         
     | 
| 
       152 
153 
     | 
    
         
             
            - lib/table_cloth/presenters/default.rb
         
     |