table-for 1.0.0 → 1.1.0

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/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ 1.1.0 (February 4, 2012)
2
+
3
+ * Removed dependency on rails 3.1, switched to >= 3.0.0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -84,7 +84,9 @@
84
84
  <% end %>
85
85
 
86
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]) %>
87
+ <% confirm = options[:confirm] ? options[:confirm] : "Are you sure you want to delete this #{record.class.to_s.titleize}?" %>
88
+ <% method = options[:method] ? options[:method] : "delete" %>
89
+ <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, {:method => method, :confirm => confirm}.merge(options[:link_html]) %>
88
90
  <% end %>
89
91
 
90
92
  <% table.columns.each do |column| %>
@@ -19,7 +19,6 @@ module TableFor
19
19
  if options[:sortable]
20
20
  order = options[:order] ? options[:order].to_s : column.name.to_s
21
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
22
  header_html[:class] = (header_html[:class] ? "#{header_html[:class]} #{sort_class}" : sort_class)
24
23
  end
25
24
  header_html
@@ -164,15 +164,78 @@ describe "table_for" do
164
164
  end
165
165
 
166
166
  describe "edit_header block" do
167
- it "should be able to replace the edit_header block"
167
+ it "should be able to replace the edit_header block" do
168
+ @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit")
169
+ buffer = @view.table_for @users[0,1] do |table|
170
+ table.define :edit_header do
171
+ "Edit"
172
+ end
173
+ table.column :edit
174
+ end
175
+
176
+ xml = XmlSimple.xml_in(%%
177
+ <table>
178
+ <thead><tr><th>Edit</th></tr></thead>
179
+ <tbody>
180
+ <tr>
181
+ <td>
182
+ <a href="/users/1/edit">Edit</a>
183
+ </td>
184
+ </tr>
185
+ </tbody>
186
+ </table>%)
187
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
188
+ end
168
189
  end
169
190
 
170
191
  describe "delete_header block" do
171
- it "should be able to replace the delete_header block"
192
+ it "should be able to replace the delete_header block" do
193
+ @view.expects(:user_path).with(User.first).returns("/users/1")
194
+ buffer = @view.table_for @users[0,1] do |table|
195
+ table.define :delete_header do
196
+ "Delete"
197
+ end
198
+ table.column :delete
199
+ end
200
+
201
+ xml = XmlSimple.xml_in(%%
202
+ <table>
203
+ <thead><tr><th>Delete</th></tr></thead>
204
+ <tbody>
205
+ <tr>
206
+ <td>
207
+ <a href="/users/1" rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this User?">Delete</a>
208
+ </td>
209
+ </tr>
210
+ </tbody>
211
+ </table>%)
212
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
213
+ end
172
214
  end
173
215
 
174
216
  describe "show_header block" do
175
- it "should be able to replace the show_header block"
217
+ it "should be able to replace the show_header block" do
218
+ @view.expects(:user_path).with(User.first).returns("/users/1")
219
+ buffer = @view.table_for @users[0,1] do |table|
220
+ table.define :show_header do
221
+ "Show"
222
+ end
223
+ table.column :show
224
+ end
225
+
226
+ xml = XmlSimple.xml_in(%%
227
+ <table>
228
+ <thead><tr><th>Show</th></tr></thead>
229
+ <tbody>
230
+ <tr>
231
+ <td>
232
+ <a href="/users/1">Show</a>
233
+ </td>
234
+ </tr>
235
+ </tbody>
236
+ </table>%)
237
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
238
+ end
176
239
  end
177
240
 
178
241
  describe "column header contents block" do
@@ -544,7 +607,22 @@ describe "table_for" do
544
607
  end
545
608
 
546
609
  describe "edit block" do
547
- it "should be able to replace the edit block"
610
+ it "should be able to replace the edit block" do
611
+ buffer = @view.table_for @users[0,1] do |table|
612
+ table.define :edit do
613
+ "Edit Link"
614
+ end
615
+ table.column :edit
616
+ end
617
+
618
+ xml = XmlSimple.xml_in(%%
619
+ <table>
620
+ <thead><tr><th></th></tr></thead>
621
+ <tbody><tr><td>Edit Link</td></tr></tbody>
622
+ </table>%)
623
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
624
+ end
625
+
548
626
  it "should be able to create an edit column" do
