diff --git a/lib/data-table/table.rb b/lib/data-table/table.rb
index 9da2104..f4a8a71 100644
--- a/lib/data-table/table.rb
+++ b/lib/data-table/table.rb
@@ -23,7 +23,8 @@ class Table
attr_reader :collection, :grouped_data, :subtotals, :totals,
:subtotal_calculations, :total_calculations, :columns
- attr_accessor :id, :title, :css_class, :empty_text,
+ attr_accessor :id, :css_class, :empty_text,
+ :title, :title_class, :subtitle, :subtitle_class, :title_separator_class,
:alternate_rows, :alternate_cols, :display_header, :hide_if_empty,
:repeat_headers_for_groups, :custom_headers
@@ -41,7 +42,10 @@ def initialize(collection)
def default_options!
@id = ''
@title = ''
- @subtitle = ''
+ @title_class = nil
+ @subtitle = nil
+ @subtitle_class = nil
+ @title_separator_class = nil
@css_class = ''
@empty_text = 'No records found'
@hide_if_empty = false
@@ -76,7 +80,14 @@ def render
def render_data_table
html = "
"
- html << "#{@title}" if @title
+ html << ''
+ if @title || @subtitle
+ html << "#{@title}" if @title
+ html << "" if @title && @subtitle
+ html << "#{@subtitle}" if @subtitle
+ end
+ html << ''
+
html << render_data_table_header if @display_header
if @collection.any?
html << render_data_table_body(@collection)
@@ -254,7 +265,7 @@ def render_totals
html = ''
@total_calculations.each_with_index do |totals_row, index|
next if totals_row.nil?
-
+
html << ""
@columns.each do |col|
value = totals_row[col.name] ||= nil
@@ -291,7 +302,7 @@ def render_subtotals(group_header, _group_data = nil, ancestors = nil)
@subtotal_calculations[path].each_with_index do |group, index|
next if group.empty?
-
+
html << "
"
@columns.each do |col|
value = group[col.name] ? group[col.name].values[0] : nil
diff --git a/lib/data-table/version.rb b/lib/data-table/version.rb
index 70deb9d..2027c3e 100644
--- a/lib/data-table/version.rb
+++ b/lib/data-table/version.rb
@@ -1,3 +1,3 @@
module DataTable
- VERSION = '2.0.3'.freeze
+ VERSION = '2.0.4'.freeze
end
diff --git a/spec/data_table_spec.rb b/spec/data_table_spec.rb
index 1392601..ef0301b 100644
--- a/spec/data_table_spec.rb
+++ b/spec/data_table_spec.rb
@@ -17,6 +17,6 @@
t.column :class, 'Class'
end
- expect(html).to eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
+ expect(html).to eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
end
end
diff --git a/spec/table_spec.rb b/spec/table_spec.rb
index 96284c8..d515a5f 100644
--- a/spec/table_spec.rb
+++ b/spec/table_spec.rb
@@ -23,7 +23,7 @@
data_table.column(:name, 'Name')
data_table.column(:class, 'Class')
expect(data_table.render).to \
- eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
+ eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
end
it "should group the records" do
@@ -35,7 +35,7 @@
expect(data_table.grouped_data).to be true
data_table.prepare_data
expect(data_table.collection).to eq(collection.group_by {|g| g[grouping_column]})
- expect(data_table.render).to eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
+ expect(data_table.render).to eq(%{| Name | Class |
|---|
| Luke Skywalker | Jedi Knight |
| Emporer Palpatine | Sith Lord |
| Mithrander | Wizard |
| Aragorn | Ranger |
})
end
it "should do totaling" do
@@ -111,7 +111,7 @@
# note how the rows are index_1, and there is no index_0 row
expect(data_table.render).to \
- eq(%{ |
|---|
| 50 |
| 95 |
| Star Wars |
| 9001 |
| 80 |
| Middle Earth |
})
+ eq(%{ |
|---|
| 50 |
| 95 |
| Star Wars |
| 9001 |
| 80 |
| Middle Earth |
})
end
it "should render a custom header" do
@@ -129,14 +129,14 @@
it "should render a table with the 'no records' message" do
expect(data_table.render).to \
- eq(%{})
+ eq(%{})
end
it "should render a custom empty text notice" do
text = "Nothing to see here"
data_table.empty_text = text
expect(data_table.render).to \
- eq(%{})
+ eq(%{})
end
it "avoids dividing by zero" do
@@ -274,8 +274,61 @@
t.column :_empty_space, ''
end
- expected_html = %(| Points | |
|---|
| 50 | |
| 51 | |
| 50.5 | |
| 52 | |
| 53 | |
| 54 | |
| 53.0 | |
| 55 | |
| 55.0 | |
| 52.5 | |
)
+ expected_html = %(| Points | |
|---|
| 50 | |
| 51 | |
| 50.5 | |
| 52 | |
| 53 | |
| 54 | |
| 53.0 | |
| 55 | |
| 55.0 | |
| 52.5 | |
)
expect(html).to eq(expected_html)
end
end
+
+ describe 'title rendering' do
+ let(:table) { DataTable::Table.new([]) }
+ let(:html) { table.render }
+ before(:each) do
+ table.title = 'My Table Title'
+ end
+ before(:each, include_subtitle: true) do
+ table.subtitle = 'My Subtitle'
+ end
+
+ context 'with a title and no subtitle' do
+ it 'renders the title' do
+ expect(html).to include("My Table Title")
+ end
+
+ it 'includes the css class for the title' do
+ table.title_class = 'custom_title_class'
+ expect(html).to include("My Table Title")
+ end
+ end
+
+ context 'with a subtitle', :include_subtitle do
+ before(:each) do
+ table.subtitle = 'My Subtitle'
+ end
+
+ it 'renders the subtitle' do
+ expect(html).to include("")
+ expect(html).to include("")
+ expect(html).to include("My Subtitle")
+ end
+
+ it 'includes the css class for the subtitle' do
+ table.subtitle_class = 'custom_subtitle_class'
+ expect(html).to include("My Subtitle")
+ end
+ end
+
+ context 'with css classes', :include_subtitle do
+ before(:each) do
+ table.title_class = 'custom_title_class'
+ table.title_separator_class = 'custom_separator_class'
+ table.subtitle_class = 'custom_subtitle_class'
+ end
+
+ it 'renders the subtitle' do
+ expect(html).to include("My Table Title")
+ expect(html).to include("")
+ expect(html).to include("My Subtitle")
+ end
+ end
+ end
end