paginated_table 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/paginated_table.rb +1 -0
- data/lib/paginated_table/controller_helpers.rb +4 -1
- data/lib/paginated_table/page.rb +0 -42
- data/lib/paginated_table/railtie.rb +6 -0
- data/lib/paginated_table/version.rb +1 -1
- data/lib/paginated_table/view_helpers.rb +1 -171
- data/test/dummy/app/controllers/data_controller.rb +11 -1
- data/test/dummy/app/views/data/_complex.html.erb +17 -0
- data/test/dummy/app/views/data/complex.html.erb +1 -0
- data/test/dummy/log/test.log +20149 -0
- data/test/dummy/tmp/cache/assets/C4B/9C0/sprockets%2Fc5653d450f0e19225c69a8448202901e +0 -0
- data/test/dummy/tmp/cache/assets/D1A/4D0/sprockets%2F7766b94fd3771a7e336ee60045f0fc7a +0 -0
- data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/D61/7F0/sprockets%2F13eed08e0333722e08db28efdc037f0c +0 -0
- data/test/dummy/tmp/cache/assets/DF7/210/sprockets%2F424c1b802ed6f5c5f23ef0de59dcd0fa +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/integration/paginated_table_integration_test.rb +15 -9
- data/test/units/column_description_test.rb +97 -0
- data/test/units/config_test.rb +2 -0
- data/test/units/controller_helpers_test.rb +9 -0
- data/test/units/data_page_test.rb +36 -0
- data/test/units/link_renderer_test.rb +44 -0
- data/test/units/page_params_test.rb +48 -0
- data/test/units/page_test.rb +2 -79
- data/test/units/row_description_test.rb +71 -0
- data/test/units/table_description_test.rb +88 -0
- data/test/units/table_renderer_test.rb +234 -0
- data/test/units/view_helpers_test.rb +1 -306
- metadata +30 -4
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -42,6 +42,12 @@ describe "paginated_table integration" do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
it "cycles the 'odd' and 'even' css classes on the data rows" do
|
46
|
+
(1..10).each do |row|
|
47
|
+
page.has_xpath?("#{tr_xpath(row)}[@class='#{row.odd? ? 'odd' : 'even'}']").must_equal true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
45
51
|
describe "with javascript" do
|
46
52
|
before do
|
47
53
|
Capybara.current_driver = Capybara.javascript_driver
|
@@ -60,13 +66,13 @@ describe "paginated_table integration" do
|
|
60
66
|
describe "without javascript" do
|
61
67
|
it "displays one page of results" do
|
62
68
|
visit "/data"
|
63
|
-
pagination_info_text.must_equal "Displaying
|
69
|
+
pagination_info_text.must_equal "Displaying Data 1 - 10 of 100 in total"
|
64
70
|
end
|
65
71
|
|
66
72
|
it "follows the link to the second page of results" do
|
67
73
|
visit "/data"
|
68
74
|
click_link "2"
|
69
|
-
pagination_info_text.must_equal "Displaying
|
75
|
+
pagination_info_text.must_equal "Displaying Data 11 - 20 of 100 in total"
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
@@ -77,14 +83,14 @@ describe "paginated_table integration" do
|
|
77
83
|
|
78
84
|
it "displays one page of results" do
|
79
85
|
visit "/data"
|
80
|
-
pagination_info_text.must_equal "Displaying
|
86
|
+
pagination_info_text.must_equal "Displaying Data 1 - 10 of 100 in total"
|
81
87
|
end
|
82
88
|
|
83
89
|
it "follows the link to the second page of results" do
|
84
90
|
visit "/data"
|
85
91
|
click_link "2"
|
86
92
|
wait_for_ajax_request
|
87
|
-
pagination_info_text.must_equal "Displaying
|
93
|
+
pagination_info_text.must_equal "Displaying Data 11 - 20 of 100 in total"
|
88
94
|
end
|
89
95
|
|
90
96
|
# Ensures the AJAX content is decorated with event handlers
|
@@ -92,10 +98,10 @@ describe "paginated_table integration" do
|
|
92
98
|
visit "/data"
|
93
99
|
click_link "4"
|
94
100
|
wait_for_ajax_request
|
95
|
-
pagination_info_text.must_equal "Displaying
|
101
|
+
pagination_info_text.must_equal "Displaying Data 31 - 40 of 100 in total"
|
96
102
|
click_link "3"
|
97
103
|
wait_for_ajax_request
|
98
|
-
pagination_info_text.must_equal "Displaying
|
104
|
+
pagination_info_text.must_equal "Displaying Data 21 - 30 of 100 in total"
|
99
105
|
end
|
100
106
|
end
|
101
107
|
end
|
@@ -195,7 +201,7 @@ describe "paginated_table integration" do
|
|
195
201
|
describe "searching" do
|
196
202
|
it "limits the results" do
|
197
203
|
visit "/data?search=secondhalf"
|
198
|
-
pagination_info_text.must_equal "Displaying
|
204
|
+
pagination_info_text.must_equal "Displaying Data 1 - 10 of 50 in total"
|
199
205
|
(51..60).each_with_index do |id, index|
|
200
206
|
page.has_xpath?("#{tr_xpath(index + 1)}/td[1][.='Name #{id}']").must_equal true
|
201
207
|
end
|
@@ -204,7 +210,7 @@ describe "paginated_table integration" do
|
|
204
210
|
it "following the page links preserves the search criteria" do
|
205
211
|
visit "/data?search=secondhalf"
|
206
212
|
click_link "2"
|
207
|
-
pagination_info_text.must_equal "Displaying
|
213
|
+
pagination_info_text.must_equal "Displaying Data 11 - 20 of 50 in total"
|
208
214
|
(61..70).each_with_index do |id, index|
|
209
215
|
page.has_xpath?("#{tr_xpath(index + 1)}/td[1][.='Name #{id}']").must_equal true
|
210
216
|
end
|
@@ -213,7 +219,7 @@ describe "paginated_table integration" do
|
|
213
219
|
it "following the sort links preserves the search criteria" do
|
214
220
|
visit "/data?search=secondhalf"
|
215
221
|
click_link "Name"
|
216
|
-
pagination_info_text.must_equal "Displaying
|
222
|
+
pagination_info_text.must_equal "Displaying Data 1 - 10 of 50 in total"
|
217
223
|
[100, 51, 52, 53, 54, 55, 56, 57, 58, 59].each_with_index do |id, index|
|
218
224
|
page.has_xpath?("#{tr_xpath(index + 1)}/td[1][.='Name #{id}']").must_equal true
|
219
225
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PaginatedTable
|
4
|
+
describe ColumnDescription do
|
5
|
+
let(:options) { {} }
|
6
|
+
let(:row) { stub("row") }
|
7
|
+
let(:name) { 'foo' }
|
8
|
+
let(:description) { ColumnDescription.new(row, name, options) }
|
9
|
+
|
10
|
+
describe "#initialize" do
|
11
|
+
it "creates a new instance with a row, a name and an optional block" do
|
12
|
+
ColumnDescription.new(row, name) { true }
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts an options hash" do
|
16
|
+
ColumnDescription.new(row, name, :baz => 'bat')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#sortable?" do
|
21
|
+
it "returns true by default" do
|
22
|
+
description.sortable?.must_equal true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns the :sortable option" do
|
26
|
+
options[:sortable] = sortable = stub("sortable")
|
27
|
+
description.sortable?.must_equal sortable
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#span" do
|
32
|
+
it "returns false by default" do
|
33
|
+
description.span.must_equal false
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the :span option" do
|
37
|
+
options[:span] = span = stub("span")
|
38
|
+
description.span.must_equal span
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#html_attributes" do
|
43
|
+
it "returns an empty hash by default" do
|
44
|
+
description.html_attributes.must_equal({})
|
45
|
+
end
|
46
|
+
|
47
|
+
it "adds the css classes given by the :class option" do
|
48
|
+
options[:class] = %w(bar baz)
|
49
|
+
description.html_attributes.must_equal(:class => 'bar baz')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "adds the css styles given by the :style option" do
|
53
|
+
options[:style] = 'font-face: bold'
|
54
|
+
description.html_attributes.must_equal(:style => 'font-face: bold')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "sets the colspan when span is :all" do
|
58
|
+
span = stub("span")
|
59
|
+
row.stubs(:colspan).with(span).returns("5")
|
60
|
+
options[:span] = span
|
61
|
+
description.html_attributes.must_equal(:colspan => '5')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#render_header" do
|
66
|
+
it "returns the titleized name" do
|
67
|
+
description.render_header.must_equal "Foo"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns the :title option if given" do
|
71
|
+
options[:title] = 'bar'
|
72
|
+
description.render_header.must_equal "bar"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#render_cell" do
|
77
|
+
let(:results) { stub("results") }
|
78
|
+
|
79
|
+
describe "on a column with no block" do
|
80
|
+
it "sends its name to the datum" do
|
81
|
+
datum = stub("datum", 'foo' => results)
|
82
|
+
description.render_cell(datum).must_equal results
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "on a column with a block" do
|
87
|
+
it "calls its block with the datum" do
|
88
|
+
datum = stub("datum")
|
89
|
+
column = ColumnDescription.new(row, name) do |block_arg|
|
90
|
+
results if block_arg == datum
|
91
|
+
end
|
92
|
+
column.render_cell(datum).must_equal results
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/test/units/config_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
1
3
|
module PaginatedTable
|
2
4
|
describe ControllerHelpers do
|
3
5
|
let(:params) { stub("params") }
|
@@ -32,6 +34,13 @@ module PaginatedTable
|
|
32
34
|
controller.paginated_table(name, collection)
|
33
35
|
end
|
34
36
|
|
37
|
+
it "renders the given partial without layout if request is xhr?" do
|
38
|
+
partial = stub("partial")
|
39
|
+
request.stubs(:xhr? => true)
|
40
|
+
controller.expects(:render).with(:partial => partial, :layout => false)
|
41
|
+
controller.paginated_table(name, collection, :partial => partial)
|
42
|
+
end
|
43
|
+
|
35
44
|
it "does not render if request is not xhr?" do
|
36
45
|
request.stubs(:xhr? => false)
|
37
46
|
controller.expects(:render).never
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PaginatedTable
|
4
|
+
describe DataPage do
|
5
|
+
describe "#data" do
|
6
|
+
let(:page) {
|
7
|
+
Page.new(
|
8
|
+
:number => 2,
|
9
|
+
:rows => 5,
|
10
|
+
:sort_column => 'name',
|
11
|
+
:sort_direction => 'asc'
|
12
|
+
)
|
13
|
+
}
|
14
|
+
let(:collection) {
|
15
|
+
collection = (1..10).map { |i| "Name #{i}" }
|
16
|
+
def collection.order(clause)
|
17
|
+
raise unless clause == "name asc"
|
18
|
+
sort
|
19
|
+
end
|
20
|
+
collection
|
21
|
+
}
|
22
|
+
|
23
|
+
it "sorts the collection and pages to the given page number" do
|
24
|
+
DataPage.new(collection, page).data.must_equal(
|
25
|
+
["Name 5", "Name 6", "Name 7", "Name 8", "Name 9"]
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#page" do
|
30
|
+
it "provides a reference to the given page" do
|
31
|
+
DataPage.new(collection, page).page.must_equal page
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PaginatedTable
|
4
|
+
describe LinkRenderer do
|
5
|
+
let(:page) { Page.new(:number => 2, :rows => 5, :sort_column => 'to_s', :sort_direction => 'desc') }
|
6
|
+
let(:data) { (1..10).to_a }
|
7
|
+
let(:data_page) { data.paginate(:page => 2, :per_page => 5) }
|
8
|
+
let(:view) { stub("view") }
|
9
|
+
let(:renderer) do
|
10
|
+
renderer = LinkRenderer.new(page)
|
11
|
+
renderer.prepare(data, {}, view)
|
12
|
+
renderer
|
13
|
+
end
|
14
|
+
let(:text) { stub("text") }
|
15
|
+
let(:href) { stub("href") }
|
16
|
+
let(:link) { stub("link") }
|
17
|
+
|
18
|
+
describe "#sort_link" do
|
19
|
+
it "calls link_to on the view with the sort url and the :remote option" do
|
20
|
+
view.stubs("url_for").
|
21
|
+
with(:sort_direction => 'asc', :per_page => '5', :page => '1', :sort_column => 'to_s').
|
22
|
+
returns(href)
|
23
|
+
view.stubs("link_to").with(text, href, :remote => true).returns(link)
|
24
|
+
renderer.sort_link(text, 'to_s').must_equal link
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#tag" do
|
29
|
+
it "calls link_to on the view with the :remote option for :a tags" do
|
30
|
+
html_safe_text = stub("html_safe_text")
|
31
|
+
text = stub("text", :to_s => stub("string", :html_safe => html_safe_text))
|
32
|
+
view.expects(:link_to).
|
33
|
+
with(html_safe_text, href, { :class => 'highlight', :remote => true }).
|
34
|
+
returns(link)
|
35
|
+
renderer.tag(:a, text, :class => 'highlight', :href => href).must_equal link
|
36
|
+
end
|
37
|
+
|
38
|
+
it "delegates to its parent for all other tags" do
|
39
|
+
view.expects(:link_to).never
|
40
|
+
renderer.tag(:span, "foo")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PaginatedTable
|
4
|
+
describe PageParams do
|
5
|
+
describe ".create_page" do
|
6
|
+
it "returns a new page created from the request params" do
|
7
|
+
page = PageParams.create_page(
|
8
|
+
:page => '2',
|
9
|
+
:per_page => '5',
|
10
|
+
:sort_column => 'name',
|
11
|
+
:sort_direction => 'desc'
|
12
|
+
)
|
13
|
+
page.number.must_equal 2
|
14
|
+
page.rows.must_equal 5
|
15
|
+
page.sort_column.must_equal 'name'
|
16
|
+
page.sort_direction.must_equal 'desc'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns a new page created from the request params and the defaults" do
|
20
|
+
page = PageParams.create_page(
|
21
|
+
{ :page => '2', :per_page => '5' },
|
22
|
+
{ :sort_column => 'name', :sort_direction => 'desc' }
|
23
|
+
)
|
24
|
+
page.number.must_equal 2
|
25
|
+
page.rows.must_equal 5
|
26
|
+
page.sort_column.must_equal 'name'
|
27
|
+
page.sort_direction.must_equal 'desc'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".to_params" do
|
32
|
+
it "creates a params hash from the page" do
|
33
|
+
page = Page.new(
|
34
|
+
:number => 2,
|
35
|
+
:rows => 5,
|
36
|
+
:sort_column => 'name',
|
37
|
+
:sort_direction => 'desc'
|
38
|
+
)
|
39
|
+
PageParams.to_params(page).must_equal(
|
40
|
+
:page => '2',
|
41
|
+
:per_page => '5',
|
42
|
+
:sort_column => 'name',
|
43
|
+
:sort_direction => 'desc'
|
44
|
+
)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/units/page_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
1
3
|
module PaginatedTable
|
2
4
|
describe Page do
|
3
5
|
let(:page) { Page.new(:number => 2, :rows => 5, :sort_column => 'name', :sort_direction => 'desc') }
|
@@ -121,83 +123,4 @@ module PaginatedTable
|
|
121
123
|
end
|
122
124
|
end
|
123
125
|
end
|
124
|
-
|
125
|
-
describe PageParams do
|
126
|
-
describe ".create_page" do
|
127
|
-
it "returns a new page created from the request params" do
|
128
|
-
page = PageParams.create_page(
|
129
|
-
:page => '2',
|
130
|
-
:per_page => '5',
|
131
|
-
:sort_column => 'name',
|
132
|
-
:sort_direction => 'desc'
|
133
|
-
)
|
134
|
-
page.number.must_equal 2
|
135
|
-
page.rows.must_equal 5
|
136
|
-
page.sort_column.must_equal 'name'
|
137
|
-
page.sort_direction.must_equal 'desc'
|
138
|
-
end
|
139
|
-
|
140
|
-
it "returns a new page created from the request params and the defaults" do
|
141
|
-
page = PageParams.create_page(
|
142
|
-
{ :page => '2', :per_page => '5' },
|
143
|
-
{ :sort_column => 'name', :sort_direction => 'desc' }
|
144
|
-
)
|
145
|
-
page.number.must_equal 2
|
146
|
-
page.rows.must_equal 5
|
147
|
-
page.sort_column.must_equal 'name'
|
148
|
-
page.sort_direction.must_equal 'desc'
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
describe ".to_params" do
|
153
|
-
it "creates a params hash from the page" do
|
154
|
-
page = Page.new(
|
155
|
-
:number => 2,
|
156
|
-
:rows => 5,
|
157
|
-
:sort_column => 'name',
|
158
|
-
:sort_direction => 'desc'
|
159
|
-
)
|
160
|
-
PageParams.to_params(page).must_equal(
|
161
|
-
:page => '2',
|
162
|
-
:per_page => '5',
|
163
|
-
:sort_column => 'name',
|
164
|
-
:sort_direction => 'desc'
|
165
|
-
)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
describe DataPage do
|
171
|
-
describe "#data" do
|
172
|
-
let(:page) {
|
173
|
-
Page.new(
|
174
|
-
:number => 2,
|
175
|
-
:rows => 5,
|
176
|
-
:sort_column => 'name',
|
177
|
-
:sort_direction => 'asc'
|
178
|
-
)
|
179
|
-
}
|
180
|
-
let(:collection) {
|
181
|
-
collection = (1..10).map { |i| "Name #{i}" }
|
182
|
-
def collection.order(clause)
|
183
|
-
raise unless clause == "name asc"
|
184
|
-
sort
|
185
|
-
end
|
186
|
-
collection
|
187
|
-
}
|
188
|
-
|
189
|
-
it "sorts the collection and pages to the given page number" do
|
190
|
-
DataPage.new(collection, page).data.must_equal(
|
191
|
-
["Name 5", "Name 6", "Name 7", "Name 8", "Name 9"]
|
192
|
-
)
|
193
|
-
end
|
194
|
-
|
195
|
-
describe "#page" do
|
196
|
-
it "provides a reference to the given page" do
|
197
|
-
DataPage.new(collection, page).page.must_equal page
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
126
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PaginatedTable
|
4
|
+
describe RowDescription do
|
5
|
+
let(:table) { TableDescription.new }
|
6
|
+
let(:description_proc) { lambda { |row| nil } }
|
7
|
+
let(:options) { {} }
|
8
|
+
let(:description) {
|
9
|
+
RowDescription.new(table, options, description_proc)
|
10
|
+
}
|
11
|
+
|
12
|
+
describe "#initialize" do
|
13
|
+
it "creates a new instance with empty columns" do
|
14
|
+
description.columns.must_equal []
|
15
|
+
end
|
16
|
+
|
17
|
+
it "calls the given block with itself" do
|
18
|
+
fake_proc = stub("proc")
|
19
|
+
fake_proc.expects(:call)
|
20
|
+
RowDescription.new(table, options, fake_proc)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#title" do
|
25
|
+
it "returns the title option" do
|
26
|
+
options[:title] = title = stub("title")
|
27
|
+
description.title.must_equal title
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#cycle" do
|
32
|
+
it "returns the cycle option" do
|
33
|
+
options[:cycle] = cycle = stub("cycle")
|
34
|
+
description.cycle.must_equal cycle
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#hidden" do
|
39
|
+
it "returns the hidden option" do
|
40
|
+
options[:hidden] = hidden = stub("hidden")
|
41
|
+
description.hidden.must_equal hidden
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#data_type" do
|
46
|
+
it "returns the data_type option" do
|
47
|
+
options[:data_type] = data_type = stub("data_type")
|
48
|
+
description.data_type.must_equal data_type
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#colspan" do
|
53
|
+
it "delegates to the table description" do
|
54
|
+
colspan = stub("colspan")
|
55
|
+
arg = stub("arg")
|
56
|
+
table.stubs(:colspan).with(arg).returns(colspan)
|
57
|
+
description.colspan(arg).must_equal colspan
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#column" do
|
62
|
+
it "constructs a new ColumnDescription and appends it to the columns array" do
|
63
|
+
column = stub("column")
|
64
|
+
name = stub("name")
|
65
|
+
ColumnDescription.stubs(:new).with(description, name).returns(column)
|
66
|
+
description.column(name)
|
67
|
+
description.columns.must_equal [column]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|