table-for 0.0.17 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,57 +7,65 @@ Table-for is a table builder for a collection of domain objects. It very easily
7
7
  In <b>Rails 3</b>, add this to your Gemfile.
8
8
 
9
9
  gem "table-for"
10
-
10
+
11
+ In this example, I'm going to assume the following setup:
12
+ From the console, I have generated a user model with email, first_name, and last_name fields, and generated a scaffold on that model
13
+ rails g scaffold user email:string first_name:string last_name:string --no-stylesheets
14
+
15
+ In seeds.rb:
16
+ User.create! :email => "andrew.hunter@livingsocial.com", :first_name => "Andrew", :last_name => "Hunter"
17
+ User.create! :email => "todd.fisher@livingsocial.com", :first_name => "Todd", :last_name => "Fisher"
18
+ User.create! :email => "jon.phillips@livingsocial.com", :first_name => "Jon", :last_name => "Phillips"
19
+
20
+ In users_controller.rb (index action)
21
+ @users = User.all
22
+
11
23
  == Sample Usage
24
+ <%= table_for @users, :table_html => {:style => "border: 1px solid black"},
25
+ :sortable => true,
26
+ :row_html => {:class => lambda { cycle('even', 'odd')},
27
+ :id => lambda {|user| "user-#{user.id}"}} do |table| %>
28
+ <%= table.column :edit %>
29
+ <%= table.column :show %>
30
+ <%= table.column :email, :label => "Email Address" %>
31
+ <%= table.column :label => "Full Name", :sortable => false, :header_html => {:style => "color:orange"} do |user| %>
32
+ <%= "#{user.first_name} #{user.last_name}" %>
33
+ <% end %>
34
+ <%= table.column :delete %>
35
+ <% end %>
36
+
37
+ == Specifying the columns to use
38
+ <%= table_for @users do |table| %>
39
+ <% table.column :email %>
40
+ <% table.column :first_name %>
41
+ <% table.column :last_name %>
42
+ <% end %>
12
43
 
13
- Say we have fetched a collection of users from the database (stored as @users), in the view we can generate a very unique table with just a few lines of code:
14
-
15
- <%= table_for @users,
16
- :table_html => {:id => "users"},
17
- :header_html => {:style => "color:red"},
18
- :row_html => {:class => lambda { |parameters| cycle('odd', 'even')}},
19
- :column_html => {:style => "color:green"} do |table| %>
20
- <%= table.column :name, :column_html => {:style => "color:blue"}, :header_html => {:style => "color:orange"} %>
21
- <%= table.column :email %>
22
- <%= table.column :phonenumber, :column_html => {:style => "color:orange"}, :header_html => {:style => "color:blue"} do |user| %>
23
- <%= phonenumber(user.phonenumber) %>
44
+ Here, each column will send its respective column name to the user object: user.send(:email), user.send(:first_name), user.send(:last_name),
45
+ to determine what data to output in each column
46
+
47
+ == Specifying the label to use for a column
48
+ <%= table_for @users do |table| %>
49
+ <% table.column :email, :label => "Email Address" %>
50
+ <% end %>
51
+
52
+ == Overridding the default way or rendering a column
53
+ <%= table_for @users do |table| %>
54
+ <% table.column :name do |user| %>
55
+ <%= "#{user.first_name} #{user.last_name}" %>
24
56
  <% end %>
25
- <%= table.column :label => "???" do |user| %>
26
- Some Random Column
57
+ <% end %>
58
+
59
+ Here, since the name of the column is not used anymore to determine what data to output, this code could be changed to:
60
+ <%= table_for @users do |table| %>
61
+ <% table.column :full_name do |user| %>
62
+ <%= "#{user.first_name} #{user.last_name}" %>
27
63
  <% end %>
28
64
  <% end %>
29
-
30
- Will generate the following table:
31
- <table id="users">
32
- <thead>
33
- <tr>
34
- <th style="color:orange">Name</th>
35
- <th style="color:red">Email</th>
36
- <th style="color:blue">Phonenumber</th>
37
- <th style="color:red">???</th>
38
- </tr>
39
- </thead>
40
- <tbody>
41
- <tr class="odd">
42
- <td style="color:blue">USER 1'S NAME</td>
43
- <td style="color:green">USER 1'S EMAIL</td>
44
- <td style="color:orange">USER 1'S PHONENUMBER</td>
45
- <td style="color:green">Some Random Column</td>
46
- </tr>
47
- <tr class="even">
48
- <td style="color:blue">USER 2'S NAME</td>
49
- <td style="color:green">USER 2'S </td>
50
- <td style="color:orange">USER 2'S PHONENUMBER</td>
51
- <td style="color:green">Some Random Column</td>
52
- </tr>
53
- ...
54
- <tr class="odd">
55
- <td style="color:blue">LAST USER'S NAME</td>
56
- <td style="color:green">LAST USER'S EMAIL</td>
57
- <td style="color:orange">LAST USER'S PHONENUMBER</td>
58
- <td style="color:green">Some Random Column</td>
59
- </tr>
60
- </tbody>
61
- </table>
62
-
63
- Notice in the above example, we were able to provide the default attributes to be applied to each column, header, and row. Then, when specifying which columns to render, the column and header attributes could be overridden on a column by column basis.
65
+
66
+ Or the block name could have been removed altogether and replaced with a label as follows:
67
+ <%= table_for @users do |table| %>
68
+ <% table.column :label => "Full Name" do |user| %>
69
+ <%= "#{user.first_name} #{user.last_name}" %>
70
+ <% end %>
71
+ <% end %>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.17
1
+ 1.0.0
@@ -12,52 +12,37 @@
12
12
  <% end %>
13
13
 
14
14
  <% table.define :header_row do |options| %>
15
- <%= content_tag :tr, options[:header_row_html] do %>
15
+ <%= content_tag :tr, options[:header_row_html] do %>
16
16
  <%= table.use :header_columns %>
17
17
  <% end %>
18
18
  <% end %>
19
19
 
20
20
  <% table.define :header_columns do |options| %>
21
21
  <% table.columns.each do |column| %>