549
627
  @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit")
550
628
 
@@ -622,7 +700,22 @@ describe "table_for" do
622
700
  end
623
701
 
624
702
  describe "show block" do
625
- it "should be able to replace the show block"
703
+ it "should be able to replace the show block" do
704
+ buffer = @view.table_for @users[0,1] do |table|
705
+ table.define :show do
706
+ "Show Link"
707
+ end
708
+ table.column :show
709
+ end
710
+
711
+ xml = XmlSimple.xml_in(%%
712
+ <table>
713
+ <thead><tr><th></th></tr></thead>
714
+ <tbody><tr><td>Show Link</td></tr></tbody>
715
+ </table>%)
716
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
717
+ end
718
+
626
719
  it "should be able to create a show column" do
627
720
  @view.expects(:user_path).with(User.first).returns("/users/1")
628
721
 
@@ -700,7 +793,22 @@ describe "table_for" do
700
793
  end
701
794
 
702
795
  describe "delete block" do
703
- it "should be able to replace the delete block"
796
+ it "should be able to replace the delete block" do
797
+ buffer = @view.table_for @users[0,1] do |table|
798
+ table.define :delete do
799
+ "Delete Link"
800
+ end
801
+ table.column :delete
802
+ end
803
+
804
+ xml = XmlSimple.xml_in(%%
805
+ <table>
806
+ <thead><tr><th></th></tr></thead>
807
+ <tbody><tr><td>Delete Link</td></tr></tbody>
808
+ </table>%)
809
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
810
+ end
811
+
704
812
  it "should be able to create a delete column" do
705
813
  @view.expects(:user_path).with(User.first).returns("/users/1")
706
814
 
@@ -806,8 +914,47 @@ describe "table_for" do
806
914
  XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
807
915
  end
808
916
 
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"
917
+ it "should be able to override the delete confirmation message for a delete link" do
918
+ @view.expects(:user_path).with(User.first).returns("/users/1")
919
+
920
+ buffer = @view.table_for @users[0,1] do |table|
921
+ table.column :delete, :confirm => "Are you sure?"
922
+ end
923
+
924
+ xml = XmlSimple.xml_in(%%
925
+ <table>
926
+ <thead><tr><th></th></tr></thead>
927
+ <tbody>
928
+ <tr>
929
+ <td>
930
+ <a href="/users/1" rel="nofollow" data-method="delete" data-confirm="Are you sure?">Delete</a>
931
+ </td>
932
+ </tr>
933
+ </tbody>
934
+ </table>%)
935
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
936
+ end
937
+
938
+ it "should be able to override the method for a delete link" do
939
+ @view.expects(:user_path).with(User.first).returns("/users/1")
940
+
941
+ buffer = @view.table_for @users[0,1] do |table|
942
+ table.column :delete, :method => :get
943
+ end
944
+
945
+ xml = XmlSimple.xml_in(%%
946
+ <table>
947
+ <thead><tr><th></th></tr></thead>
948
+ <tbody>
949
+ <tr>
950
+ <td>
951
+ <a href="/users/1" data-method="get" data-confirm="Are you sure you want to delete this User?">Delete</a>
952
+ </td>
953
+ </tr>
954
+ </tbody>
955
+ </table>%)
956
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
957
+ end
811
958
  end
812
959
 
813
960
  describe "column data contents block" do
