sorting_table_for 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,337 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../fixtures/user')
5
+
6
+ include SortingTableForSpecHelper
7
+
8
+ describe SortingTableFor, :type => :helper do
9
+
10
+ before :each do
11
+ @users = User.all
12
+ helper.stub!(:url_for).and_return('fake_link')
13
+ helper.stub!(:params).and_return({ :controller => 'fakes', :action => 'index' })
14
+ helper.output_buffer = ''
15
+ end
16
+
17
+ describe ' #header table' do
18
+
19
+ describe ' #default usage' do
20
+
21
+ it "should works" do
22
+ helper.sorting_table_for(@users) do |table|
23
+ html = table.headers
24
+ html.should have_comp_tag("thead", :count => 1)
25
+ html.should have_comp_tag("tr", :count => 1)
26
+ html.should have_comp_tag("th", :count => 11)
27
+ end
28
+ end
29
+
30
+ it "should add sorting class on th" do
31
+ helper.sorting_table_for(@users) do |table|
32
+ table.headers.should have_comp_tag("th[class='cur-sort-not']", :count => 9)
33
+ end
34
+ end
35
+
36
+ it "should have link for sorting" do
37
+ helper.sorting_table_for(@users) do |table|
38
+ table.headers.should have_comp_tag("a", :count => 9)
39
+ end
40
+ end
41
+
42
+ it "should not sort" do
43
+ helper.sorting_table_for(@users, :sort => false) do |table|
44
+ table.headers.should_not have_comp_tag("th[class='cur-sort-not']")
45
+ table.headers.should_not have_comp_tag("a")
46
+ end
47
+ end
48
+
49
+ it "should be customize with html" do
50
+ table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
51
+ html = table.headers(:html => { :class => 'header_class', :id => 'header_id' })
52
+ html.should have_comp_tag("tr[class=header_class][id=header_id]")
53
+ end
54
+ helper.output_buffer.concat(table_html)
55
+ helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
56
+ end
57
+
58
+ end
59
+
60
+ describe ' #custom headers' do
61
+
62
+ it "should choose one column" do
63
+ helper.sorting_table_for(@users) do |table|
64
+ table.headers(:username).should have_comp_tag("th", :count => 1)
65
+ end
66
+ end
67
+
68
+ it "should choose multi columns" do
69
+ helper.sorting_table_for(@users) do |table|
70
+ table.headers(:username, :price).should have_comp_tag("th", :count => 2)
71
+ end
72
+ end
73
+
74
+ it "should choose multi columns and one action" do
75
+ helper.sorting_table_for(@users) do |table|
76
+ table.headers(:username, :price, :actions => :edit).should have_comp_tag("th", :count => 3)
77
+ end
78
+ end
79
+
80
+ it "should choose multi columns and actions" do
81
+ helper.sorting_table_for(@users) do |table|
82
+ table.headers(:username, :price, :actions => [:edit, :edit_password]).should have_comp_tag("th", :count => 4)
83
+ end
84
+ end
85
+
86
+ it "should works with non symbol field" do
87
+ helper.sorting_table_for(@users) do |table|
88
+ html = table.headers(:username, 'test', image_tag('rails.png'))
89
+ html.should have_comp_tag("th", :count => 3)
90
+ html.should have_comp_tag("th:nth-child(2)", :text => 'test')
91
+ html.should have_comp_tag("th:nth-child(3) img")
92
+ end
93
+ end
94
+
95
+ it "should works with non key symbol" do
96
+ helper.sorting_table_for(@users) do |table|
97
+ table.headers(:username, :test).should have_comp_tag("th", :count => 2)
98
+ end
99
+ end
100
+
101
+ it "should works with i18n for symbol" do
102
+ helper.sorting_table_for(@users) do |table|
103
+ html = table.headers(:username, :firstname, 'hello', :my_fake, :actions => :edit)
104
+ html.should have_comp_tag("th:nth-child(1)", :text => 'Usernames')
105
+ html.should have_comp_tag("th:nth-child(2)", :text => 'Firstnames')
106
+ html.should have_comp_tag("th:nth-child(3)", :text => 'hello')
107
+ html.should have_comp_tag("th:nth-child(4)", :text => 'Hello fake')
108
+ html.should have_comp_tag("th:nth-child(5)", :text => 'Edit users')
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ describe " #custom header" do
115
+
116
+ it "should works" do
117
+ helper.sorting_table_for(@users) do |table|
118
+ html = table.headers do
119
+ table.header(:username)
120
+ end
121
+ html.should have_comp_tag("thead", :count => 1)
122
+ html.should have_comp_tag("tr", :count => 1)
123
+ html.should have_comp_tag("th", :count => 1)
124
+ end
125
+ end
126
+
127
+ it "should works all value" do
128
+ helper.sorting_table_for(@users) do |table|
129
+ html = table.headers do
130
+ table.header :username
131
+ table.header :price
132
+ table.header 'test'
133
+ table.header image_tag('rails.png')
134
+ table.header I18n.t('my_test', :scope => [:fake_scope])
135
+ end
136
+ html.should have_comp_tag("th", :count => 5)
137
+ html.should have_comp_tag("th[class=cur-sort-not]", :count => 2)
138
+ html.should have_comp_tag("th:nth-child(3)", :text => 'test')
139
+ html.should have_comp_tag("th:nth-child(4) img")
140
+ html.should have_comp_tag('th:nth-child(5)', :text => 'Hello')
141
+ end
142
+ end
143
+
144
+ it "should be customize with html" do
145
+ table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
146
+ html = table.headers(:html => { :class => 'header_class', :id => 'header_id' }) do
147
+ table.header :username, :html => { :class => 'cell_1_class', :id => 'cell_1_id', :title => 'hello_1' }
148
+ table.header :firstname, :html => { :class => 'cell_2_class', :id => 'cell_2_id', :title => 'hello_2' }
149
+ table.header 'hello', :html => { :class => 'cell_3_class', :id => 'cell_3_id', :title => 'hello_3' }
150
+ end
151
+ html.should have_comp_tag("tr[class=header_class][id=header_id]")
152
+ html.should have_comp_tag("th:nth-child(1)[class='cell_1_class cur-sort-not'][id=cell_1_id][title=hello_1]")
153
+ html.should have_comp_tag("th:nth-child(2)[class='cell_2_class cur-sort-not'][id=cell_2_id][title=hello_2]")
154
+ html.should have_comp_tag("th:nth-child(3)[class=cell_3_class][id=cell_3_id][title=hello_3]")
155
+ end
156
+ helper.output_buffer.concat(table_html)
157
+ helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
158
+ end
159
+
160
+ it "should works with i18n for symbol" do
161
+ helper.sorting_table_for(@users) do |table|
162
+ html = table.headers do
163
+ table.header :username
164
+ table.header :firstname
165
+ table.header 'hello'
166
+ table.header :my_fake
167
+ table.header :action => :edit
168
+ end
169
+ html.should have_comp_tag("th:nth-child(1)", :text => 'Usernames')
170
+ html.should have_comp_tag("th:nth-child(2)", :text => 'Firstnames')
171
+ html.should have_comp_tag("th:nth-child(3)", :text => 'hello')
172
+ html.should have_comp_tag("th:nth-child(4)", :text => 'Hello fake')
173
+ html.should have_comp_tag("th:nth-child(5)", :text => 'Edit users')
174
+ end
175
+ end
176
+
177
+ it "should not sort all columns" do
178
+ helper.sorting_table_for(@users, :sort => false) do |table|
179
+ html = table.headers do
180
+ table.header :username
181
+ table.header :price
182
+ end
183
+ html.should_not have_comp_tag("th[class=cur-sort-not]")
184
+ end
185
+ end
186
+
187
+ it "should not sort on select columns" do
188
+ helper.sorting_table_for(@users) do |table|
189
+ html = table.headers do
190
+ table.header :username, :sort => true
191
+ table.header :price, :sort => false
192
+ end
193
+ html.should have_comp_tag("th:nth-child(1)[class=cur-sort-not]")
194
+ html.should_not have_comp_tag("th:nth-child(2)[class=cur-sort-not]")
195
+ end
196
+ end
197
+
198
+ it "should not sort on select columns with global" do
199
+ helper.sorting_table_for(@users, :sort => false) do |table|
200
+ html = table.headers do
201
+ table.header :username, :sort => true
202
+ table.header :price, :sort => false
203
+ end
204
+ html.should have_comp_tag("th:nth-child(1)[class=cur-sort-not]")
205
+ html.should_not have_comp_tag("th:nth-child(2)[class=cur-sort-not]")
206
+ end
207
+ end
208
+
209
+ end
210
+
211
+ describe ' #custom sub header' do
212
+
213
+ it "should works" do
214
+ helper.sorting_table_for(@users) do |table|
215
+ html = table.headers do
216
+ table.header do
217
+ :username
218
+ end
219
+ table.header do
220
+ :firstname
221
+ end
222
+ end
223
+ html.should have_comp_tag("th", :count => 2)
224
+ end
225
+ end
226
+
227
+ it "should not set link for sorting" do
228
+ helper.sorting_table_for(@users) do |table|
229
+ html = table.headers do
230
+ table.header do
231
+ :username
232
+ end
233
+ end
234
+ html.should_not have_comp_tag("a")
235
+ end
236
+ end
237
+
238
+ it "should not add html class for sorting" do
239
+ helper.sorting_table_for(@users) do |table|
240
+ html = table.headers do
241
+ table.header do
242
+ :username
243
+ end
244
+ end
245
+ html.should_not have_comp_tag("th[class=cur-sort-not]")
246
+ end
247
+ end
248
+
249
+ it "should not set I18n ici" do
250
+ helper.sorting_table_for(@users) do |table|
251
+ html = table.headers do
252
+ table.header do
253
+ :username
254
+ end
255
+ table.header do
256
+ 'firstname'
257
+ end
258
+ table.header do
259
+ I18n.t(:my_test, :scope => :fake_scope)
260
+ end
261
+ end
262
+ if SortingTableFor::Tools::rails3?
263
+ html.should have_comp_tag("th:nth-child(1)", :text => '')
264
+ else
265
+ html.should have_comp_tag("th:nth-child(1)", :text => 'username')
266
+ end
267
+ html.should have_comp_tag("th:nth-child(2)", :text => 'firstname')
268
+ html.should have_comp_tag("th:nth-child(3)", :text => 'Hello')
269
+ end
270
+ end
271
+
272
+ it "should be customize with html" do
273
+ table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
274
+ html = table.headers(:html => { :class => 'header_class', :id => 'header_id' }) do
275
+ table.header :html => { :class => 'cell_1_class', :id => 'cell_1_id', :title => 'hello_1' } do
276
+ :username
277
+ end
278
+ table.header :html => { :class => 'cell_2_class', :id => 'cell_2_id', :title => 'hello_2' } do
279
+ :firstname
280
+ end
281
+ table.header :html => { :class => 'cell_3_class', :id => 'cell_3_id', :title => 'hello_3' } do
282
+ 'hello'
283
+ end
284
+ end
285
+ html.should have_comp_tag("tr[class=header_class][id=header_id]")
286
+ html.should have_comp_tag("th:nth-child(1)[class='cell_1_class'][id=cell_1_id][title=hello_1]")
287
+ html.should have_comp_tag("th:nth-child(2)[class='cell_2_class'][id=cell_2_id][title=hello_2]")
288
+ html.should have_comp_tag("th:nth-child(3)[class=cell_3_class][id=cell_3_id][title=hello_3]")
289
+ end
290
+ helper.output_buffer.concat(table_html)
291
+ helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
292
+ end
293
+
294
+ end
295
+
296
+ describe ' #With editable options' do
297
+
298
+ before :each do
299
+ ## Restaure default values
300
+ SortingTableFor::TableBuilder.reserved_columns = [:id, :password, :salt]
301
+ SortingTableFor::TableBuilder.default_actions = [:edit, :delete]
302
+ SortingTableFor::TableBuilder.params_sort_table = :table_sort
303
+ end
304
+
305
+ it "should edit reserved columns" do
306
+ SortingTableFor::TableBuilder.reserved_columns = [:id, :firstname, :lastname, :position, :salary, :price, :active, :created_at, :updated_at]
307
+ helper.sorting_table_for(@users) do |table|
308
+ table.headers.should have_comp_tag("th", :count => 3)
309
+ end
310
+ end
311
+
312
+ it "should edit params sort table" do
313
+ pending("Check if the link is edit")
314
+ SortingTableFor::TableBuilder.params_sort_table = :hello_table
315
+ helper.sorting_table_for(@users) do |table|
316
+ end
317
+ end
318
+
319
+ it "should edit default actions" do
320
+ SortingTableFor::TableBuilder.default_actions = [:show, :edit_password, :edit, :delete]
321
+ helper.sorting_table_for(@users) do |table|
322
+ table.headers.should have_comp_tag("th", :count => User.column_names.size + 3)
323
+ end
324
+ end
325
+
326
+ it "should edit default actions" do
327
+ pending('check if the link is valid')
328
+ SortingTableFor::TableBuilder.default_actions = [:show, :edit_password]
329
+ helper.sorting_table_for(@users) do |table|
330
+ end
331
+ end
332
+
333
+ end
334
+
335
+ end
336
+
337
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../fixtures/user')
5
+
6
+ include SortingTableForSpecHelper
7
+
8
+ describe SortingTableFor do
9
+ describe ' #i18n' do
10
+
11
+ before :each do
12
+ SortingTableFor::I18n.set_options({:controller => 'fakes_controller', :action => 'fake_action'}, 'user', '')
13
+ end
14
+
15
+ it "should works by default" do
16
+ SortingTableFor::I18n.set_options({}, 'user', '')
17
+ SortingTableFor::I18n.t(:test_name).should == 'i18n name'
18
+ end
19
+
20
+ it "should set default scope" do
21
+ SortingTableFor::I18n.t(:name).should == 'fake name'
22
+ end
23
+
24
+ it "should not overwrite scope" do
25
+ SortingTableFor::I18n.t(:my_test, :scope => :fake_scope).should == 'Hello'
26
+ end
27
+
28
+ it "should works with i18n options" do
29
+ SortingTableFor::I18n.t(:my_test, :value => :hello).should == 'say hello'
30
+ end
31
+
32
+ it "should set namespace scope" do
33
+ SortingTableFor::I18n.set_options({:controller => 'fakes_controller', :action => 'fake_action'}, 'user', 'fake_namespace')
34
+ SortingTableFor::I18n.t(:name).should == 'fake namespace'
35
+ end
36
+
37
+ it "should set scope with options" do
38
+ SortingTableFor::TableBuilder.i18n_default_scope = [ :action ]
39
+ SortingTableFor::I18n.t(:name).should == 'fake action'
40
+ end
41
+
42
+ it "should set scope with all options" do
43
+ SortingTableFor::TableBuilder.i18n_default_scope = [ :model, :namespace, :controller, :action ]
44
+ SortingTableFor::I18n.set_options({:controller => 'fakes_controller', :action => 'fake_action'}, 'user', 'fake_namespace')
45
+ SortingTableFor::I18n.t(:name).should == 'fake'
46
+ end
47
+
48
+ it "should not replace keywords" do
49
+ SortingTableFor::TableBuilder.i18n_default_scope = [ :controller, :action, :my_add ]
50
+ SortingTableFor::I18n.t(:name).should == 'fake add'
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,92 @@
1
+ test:
2
+ date:
3
+ formats:
4
+ default: "%Y-%m-%d"
5
+ short: "%b %d"
6
+ long: "%B %d, %Y"
7
+
8
+ day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
9
+ abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
10
+
11
+ month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
12
+ abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
13
+ order:
14
+ - :year
15
+ - :month
16
+ - :day
17
+
18
+ time:
19
+ formats:
20
+ default: "%a, %d %b %Y %H:%M:%S %z"
21
+ short: "%d %b %H:%M"
22
+ long: "%B %d, %Y %H:%M"
23
+ am: "am"
24
+ pm: "pm"
25
+
26
+ number:
27
+ format:
28
+ separator: "."
29
+ delimiter: ","
30
+ precision: 3
31
+ significant: false
32
+ strip_insignificant_zeros: false
33
+
34
+ currency:
35
+ format:
36
+ format: "%u%n"
37
+ unit: "$"
38
+ separator: "."
39
+ delimiter: ","
40
+ precision: 2
41
+ significant: false
42
+ strip_insignificant_zeros: false
43
+
44
+ sorting_table_for:
45
+ total_entries: "Total Entries {{value}}"
46
+ columns:
47
+ bool_true: 'True'
48
+ bool_false: 'False'
49
+
50
+ fakes:
51
+ index:
52
+ edit: 'Edit'
53
+ delete: 'Delete'
54
+ header:
55
+ username: 'Usernames'
56
+ firstname: 'Firstnames'
57
+ lastname: 'Lastnames'
58
+ position: 'Positions'
59
+ salary: 'Salary'
60
+ price: 'Prices'
61
+ active: 'Actives'
62
+ created_at: 'Created at'
63
+ updated_at: 'Updated at'
64
+ edit: 'Edit users'
65
+ delete: 'Delete users'
66
+ my_fake: 'Hello fake'
67
+
68
+
69
+ fake_scope:
70
+ my_test: 'Hello'
71
+
72
+ test_name: 'i18n name'
73
+ fakes_controller:
74
+ fake_action:
75
+ name: 'fake name'
76
+ my_test: 'say {{value}}'
77
+ my_add:
78
+ name: 'fake add'
79
+
80
+ user:
81
+ fake_namespace:
82
+ fakes_controller:
83
+ fake_action:
84
+ name: 'fake'
85
+
86
+ fake_namespace:
87
+ fakes_controller:
88
+ fake_action:
89
+ name: 'fake namespace'
90
+
91
+ fake_action:
92
+ name: 'fake action'