22
- <% header_html = options.merge(column.options)[:header_html] %>
23
- <% if options.merge(column.options)[:sortable] %>
24
- <% order = column.options[:order] ? column.options[:order].to_s : column.name.to_s %>
25
-
26
- <% sort_class = params[:order] != order ? "sorting" : (params[:sort_mode] == "desc" ? "sorting_desc" : "sorting_asc") %>
27
- <% header_html = {} if header_html.nil? %>
28
- <% header_html[:class] ||= "" %>
29
- <% header_html[:class] += " #{sort_class}" %>
30
- <% end %>
31
-
32
- <%= content_tag :th, header_html do %>
33
- <%= table.use "#{column.name.to_s}_header", options.merge(column.options) %>
34
- <% end %>
22
+ <%= table.use :header_column, column, column.options %>
23
+ <% end %>
24
+ <% end %>
25
+
26
+ <% table.define :header_column do |column, options| %>
27
+ <%= content_tag :th, table_for_header_html(column, options) do %>
28
+ <%= table.use "#{column.name}_header", column, column.options %>
35
29
  <% end %>
36
30
  <% end %>
37
31
 
38
- <% table.columns.each do |column| %>
39
- <% table.define "#{column.name.to_s}_header", :column => column do |options| %>
32
+ <% table.define :edit_header do %><% end %>
33
+
34
+ <% table.define :show_header do %><% end %>
35
+
36
+ <% table.define :delete_header do %><% end %>
37
+
38
+ <% table.columns.each do |column| %>
39
+ <% table.define "#{column.name}_header" do |column, options| %>
40
40
  <% if options[:sortable] %>
41
- <%= table.use "#{options[:column].name.to_s}_header_sortable_link", options %>
41
+ <%= table_for_sort_link(column, options) %>
42
42
  <% else %>
43
- <%= options[:label] ? options[:label] : options[:column].name.to_s.titleize %>
43
+ <%= options[:label] ? options[:label] : column.name.to_s.titleize %>
44
44
  <% end %>
45
45
  <% end %>
46
-
47
- <% table.define "#{column.name.to_s}_header_sortable_link", :column => column do |options| %>
48
- <%= table.use :header_sortable_link, options %>
49
- <% end %>
50
- <% end %>
51
-
52
- <% table.define :header_sortable_link do |options| %>
53
- <% order = options[:order] ? options[:order].to_s : options[:column].name.to_s %>
54
- <% label = (options[:label] ? options[:label] : options[:column].name.to_s.titleize) %>
55
- <% sort_mode = ((params[:order] != order or params[:sort_mode] == "desc") ? "asc" : "desc") %>
56
- <% parameters = params.merge({:order => (options[:order] ? options[:order] : options[:column].name), :sort_mode => sort_mode}) %>
57
- <% parameters.delete(:action); parameters.delete(:controller) %>
58
- <% url = options[:sort_url] ? options[:sort_url] : "" %>
59
-
60
- <%= link_to label, "#{url}?#{parameters.to_query}" %>
61
46
  <% end %>
62
47
 
63
48
  <% table.define :tbody do |options| %>
@@ -66,43 +51,46 @@
66
51
  <% end %>
67
52
  <% end %>
68
53
 
69
- <% table.define :rows do %>
54
+ <% table.define :rows do |options| %>
70
55
  <% records.each do |record| %>
71
56
  <%= table.use :row, record %>
72
57
  <% end %>
73
58
  <% end %>
74
59
 
75
60
  <% table.define :row do |record, options| %>
76
- <%= content_tag :tr, table_for_options(options[:row_html], options) do %>
77
- <%= table.use :data_columns, record, options %>
61
+ <%= content_tag :tr, table_for_evaluated_options(record, options[:row_html]) do %>
62
+ <%= table.use :columns, record %>
78
63
  <% end %>
79
64
  <% end %>
80
65
 
81
- <% table.define :data_columns do |record, options| %>
66
+ <% table.define :columns do |record, options| %>
82
67
  <% table.columns.each do |column| %>
83
- <%= content_tag :td, options.merge(column.options)[:column_html] do %>
84
- <%= table.use column, record, options.merge(:column => column) %>
85
- <% end %>
68
+ <%= table.use :column, record, column, column.options %>
69
+ <% end %>
70
+ <% end %>
71
+
72
+ <% table.define :column do |record, column, options| %>
73
+ <%= content_tag :td, table_for_evaluated_options(record, column, options[:column_html]) do %>
74
+ <%= table.use column.name, record, column, column.options %>
86
75
  <% end %>
87
76
  <% end %>
88
77
 
89
- <% table.define :edit, :action => :edit, :link_label => "Edit" do |record, options| %>
78
+ <% table.define :edit, :action => :edit, :link_label => "Edit" do |record, column, options| %>
90
79
  <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, options[:link_html] %>
91
80
  <% end %>
92
81
 
93
- <% table.define :show, :action => nil, :link_label => "Show" do |record, options| %>
82
+ <% table.define :show, :action => nil, :link_label => "Show" do |record, column, options| %>
94
83
  <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, options[:link_html] %>
95
84
  <% end %>
96
85
 
97
- <% table.define :delete, :link_html => {}, :link_label => "Delete" do |record, options| %>
98
- <%= link_to options[:link_label], [options[:scope], record].flatten, {:method => "delete", :confirm => "Are you sure you want to delete this #{record.class.to_s.titleize}?"}.merge(options[:link_html]) %>
86
+ <% table.define :delete, :action => nil, :link_html => {}, :link_label => "Delete" do |record, column, options| %>
87
+ <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, {:method => "delete", :confirm => "Are you sure you want to delete this #{record.class.to_s.titleize}?"}.merge(options[:link_html]) %>
99
88
  <% end %>
100
89
 
101
90
  <% table.columns.each do |column| %>
102
- <% table.define column.name, :column => column do |record, options| %>
103
- <%= record.send(options[:column].name) %>
91
+ <% table.define "#{column.name}" do |record, column, options| %>
92
+ <%= record.send(column.name) %>
104
93
  <% end %>
105
94
  <% end %>
106
95
 
107
- <%# Now that all the blocks are defined, render the table %>
108
- <%= table.use :table %>
96
+ <%= table.use :table %>
data/lib/table-for.rb CHANGED
@@ -1,13 +1,5 @@
1
1
  require "action_view"
2
2
 
3
- $LOAD_PATH.unshift(File.dirname(__FILE__))
4
-
5
3
  require "table_for/base"