@@ -0,0 +1,147 @@
1
+ require "spec_helper"
2
+
3
+ describe TableFor::ViewAdditions do
4
+ before(:each) do
5
+ @view_class = Class.new
6
+ @view = @view_class.new
7
+ @view_class.send(:include, ActionView::Helpers::TextHelper)
8
+ @view_class.send(:include, TableFor::ViewAdditions::ClassMethods)
9
+ @records = [OpenStruct.new(:id => 1)]
10
+ @column = stub(:name => :my_column)
11
+ end
12
+
13
+ describe "table_for method" do
14
+ it "should call render on the TableFor::Base instance" do
15
+ TableFor::Base.expects(:new).returns(mock(:render => ""))
16
+ @view.table_for(@records)
17
+ end
18
+
19
+ it "should pass the view as the first parameter to TableFor::Base initialization" do
20
+ TableFor::Base.expects(:new).with {|view, options| view == @view}.returns(mock(:render => ""))
21
+ @view.table_for(@records)
22
+ end
23
+
24
+ it "should default the template to render" do
25
+ TableFor::Base.expects(:new).with {|view, options| options[:template] == "table_for/table_for"}.returns(mock(:render => ""))
26
+ @view.table_for(@records)
27
+ end
28
+
29
+ it "should default the variable to 'table' to render" do
30
+ TableFor::Base.expects(:new).with {|view, options| options[:variable] == "table"}.returns(mock(:render => ""))
31
+ @view.table_for(@records)
32
+ end
33
+
34
+ it "should default the records to the collection passed in" do
35
+ TableFor::Base.expects(:new).with {|view, options| options[:records] == @records}.returns(mock(:render => ""))
36
+ @view.table_for(@records)
37
+ end
38
+
39
+ it "should add any runtime options to the options initialized for TableFor::Base" do
40
+ TableFor::Base.expects(:new).with {|view, options| options[:option1] == 1 && options[:option2] == "2"}.returns(mock(:render => ""))
41
+ @view.table_for(@records, :option1 => 1, :option2 => "2")
42
+ end
43
+ end
44
+
45
+ describe "table_for_evaluated_options method" do
46
+ it "should evaluate any proc options" do
47
+ proc1 = lambda {@view.cycle("even", "odd")}
48
+ proc2 = lambda {@view.cycle("one", "two")}
49
+ evaluated_options = @view.table_for_evaluated_options(:class => proc1, :id => proc2, :style => "color:red")
50
+ evaluated_options[:class].should eql "even"
51
+ evaluated_options[:id].should eql "one"
52
+ evaluated_options[:style].should eql "color:red"
53
+ end
54
+
55
+ it "should pass any additional arguments to evaluated procs" do
56
+ proc1 = lambda { |param1, param2| "user_#{param1}_#{param2}"}
57
+ evaluated_options = @view.table_for_evaluated_options(1, 2, :class => proc1)
58
+ evaluated_options[:class].should eql "user_1_2"
59
+ end
60
+ end
61
+
62
+ describe "table_for_header_html method" do
63
+ it "should return nil if header_html is not passed in" do
64
+ header_html = @view.table_for_header_html(@column)
65
+ header_html.should eql({})
66
+ end
67
+
68
+ it "should evaluate any procs for header_html" do
69
+ header_html = @view.table_for_header_html(@column, :header_html => {:class => lambda {|column| "#{column.name}_header"}})
70
+ header_html[:class].should eql "#{@column.name}_header"
71
+ end
72
+
73
+ it "should join the 'sorting' class with any other header_html class provided" do
74
+ @view.expects(:params).returns({})
75
+ header_html = @view.table_for_header_html(@column, :header_html => {:class => "c1 c2"}, :sortable => true)
76
+ header_html[:class].should eql "c1 c2 sorting"
77
+ end
78
+
79
+ it "should add a 'sorting' class to the header_html class if a column is sortable" do
80
+ @view.expects(:params).returns({})
81
+ header_html = @view.table_for_header_html(@column, :sortable => true)
82
+ header_html[:class].should eql "sorting"
83
+ end
84
+
85
+ it "should add a 'sorting_asc' class to the header_html class if a column is sortable and it is already sorted in asc order" do
86
+ @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "asc")
87
+ header_html = @view.table_for_header_html(@column, :sortable => true)
88
+ header_html[:class].should eql "sorting_asc"
89
+ end
90
+
91
+ it "should add a 'sorting_desc' class to the header_html class if a column is sortable and it is already sorted in desc order" do
92
+ @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "desc")
93
+ header_html = @view.table_for_header_html(@column, :sortable => true)
94
+ header_html[:class].should eql "sorting_desc"
95
+ end
96
+
97
+ it "should add a 'sorting' class to the header_html class if a column is sortable and it is reset mode" do
98
+ @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "reset")
99
+ header_html = @view.table_for_header_html(@column, :sortable => true)
100
+ header_html[:class].should eql "sorting"
101
+ end
102
+ end
103
+
104
+ describe "table_for_sort_link method" do
105
+ it "should be able to generate a sort link for a column if that column is sortable" do
106
+ @view.expects(:params).at_least_once.returns({})
107
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "?order=#{@column.name}&sort_mode=asc").returns "my link"
108
+ @view.table_for_sort_link(@column).should eql "my link"
109
+ end
110
+
111
+ it "should be able to generate a sort link for a column if that column is sortable and it is already sorted in asc order" do
112
+ @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "asc")
113
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "?order=#{@column.name}&sort_mode=desc").returns "my link"
114
+ @view.table_for_sort_link(@column).should eql "my link"
115
+ end
116
+
117
+ it "should be able to generate a sort link for a column if that column is sortable and it is already sorted in desc order" do
118
+ @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "desc")
119
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "?order=#{@column.name}&sort_mode=reset").returns "my link"
120
+ @view.table_for_sort_link(@column).should eql "my link"
121
+ end
122
+
123
+ it "should be able specify the label for a sort link" do
124
+ @view.expects(:params).at_least_once.returns({})
125
+ @view.expects(:link_to).with("My Label", "?order=#{@column.name}&sort_mode=asc").returns "my link"
126
+ @view.table_for_sort_link(@column, :label => "My Label").should eql "my link"
127
+ end
128
+
129
+ it "should be able specify the sort_url for a sort link" do
130
+ @view.expects(:params).at_least_once.returns({})
131
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "/users?order=#{@column.name}&sort_mode=asc").returns "my link"
132
+ @view.table_for_sort_link(@column, :sort_url => "/users").should eql "my link"
133
+ end
134
+
135
+ it "should be able specify the order field for a sort link" do
136
+ @view.expects(:params).at_least_once.returns({})
137
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "?order=first_name%2Clast_name&sort_mode=asc").returns "my link"
138
+ @view.table_for_sort_link(@column, :order => "first_name,last_name").should eql "my link"
139
+ end
140
+
141
+ it "should remove the action and controller params when generating the url" do
142
+ @view.expects(:params).at_least_once.returns(:controller => "users", :action => "show")
143
+ @view.expects(:link_to).with(@column.name.to_s.titleize, "?order=#{@column.name}&sort_mode=asc").returns "my link"
144
+ @view.table_for_sort_link(@column).should eql "my link"
145
+ end
146
+ end
147
+ 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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter
@@ -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: 3
42
+ hash: 7
43
43
  segments:
44
44
  - 3
45
- - 1
46
45
  - 0
47
- version: 3.1.0
46
+ - 0
47
+ version: 3.0.0
48
48
  prerelease: false
49
49
  type: :runtime
50
50
  requirement: *id002
@@ -120,6 +120,34 @@ dependencies:
120
120
  prerelease: false
121
121
  type: :development
122
122
  requirement: *id007
123
+ - !ruby/object:Gem::Dependency
124
+ name: jeweler
125
+ version_requirements: &id008 !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ prerelease: false
135
+ type: :development
136
+ requirement: *id008
137
+ - !ruby/object:Gem::Dependency
138
+ name: jeweler
139
+ version_requirements: &id009 !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ prerelease: false
149
+ type: :development
150
+ requirement: *id009
123
151
  description: table-for is a table builder for an array of objects, easily allowing overriding of how any aspect of the table is generated
124
152
  email: hunterae@gmail.com
125
153
  executables: []
@@ -129,6 +157,7 @@ extensions: []
129
157
  extra_rdoc_files:
130
158
  - README.rdoc
131
159
  files:
160
+ - CHANGELOG.rdoc
132
161
  - README.rdoc
133
162
  - Rakefile
134
163
  - VERSION
@@ -140,6 +169,7 @@ files:
140
169
  - rails/init.rb
141
170
  - spec/integration/table_for_spec.rb
142
171
  - spec/spec_helper.rb
172
+ - spec/table_for/view_additions_spec.rb
143
173
  has_rdoc: true
144
174
  homepage: http://github.com/hunterae/table-for
145
175
  licenses: []