6
- require "table_for/helper_methods"
7
- require "table_for/engine"
8
-
9
- $LOAD_PATH.shift
10
-
11
- if defined?(ActionView::Base)
12
- ActionView::Base.send :include, TableFor::HelperMethods
13
- end
4
+ require "table_for/view_additions"
5
+ require "table_for/engine"
@@ -4,7 +4,7 @@ module TableFor
4
4
  class Base < BuildingBlocks::Base
5
5
  alias columns queued_blocks
6
6
  alias column queue
7
-
7
+
8
8
  def header(name, options={}, &block)
9
9
  define("#{name.to_s}_header", options, &block)
10
10
  end
@@ -0,0 +1,44 @@
1
+ module TableFor
2
+ module ViewAdditions
3
+ module ClassMethods
4
+ def table_for(records, options={}, &block)
5
+ options[:records] = records
6
+ options[:template] = "table_for/table_for"
7
+ options[:variable] = "table"
8
+
9
+ TableFor::Base.new(self, options, &block).render
10
+ end
11
+
12
+ def table_for_evaluated_options(*args)
13
+ options = args.extract_options!
14
+ options.inject({}) { |hash, (k, v)| hash[k] = (v.is_a?(Proc) ? v.call(*args) : v); hash} unless options.nil?
15
+ end
16
+
17
+ def table_for_header_html(column, options={})
18
+ header_html = table_for_evaluated_options(column, options[:header_html])
19
+ if options[:sortable]
20
+ order = options[:order] ? options[:order].to_s : column.name.to_s
21
+ sort_class = (params[:order] != order || params[:sort_mode] == "reset") ? "sorting" : (params[:sort_mode] == "desc" ? "sorting_desc" : "sorting_asc")
22
+ header_html = {} if header_html.nil?
23
+ header_html[:class] = (header_html[:class] ? "#{header_html[:class]} #{sort_class}" : sort_class)
24
+ end
25
+ header_html
26
+ end
27
+
28
+ def table_for_sort_link(column, options={})
29
+ order = options[:order] ? options[:order].to_s : column.name.to_s
30
+ label = (options[:label] ? options[:label] : column.name.to_s.titleize)
31
+ sort_mode = (params[:order] != order || params[:sort_mode] == "reset") ? "asc" : (params[:sort_mode] == "desc" ? "reset" : "desc")
32
+ parameters = params.merge(:order => order, :sort_mode => sort_mode)
33
+ parameters.delete(:action)
34
+ parameters.delete(:controller)
35
+ url = options[:sort_url] ? options[:sort_url] : ""
36
+ link_to label, "#{url}?#{parameters.to_query}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ if defined?(ActionView::Base)
43
+ ActionView::Base.send :include, TableFor::ViewAdditions::ClassMethods
44
+ end
@@ -0,0 +1,830 @@
1
+ require "spec_helper"
2
+
3
+ describe "table_for" do
4
+ with_model :user do
5
+ table do |t|
6
+ t.string "email"
7
+ t.string "first_name"
8
+ t.string "last_name"
9
+ end
10
+ end
11
+
12
+ before :each do
13
+ User.create! :email => "andrew.hunter@livingsocial.com", :first_name => "Andrew", :last_name => "Hunter"
14
+ User.create! :email => "todd.fisher@livingsocial.com", :first_name => "Todd", :last_name => "Fisher"
15
+ User.create! :email => "jon.phillips@livingsocial.com", :first_name => "Jon", :last_name => "Phillips"
16
+ @users = User.all
17
+ @view = ActionView::Base.new("app/views")
18
+ end
19
+
20
+ it "should be able render a table with email and first and last name columns" do
21
+ buffer = @view.table_for @users do |table|
22
+ table.column :email
23
+ table.column :first_name
24
+ table.column :last_name
25
+ end
26
+
27
+ xml = XmlSimple.xml_in(%%
28
+ <table>
29
+ <thead><tr><th>Email</th><th>First Name</th><th>Last Name</th></tr></thead>
30
+ <tbody>
31
+ <tr><td>andrew.hunter@livingsocial.com</td><td>Andrew</td><td>Hunter</td></tr>
32
+ <tr><td>todd.fisher@livingsocial.com</td><td>Todd</td><td>Fisher</td></tr>
33
+ <tr><td>jon.phillips@livingsocial.com</td><td>Jon</td><td>Phillips</td></tr>
34
+ </tbody>
35
+ </table>%)
36
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
37
+ end
38
+
39
+ describe "table block" do
40
+ it "should be able to replace the table block" do
41
+ buffer = @view.table_for @users[0,1] do |table|
42
+ table.define :table do
43
+ "My new table definition"
44
+ end
45
+ table.column :email
46
+ table.column :first_name
47
+ table.column :last_name
48
+ end
49
+
50
+ buffer.strip.should eql "My new table definition"
51
+ end
52
+
53
+ it "should be able to specify html attributes" do
54
+ buffer = @view.table_for @users, :table_html => {:style => "background-color: orange"}
55
+ xml = XmlSimple.xml_in(%%<table style="background-color: orange"><thead><tr></tr></thead><tbody><tr></tr><tr></tr><tr></tr></tbody></table>%)
56
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
57
+ end
58
+ end
59
+
60
+ describe "thead block" do
61
+ it "should be able to replace the thead block" do
62
+ buffer = @view.table_for @users[0,1] do |table|
63
+ table.define :thead do
64
+ "<thead><tr><th>My new thead definition</th></tr></thead>".html_safe
65
+ end
66
+ table.column :first_name
67
+ end
68
+
69
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>My new thead definition</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
70
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
71
+ end
72
+
73
+ it "should be able to specify html attributes" do
74
+ buffer = @view.table_for @users[0,1], :thead_html => {:style => "background-color: orange"}
75
+ xml = XmlSimple.xml_in(%%<table><thead style="background-color: orange"><tr></tr></thead><tbody><tr></tr></tbody></table>%)
76
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
77
+ end
78
+ end
79
+
80
+ describe "header_row block" do
81
+ it "should be able to replace the header_row block" do
82
+ buffer = @view.table_for @users[0,1] do |table|
83
+ table.define :header_row do
84
+ "<tr><th>My new header_row definition</th></tr>".html_safe
85
+ end
86
+ table.column :first_name
87
+ end
88
+
89
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>My new header_row definition</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
90
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
91
+ end
92
+
93
+ it "should be able to specify html attributes" do
94
+ buffer = @view.table_for @users[0,1], :header_row_html => {:style => "background-color: orange"} do |table|
95
+ table.column :first_name
96
+ end
97
+ xml = XmlSimple.xml_in(%%<table><thead><tr style="background-color: orange"><th>First Name</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
98
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
99
+ end
100
+ end
101
+
102
+ describe "header_columns block" do
103
+ it "should be able to replace the header_columns block" do
104
+ buffer = @view.table_for @users[0,1] do |table|
105
+ table.define :header_columns do
106
+ "<th>Column 1</th><th>Column 2</th>".html_safe
107
+ end
108
+ table.column :first_name
109
+ end
110
+
111
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
112
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
113
+ end
114
+ end
115
+
116
+ describe "header_column block" do
117
+ it "should be able to replace the header_columns block" do
118
+ buffer = @view.table_for @users[0,1] do |table|
119
+ table.define :header_column do
120
+ "<th>Repeated Column</th>".html_safe
121
+ end
122
+ table.column :first_name
123
+ table.column :last_name
124
+ end
125
+
126
+ xml = XmlSimple.xml_in(%%
127
+ <table>
128
+ <thead><tr><th>Repeated Column</th><th>Repeated Column</th></tr></thead>
129
+ <tbody><tr><td>Andrew</td><td>Hunter</td></tr></tbody>
130
+ </table>%)
131
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
132
+ end
133
+
134
+ it "should be able to specify html attributes" do
135
+ buffer = @view.table_for @users[0,1], :header_html => {:class => "sortable"} do |table|
136
+ table.column :first_name
137
+ end
138
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th class="sortable">First Name</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
139
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
140
+ end
141
+
142
+ it "should be able to dynamically specify column attributes" do
143
+ buffer = @view.table_for @users[0, 1], :header_html => {:class => lambda {@view.cycle("even", "odd")},
144
+ :id => lambda {|column| "#{column.name.to_s}_header"}} do |table|
145
+ table.column :email
146
+ table.column :first_name
147
+ table.column :last_name
148
+ end
149
+
150
+ xml = XmlSimple.xml_in(%%
151
+ <table>
152
+ <thead>
153
+ <tr>
154
+ <th class="even" id="email_header">Email</th>
155
+ <th class="odd" id="first_name_header">First Name</th>
156
+ <th class="even" id="last_name_header">Last Name</th>
157
+ </tr></thead>
158
+ <tbody>
159
+ <tr><td>andrew.hunter@livingsocial.com</td><td>Andrew</td><td>Hunter</td></tr>
160
+ </tbody>
161
+ </table>%)
162
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
163
+ end
164
+ end
165
+
166
+ describe "edit_header block" do
167
+ it "should be able to replace the edit_header block"
168
+ end
169
+
170
+ describe "delete_header block" do
171
+ it "should be able to replace the delete_header block"
172
+ end
173
+
174
+ describe "show_header block" do
175
+ it "should be able to replace the show_header block"
176
+ end
177
+
178
+ describe "column header contents block" do
179
+ it "should be able to override the label for a particular column" do
180
+ buffer = @view.table_for @users[0,1] do |table|
181
+ table.column :email, :label => "Email Address"
182
+ end
183
+
184
+ xml = XmlSimple.xml_in(%%
185
+ <table>
186
+ <thead><tr><th>Email Address</th></tr></thead>
187
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
188
+ </table>%)
189
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
190
+ end
191
+
192
+ it "should be able to override the global defined label for all columns" do
193
+ buffer = @view.table_for @users[0,1], :label => "My Default Label" do |table|
194
+ table.column :email, :label => "Email Address"
195
+ table.column :first_name
196
+ end
197
+
198
+ xml = XmlSimple.xml_in(%%
199
+ <table>
200
+ <thead><tr><th>Email Address</th><th>My Default Label</th></tr></thead>
201
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td><td>Andrew</td></tr></tbody>
202
+ </table>%)
203
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
204
+ end
205
+
206
+ it "should be able to override the definition for a particular column header block" do
207
+ buffer = @view.table_for @users[0,1] do |table|
208
+ table.column :email, :label => "Email Address"
209
+ table.define :email_header do |column, options|
210
+ "My Own Header (Replaced #{options[:label]})"
211
+ end
212
+ end
213
+
214
+ xml = XmlSimple.xml_in(%%
215
+ <table>
216
+ <thead><tr><th>My Own Header (Replaced Email Address)</th></tr></thead>
217
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
218
+ </table>%)
219
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
220
+ end
221
+
222
+ it "should be able to override the definition for a particular column header block using the table_for 'header' method" do
223
+ buffer = @view.table_for @users[0,1] do |table|
224
+ table.column :email, :label => "Email Address"
225
+ table.header :email do |column, options|
226
+ "My Own Header (Replaced #{options[:label]})"
227
+ end
228
+ end
229
+
230
+ xml = XmlSimple.xml_in(%%
231
+ <table>
232
+ <thead><tr><th>My Own Header (Replaced Email Address)</th></tr></thead>
233
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
234
+ </table>%)
235
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
236
+ end
237
+
238
+ it "should add a 'sorting' class to the 'th' element and a link around the header content if a column is sortable" do
239
+ @view.expects(:params).at_least_once.returns({})
240
+ buffer = @view.table_for @users[0,1] do |table|
241
+ table.column :email, :sortable => true, :header_html => {:class => "email", :style => "color:red"}
242
+ end
243
+ xml = XmlSimple.xml_in(%%
244
+ <table>
245
+ <thead><tr><th class="email sorting" style="color:red"><a href="?order=email&sort_mode=asc">Email</a></th></tr></thead>
246
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
247
+ </table>%)
248
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
249
+ end
250
+
251
+ it "should add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order" do
252
+ @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "asc"})
253
+ buffer = @view.table_for @users[0,1] do |table|
254
+ table.column :email, :sortable => true
255
+ end
256
+ xml = XmlSimple.xml_in(%%
257
+ <table>
258
+ <thead><tr><th class="sorting_asc"><a href="?order=email&sort_mode=desc">Email</a></th></tr></thead>
259
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
260
+ </table>%)
261
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
262
+ end
263
+
264
+ it "should add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order and the order param matches the column order field" do
265
+ @view.expects(:params).at_least_once.returns({:order => "email_address", :sort_mode => "asc"})
266
+ buffer = @view.table_for @users[0,1] do |table|
267
+ table.column :email, :sortable => true, :order => "email_address"
268
+ end
269
+ xml = XmlSimple.xml_in(%%
270
+ <table>
271
+ <thead><tr><th class="sorting_asc"><a href="?order=email_address&sort_mode=desc">Email</a></th></tr></thead>
272
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
273
+ </table>%)
274
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
275
+ end
276
+
277
+ it "should not add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order and the order param does not match the column order field" do
278
+ @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "asc"})
279
+ buffer = @view.table_for @users[0,1] do |table|
280
+ table.column :email, :sortable => true, :order => "email_address"
281
+ end
282
+ xml = XmlSimple.xml_in(%%
283
+ <table>
284
+ <thead><tr><th class="sorting"><a href="?order=email_address&sort_mode=asc">Email</a></th></tr></thead>
285
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
286
+ </table>%)
287
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
288
+ end
289
+
290
+ it "should add a 'sorting_desc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in desc order" do
291
+ @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "desc"})
292
+ buffer = @view.table_for @users[0,1] do |table|
293
+ table.column :email, :sortable => true
294
+ end
295
+ xml = XmlSimple.xml_in(%%
296
+ <table>
297
+ <thead><tr><th class="sorting_desc"><a href="?order=email&sort_mode=reset">Email</a></th></tr></thead>
298
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
299
+ </table>%)
300
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
301
+ end
302
+
303
+ it "should add a 'sorting' class to the 'th' element and a link around the header content if a column is sortable and the sort_mode is in reset mode" do
304
+ @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "reset"})
305
+ buffer = @view.table_for @users[0,1] do |table|
306
+ table.column :email, :sortable => true
307
+ end
308
+ xml = XmlSimple.xml_in(%%
309
+ <table>
310
+ <thead><tr><th class="sorting"><a href="?order=email&sort_mode=asc">Email</a></th></tr></thead>
311
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
312
+ </table>%)
313
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
314
+ end
315
+
316
+ it "should allow a sort_url to be specified for sortable columns" do
317
+ @view.expects(:params).at_least_once.returns({})
318
+ buffer = @view.table_for @users[0,1] do |table|
319
+ table.column :email, :sortable => true, :sort_url => "/users"
320
+ end
321
+ xml = XmlSimple.xml_in(%%
322
+ <table>
323
+ <thead><tr><th class="sorting"><a href="/users?order=email&sort_mode=asc">Email</a></th></tr></thead>
324
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td></tr></tbody>
325
+ </table>%)
326
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
327
+ end
328
+
329
+ it "should allow a global sortable option for all columns and the ability to override on a column by column basis" do
330
+ @view.expects(:params).at_least_once.returns({})
331
+ buffer = @view.table_for @users[0,1], :sortable => true do |table|
332
+ table.column :email
333
+ table.column :first_name, :sortable => false
334
+ table.column :last_name
335
+ end
336
+ xml = XmlSimple.xml_in(%%
337
+ <table>
338
+ <thead>
339
+ <tr>
340
+ <th class="sorting"><a href="?order=email&sort_mode=asc">Email</a></th>
341
+ <th>First Name</th>
342
+ <th class="sorting"><a href="?order=last_name&sort_mode=asc">Last Name</a></th>
343
+ </tr>
344
+ </thead>
345
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td><td>Andrew</td><td>Hunter</td></tr></tbody>
346
+ </table>%)
347
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
348
+ end
349
+
350
+ it "should allow a global header_html option for all columns and the ability to override on a column by column basis" do
351
+ buffer = @view.table_for @users[0,1], :header_html => {:class => lambda {|column| "#{column.name}_header"}} do |table|
352
+ @view.expects(:params).at_least_once.returns({})
353
+ table.column :email, :sortable => true
354
+ table.column :first_name, :header_html => {:class => "my_header"}
355
+ table.column :last_name, :header_html => {:class => "my_other_header"}, :sortable => true
356
+ end
357
+ xml = XmlSimple.xml_in(%%
358
+ <table>
359
+ <thead>
360
+ <tr>
361
+ <th class="email_header sorting"><a href="?order=email&sort_mode=asc">Email</a></th>
362
+ <th class="my_header">First Name</th>
363
+ <th class="my_other_header sorting"><a href="?order=last_name&sort_mode=asc">Last Name</a></th>
364
+ </tr>
365
+ </thead>
366
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td><td>Andrew</td><td>Hunter</td></tr></tbody>
367
+ </table>%)
368
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
369
+ end
370
+ end
371
+
372
+ describe "tbody block" do
373
+ it "should be able to replace the tbody block" do
374
+ buffer = @view.table_for @users[0,1] do |table|
375
+ table.define :tbody do
376
+ "<tbody><tr><td>My new tbody definition</td></tr></tbody>".html_safe
377
+ end
378
+ table.column :first_name
379
+ end
380
+
381
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>First Name</th></tr></thead><tbody><tr><td>My new tbody definition</td></tr></tbody></table>%)
382
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
383
+ end
384
+
385
+ it "should be able to specify html attributes" do
386
+ buffer = @view.table_for @users[0,1], :tbody_html => {:style => "background-color: orange"}
387
+ xml = XmlSimple.xml_in(%%<table><thead><tr></tr></thead><tbody style="background-color: orange"><tr></tr></tbody></table>%)
388
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
389
+ end
390
+ end
391
+
392
+ describe "rows block" do
393
+ it "should be able to replace the rows block" do
394
+ buffer = @view.table_for @users do |table|
395
+ table.define :rows do
396
+ "<tr><td>There are #{@users.length} rows</td></tr><tr><td>This is the next row</td></tr>".html_safe
397
+ end
398
+ table.column :first_name
399
+ end
400
+
401
+ xml = XmlSimple.xml_in(%%
402
+ <table>
403
+ <thead><tr><th>First Name</th></tr></thead>
404
+ <tbody><tr><td>There are 3 rows</td></tr><tr><td>This is the next row</td></tr></tbody>
405
+ </table>%)
406
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
407
+ end
408
+ end
409
+
410
+ describe "row block" do
411
+ it "should be able to replace the row block" do
412
+ buffer = @view.table_for @users do |table|
413
+ table.define :row do |user|
414
+ "<tr><td>User #{user.first_name}</td></tr>".html_safe
415
+ end
416
+ table.column :first_name
417
+ end
418
+
419
+ xml = XmlSimple.xml_in(%%
420
+ <table>
421
+ <thead><tr><th>First Name</th></tr></thead>
422
+ <tbody>
423
+ <tr><td>User Andrew</td></tr>
424
+ <tr><td>User Todd</td></tr>
425
+ <tr><td>User Jon</td></tr>
426
+ </tbody>
427
+ </table>%)
428
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
429
+ end
430
+
431
+ it "should be able to dynamically specify row attributes" do
432
+ buffer = @view.table_for @users, :row_html => {:class => lambda {@view.cycle("even", "odd")},
433
+ :id => lambda {|user| "user-#{user.id}"}} do |table|
434
+ table.column :email
435
+ end
436
+
437
+ xml = XmlSimple.xml_in(%%
438
+ <table>
439
+ <thead><tr><th>Email</th></tr></thead>
440
+ <tbody>
441
+ <tr class="even" id="user-1"><td>andrew.hunter@livingsocial.com</td></tr>
442
+ <tr class="odd" id="user-2"><td>todd.fisher@livingsocial.com</td></tr>
443
+ <tr class="even" id="user-3"><td>jon.phillips@livingsocial.com</td></tr>
444
+ </tbody>
445
+ </table>%)
446
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
447
+ end
448
+ end
449
+
450
+ describe "columns block" do
451
+ it "should be able to replace the columns block" do
452
+ buffer = @view.table_for @users[0, 1] do |table|
453
+ table.define :columns do |user|
454
+ table.columns.map {|column| "<td>Value: #{user.send(column.name) if user.respond_to?(column.name)}</td>"}.join("").html_safe
455
+ end
456
+ table.column :first_name
457
+ table.column :last_name
458
+ table.column :some_random_column
459
+ end
460
+
461
+ xml = XmlSimple.xml_in(%%
462
+ <table>
463
+ <thead><tr><th>First Name</th><th>Last Name</th><th>Some Random Column</th></tr></thead>
464
+ <tbody><tr><td>Value: Andrew</td><td>Value: Hunter</td><td>Value:</td></tr></tbody>
465
+ </table>%)
466
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
467
+ end
468
+ end
469
+
470
+ describe "column block" do
471
+ it "should be able to replace the columns block" do
472
+ buffer = @view.table_for @users[0, 1] do |table|
473
+ table.define :column do |user, column, options|
474
+ "<td>#{column.name.to_s.titleize} value is #{user.send(column.name)}</td>".html_safe
475
+ end
476
+ table.column :email
477
+ table.column :first_name
478
+ table.column :last_name
479
+ end
480
+
481
+ xml = XmlSimple.xml_in(%%
482
+ <table>
483
+ <thead><tr><th>Email</th><th>First Name</th><th>Last Name</th></tr></thead>
484
+ <tbody>
485
+ <tr>
486
+ <td>Email value is andrew.hunter@livingsocial.com</td>
487
+ <td>First Name value is Andrew</td>
488
+ <td>Last Name value is Hunter</td>
489
+ </tr>
490
+ </tbody>
491
+ </table>%)
492
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
493
+ end
494
+
495
+ it "should be able to specify html attributes" do
496
+ buffer = @view.table_for @users[0,1], :column_html => {:class => "data"} do |table|
497
+ table.column :first_name
498
+ end
499
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>First Name</th></tr></thead><tbody><tr><td class="data">Andrew</td></tr></tbody></table>%)
500
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
501
+ end
502
+
503
+ it "should be able to dynamically specify column attributes" do
504
+ buffer = @view.table_for @users[0, 1], :column_html => {:class => lambda {@view.cycle("even", "odd")},
505
+ :id => lambda {|user, column| "#{column.name.to_s}_data"}} do |table|
506
+ table.column :email
507
+ table.column :first_name
508
+ table.column :last_name
509
+ end
510
+
511
+ xml = XmlSimple.xml_in(%%
512
+ <table>
513
+ <thead>
514
+ <tr><th>Email</th><th>First Name</th><th>Last Name</th></tr>
515
+ </thead>
516
+ <tbody>
517
+ <tr>
518
+ <td class="even" id="email_data">andrew.hunter@livingsocial.com</td>
519
+ <td class="odd" id="first_name_data">Andrew</td>
520
+ <td class="even" id="last_name_data">Hunter</td>
521
+ </tr>
522
+ </tbody>
523
+ </table>%)
524
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
525
+ end
526
+
527
+ it "should allow a global column_html option for all columns and the ability to override on a column by column basis" do
528
+ buffer = @view.table_for @users[0,1], :column_html => {:class => lambda {|record, column| "#{column.name}_data"}} do |table|
529
+ table.column :first_name, :column_html => {:class => "my_data"}
530
+ table.column :last_name
531
+ end
532
+ xml = XmlSimple.xml_in(%%
533
+ <table>
534
+ <thead><tr><th>First Name</th><th>Last Name</th></tr></thead>
535
+ <tbody>
536
+ <tr>
537
+ <td class="my_data">Andrew</td>
538
+ <td class="last_name_data">Hunter</td>
539
+ </tr>
540
+ </tbody>
541
+ </table>%)
542
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
543
+ end
544
+ end
545
+
546
+ describe "edit block" do
547
+ it "should be able to replace the edit block"
548
+ it "should be able to create an edit column" do
549
+ @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit")
550
+
551
+ buffer = @view.table_for @users[0,1] do |table|
552
+ table.column :edit
553
+ end
554
+
555
+ xml = XmlSimple.xml_in(%%
556
+ <table>
557
+ <thead><tr><th></th></tr></thead>
558
+ <tbody><tr><td><a href="/users/1/edit">Edit</a></td></tr></tbody>
559
+ </table>%)
560
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
561
+ end
562
+
563
+ it "should be able to update the label for an edit column" do
564
+ @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit")
565
+
566
+ buffer = @view.table_for @users[0,1] do |table|
567
+ table.column :edit, :link_label => "Modify"
568
+ end
569
+
570
+ xml = XmlSimple.xml_in(%%
571
+ <table>
572
+ <thead><tr><th></th></tr></thead>
573
+ <tbody><tr><td><a href="/users/1/edit">Modify</a></td></tr></tbody>
574
+ </table>%)
575
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
576
+ end
577
+
578
+ it "should be able to specify the action for an edit column" do
579
+ @view.expects(:modify_user_path).with(User.first).returns("/users/1/modify")
580
+
581
+ buffer = @view.table_for @users[0,1] do |table|
582
+ table.column :edit, :action => :modify
583
+ end
584
+
585
+ xml = XmlSimple.xml_in(%%
586
+ <table>
587
+ <thead><tr><th></th></tr></thead>
588
+ <tbody><tr><td><a href="/users/1/modify">Edit</a></td></tr></tbody>
589
+ </table>%)
590
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
591
+ end
592
+
593
+ it "should be able to specify the scope for an edit column" do
594
+ @view.expects(:edit_user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1/edit")
595
+
596
+ buffer = @view.table_for @users[0,1] do |table|
597
+ table.column :edit, :scope => [User.last, :test]
598
+ end
599
+
600
+ xml = XmlSimple.xml_in(%%
601
+ <table>
602
+ <thead><tr><th></th></tr></thead>
603
+ <tbody><tr><td><a href="/users/3/test/users/1/edit">Edit</a></td></tr></tbody>
604
+ </table>%)
605
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
606
+ end
607
+
608
+ it "should be able to specify the html for an edit column" do
609
+ @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit")
610
+
611
+ buffer = @view.table_for @users[0,1] do |table|
612
+ table.column :edit, :link_html => {:style => "color:red"}
613
+ end
614
+
615
+ xml = XmlSimple.xml_in(%%
616
+ <table>
617
+ <thead><tr><th></th></tr></thead>
618
+ <tbody><tr><td><a href="/users/1/edit" style="color:red">Edit</a></td></tr></tbody>
619
+ </table>%)
620
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
621
+ end
622
+ end
623
+
624
+ describe "show block" do
625
+ it "should be able to replace the show block"
626
+ it "should be able to create a show column" do
627
+ @view.expects(:user_path).with(User.first).returns("/users/1")
628
+
629
+ buffer = @view.table_for @users[0,1] do |table|
630
+ table.column :show
631
+ end
632
+
633
+ xml = XmlSimple.xml_in(%%
634
+ <table>
635
+ <thead><tr><th></th></tr></thead>
636
+ <tbody><tr><td><a href="/users/1">Show</a></td></tr></tbody>
637
+ </table>%)
638
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
639
+ end
640
+
641
+ it "should be able to update the label for an show column" do
642
+ @view.expects(:user_path).with(User.first).returns("/users/1")
643
+
644
+ buffer = @view.table_for @users[0,1] do |table|
645
+ table.column :show, :link_label => "Display"
646
+ end
647
+
648
+ xml = XmlSimple.xml_in(%%
649
+ <table>
650
+ <thead><tr><th></th></tr></thead>
651
+ <tbody><tr><td><a href="/users/1">Display</a></td></tr></tbody>
652
+ </table>%)
653
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
654
+ end
655
+
656
+ it "should be able to specify the action for an show column" do
657
+ @view.expects(:display_user_path).with(User.first).returns("/users/1/display")
658
+
659
+ buffer = @view.table_for @users[0,1] do |table|
660
+ table.column :show, :action => :display
661
+ end
662
+
663
+ xml = XmlSimple.xml_in(%%
664
+ <table>
665
+ <thead><tr><th></th></tr></thead>
666
+ <tbody><tr><td><a href="/users/1/display">Show</a></td></tr></tbody>
667
+ </table>%)
668
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
669
+ end
670
+
671
+ it "should be able to specify the scope for an show column" do
672
+ @view.expects(:user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1")
673
+
674
+ buffer = @view.table_for @users[0,1] do |table|
675
+ table.column :show, :scope => [User.last, :test]
676
+ end
677
+
678
+ xml = XmlSimple.xml_in(%%
679
+ <table>
680
+ <thead><tr><th></th></tr></thead>
681
+ <tbody><tr><td><a href="/users/3/test/users/1">Show</a></td></tr></tbody>
682
+ </table>%)
683
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
684
+ end
685
+
686
+ it "should be able to specify the html for an show column" do
687
+ @view.expects(:user_path).with(User.first).returns("/users/1")
688
+
689
+ buffer = @view.table_for @users[0,1] do |table|
690
+ table.column :show, :link_html => {:style => "color:red"}
691
+ end
692
+
693
+ xml = XmlSimple.xml_in(%%
694
+ <table>
695
+ <thead><tr><th></th></tr></thead>
696
+ <tbody><tr><td><a href="/users/1" style="color:red">Show</a></td></tr></tbody>
697
+ </table>%)
698
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
699
+ end
700
+ end
701
+
702
+ describe "delete block" do
703
+ it "should be able to replace the delete block"
704
+ it "should be able to create a delete column" do
705
+ @view.expects(:user_path).with(User.first).returns("/users/1")
706
+
707
+ buffer = @view.table_for @users[0,1] do |table|
708
+ table.column :delete
709
+ end
710
+
711
+ xml = XmlSimple.xml_in(%%
712
+ <table>
713
+ <thead><tr><th></th></tr></thead>
714
+ <tbody>
715
+ <tr>
716
+ <td>
717
+ <a href="/users/1" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Delete</a>
718
+ </td>
719
+ </tr>
720
+ </tbody>
721
+ </table>%)
722
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
723
+ end
724
+
725
+ it "should be able to update the label for an delete column" do
726
+ @view.expects(:user_path).with(User.first).returns("/users/1")
727
+
728
+ buffer = @view.table_for @users[0,1] do |table|
729
+ table.column :delete, :link_label => "Destroy"
730
+ end
731
+
732
+ xml = XmlSimple.xml_in(%%
733
+ <table>
734
+ <thead><tr><th></th></tr></thead>
735
+ <tbody>
736
+ <tr>
737
+ <td>
738
+ <a href="/users/1" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Destroy</a>
739
+ </td>
740
+ </tr>
741
+ </tbody>
742
+ </table>%)
743
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
744
+ end
745
+
746
+ it "should be able to specify the action for an delete column" do
747
+ @view.expects(:dispose_user_path).with(User.first).returns("/users/1/dispose")
748
+
749
+ buffer = @view.table_for @users[0,1] do |table|
750
+ table.column :delete, :action => :dispose
751
+ end
752
+
753
+ xml = XmlSimple.xml_in(%%
754
+ <table>
755
+ <thead><tr><th></th></tr></thead>
756
+ <tbody>
757
+ <tr>
758
+ <td>
759
+ <a href="/users/1/dispose" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Delete</a>
760
+ </td>
761
+ </tr>
762
+ </tbody>
763
+ </table>%)
764
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
765
+ end
766
+
767
+ it "should be able to specify the scope for a delete column" do
768
+ @view.expects(:user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1")
769
+
770
+ buffer = @view.table_for @users[0,1] do |table|
771
+ table.column :delete, :scope => [User.last, :test]
772
+ end
773
+
774
+ xml = XmlSimple.xml_in(%%
775
+ <table>
776
+ <thead><tr><th></th></tr></thead>
777
+ <tbody>
778
+ <tr>
779
+ <td>
780
+ <a href="/users/3/test/users/1" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Delete</a>
781
+ </td>
782
+ </tr>
783
+ </tbody>
784
+ </table>%)
785
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
786
+ end
787
+
788
+ it "should be able to specify the html for a delete column" do
789
+ @view.expects(:user_path).with(User.first).returns("/users/1")
790
+
791
+ buffer = @view.table_for @users[0,1] do |table|
792
+ table.column :delete, :link_html => {:style => "color:red"}
793
+ end
794
+
795
+ xml = XmlSimple.xml_in(%%
796
+ <table>
797
+ <thead><tr><th></th></tr></thead>
798
+ <tbody>
799
+ <tr>
800
+ <td>
801
+ <a href="/users/1" style="color:red" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Delete</a>
802
+ </td>
803
+ </tr>
804
+ </tbody>
805
+ </table>%)
806
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
807
+ end
808
+
809
+ it "should be able to override the delete message for a delete link"
810
+ it "should be able to override the method for a delete link"
811
+ end
812
+
813
+ describe "column data contents block" do
814
+ it "should be able to replace an individual column data contents block" do
815
+ buffer = @view.table_for @users[0,1] do |table|
816
+ table.column :email, :label => "Email Address"
817
+ table.column :label => "Full Name" do |user|
818
+ "#{user.first_name} #{user.last_name}"
819
+ end
820
+ end
821
+
822
+ xml = XmlSimple.xml_in(%%
823
+ <table>
824
+ <thead><tr><th>Email Address</th><th>Full Name</th></tr></thead>
825
+ <tbody><tr><td>andrew.hunter@livingsocial.com</td><td>Andrew Hunter</td></tr></tbody>
826
+ </table>%)
827
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
828
+ end
829
+ end
830
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require(:default)
5
+
6
+ require 'table-for'
7
+ require 'ostruct'
8
+ require 'xmlsimple'
9
+ require 'active_record'
10
+ require 'active_support/all'
11
+ require 'with_model'
12
+
13
+ def print_hash(hash)
14
+ hash.inject("") { |s, (k, v)| "#{s} #{k}: #{v}." }
15
+ end
16
+
17
+ RSpec.configure do |config|
18
+ config.extend WithModel
19
+ config.mock_with :mocha
20
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
21
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table-for
3
3
  version: !ruby/object:Gem::Version
4
- hash: 61
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
9
  - 0
9
- - 17
10
- version: 0.0.17
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-20 00:00:00 -05:00
18
+ date: 2012-02-04 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -37,14 +37,14 @@ dependencies:
37
37
  version_requirements: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
39
39
  requirements:
40
- - - ">="
40
+ - - "="
41
41
  - !ruby/object:Gem::Version
42
- hash: 7
42
+ hash: 3
43
43
  segments:
44
44
  - 3
45
+ - 1
45
46
  - 0
46
- - 0
47
- version: 3.0.0
47
+ version: 3.1.0
48
48
  prerelease: false
49
49
  type: :runtime
50
50
  requirement: *id002
@@ -55,12 +55,12 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- hash: 15
58
+ hash: 23
59
59
  segments:
60
+ - 1
60
61
  - 0
61
62
  - 0
62
- - 8
63
- version: 0.0.8
63
+ version: 1.0.0
64
64
  prerelease: false
65
65
  type: :runtime
66
66
  requirement: *id003
@@ -92,6 +92,34 @@ dependencies:
92
92
  prerelease: false
93
93
  type: :development
94
94
  requirement: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: jeweler
97
+ version_requirements: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ prerelease: false
107
+ type: :development
108
+ requirement: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ name: jeweler
111
+ version_requirements: &id007 !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ prerelease: false
121
+ type: :development
122
+ requirement: *id007
95
123
  description: table-for is a table builder for an array of objects, easily allowing overriding of how any aspect of the table is generated
96
124
  email: hunterae@gmail.com
97
125
  executables: []
@@ -108,8 +136,10 @@ files:
108
136
  - lib/table-for.rb
109
137
  - lib/table_for/base.rb
110
138
  - lib/table_for/engine.rb
111
- - lib/table_for/helper_methods.rb
139
+ - lib/table_for/view_additions.rb
112
140
  - rails/init.rb
141
+ - spec/integration/table_for_spec.rb
142
+ - spec/spec_helper.rb
113
143
  has_rdoc: true
114
144
  homepage: http://github.com/hunterae/table-for
115
145
  licenses: []
@@ -1,20 +0,0 @@
1
- module TableFor
2
- module HelperMethods
3
- def table_for_options(options={}, parameters={})
4
- return nil if options.nil?
5
-
6
- evaluated_options = {}
7
- options.each_pair { |k, v| evaluated_options[k] = (v.is_a?(Proc) ? v.call(parameters) : v)}
8
- evaluated_options
9
- end
10
-
11
- def table_for(records, options={}, &block)
12
- options[:records] = records
13
- options[:template] = "table_for/table_for"
14
- options[:templates_folder] = "table_for"
15
- options[:variable] = "table"
16
-
17
- TableFor::Base.new(self, options, &block).render
18
- end
19
- end
20
- end