Skip to content

Commit b40cc88

Browse files
committed
add spec on default_pagination, bootstrap_pagination
1 parent 827a2c6 commit b40cc88

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

lib/liquid-rails/filters/misc_filter.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def toggle_class_name(class_name, condition)
2525

2626
def default_pagination(paginate)
2727
html = []
28-
html << %(<span class="prev">#{link_to(paginate['previous']['title'], paginate['previous']['url'])}</span>) if paginate['previous']
28+
html << %(<span class="prev">#{link_to(paginate['previous']['title'], paginate['previous']['url'], rel: 'prev')}</span>) if paginate['previous']
2929

3030
for part in paginate['parts']
3131
if part['is_link']
@@ -37,28 +37,32 @@ def default_pagination(paginate)
3737
end
3838
end
3939

40-
html << %(<span class="next">#{link_to(paginate['next']['title'], paginate['next']['url'])}</span>) if paginate['next']
40+
html << %(<span class="next">#{link_to(paginate['next']['title'], paginate['next']['url'], rel: 'next')}</span>) if paginate['next']
4141
html.join(' ')
4242
end
4343

44-
def bootstrap_pagination(paginate, size="")
44+
# Bootstrap pagination filter
45+
#
46+
# @param [ paginate ]
47+
# @param [ size ]: .pagination-lg, .pagination-sm
48+
49+
def bootstrap_pagination(paginate, size='')
4550
html = []
46-
nav_ele = %{<nav><ul class="pagination #{size}">}
47-
html << nav_ele
51+
html << %{<nav><ul class="pagination #{size}">}
4852

4953
if paginate['previous']
50-
html << %(<li><a href="#{paginate['previous']['url']}" aria-label="Previous"><span aria-hidden="true"> #{paginate['previous']['title']}</span></a></li>)
54+
html << %(<li><a href="#{paginate['previous']['url']}" aria-label="Previous"><span aria-hidden="true">#{paginate['previous']['title']}</span></a></li>)
5155
else
5256
html << %(<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li>)
5357
end
5458

5559
for part in paginate['parts']
5660
if part['is_link']
5761
html << %(<li><a href="#{part['url']}">#{part['title']}</a></li>)
58-
elsif part['hellip_break']
59-
html << %(<li><a href="#">#{part['title']}</a></li>)
60-
else
62+
elsif part['title'].to_i == paginate['current_page'].to_i
6163
html << %(<li class="active"><a href="#">#{part['title']}</a></li>)
64+
else
65+
html << %(<li><a href="#">#{part['title']}</a></li>)
6266
end
6367
end
6468

lib/liquid-rails/tags/paginate_tag.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def render(context)
5050
pagination = {}
5151
pagination['collection'] = paginated_collection
5252
pagination['current_offset'] = (current_page-1) * @page_size
53+
pagination['current_page'] = current_page
5354
pagination['page_size'] = @page_size
5455
pagination['pages'] = page_count
5556
pagination['items'] = paginated_collection.total_count

spec/lib/liquid-rails/tags/paginate_tag_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module Rails
2727
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_offset }}{% endpaginate %}", '0', { 'post' => @post_drop })
2828
end
2929

30+
it '#current_page' do
31+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_page }}{% endpaginate %}", '1', { 'post' => @post_drop })
32+
end
33+
3034
it '#page_size' do
3135
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.page_size }}{% endpaginate %}", '2', { 'post' => @post_drop })
3236
end
@@ -54,6 +58,11 @@ module Rails
5458

5559
context 'second_page' do
5660
before(:all) { controller.params[:page] = 2 }
61+
after(:all) { controller.params[:page] = nil }
62+
63+
it '#current_page' do
64+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_page }}{% endpaginate %}", '2', { 'post' => @post_drop })
65+
end
5766

5867
it '#previous' do
5968
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.previous }}{% endpaginate %}", %|{"title"=>"&laquo; Previous", "url"=>"/?page=1", "is_link"=>true}|, { 'post' => @post_drop })
@@ -66,6 +75,7 @@ module Rails
6675

6776
context 'last_page' do
6877
before(:all) { controller.params[:page] = 3 }
78+
after(:all) { controller.params[:page] = nil }
6979

7080
it 'returns the page size' do
7181
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.collection | size }}{% endpaginate %}", '1', { 'post' => @post_drop })
@@ -79,6 +89,10 @@ module Rails
7989
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_offset }}{% endpaginate %}", '4', { 'post' => @post_drop })
8090
end
8191

92+
it '#current_page' do
93+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_page }}{% endpaginate %}", '3', { 'post' => @post_drop })
94+
end
95+
8296
it '#page_size' do
8397
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.page_size }}{% endpaginate %}", '2', { 'post' => @post_drop })
8498
end
@@ -103,6 +117,44 @@ module Rails
103117
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.parts }}{% endpaginate %}", %|{"title"=>1, "url"=>"/?page=1", "is_link"=>true}{"title"=>2, "url"=>"/?page=2", "is_link"=>true}{"title"=>3, "is_link"=>false, "hellip_break"=>false}|, { 'post' => @post_drop })
104118
end
105119
end
120+
121+
context 'default_pagination' do
122+
after(:all) { controller.params[:page] = nil }
123+
124+
it 'is in the first_page' do
125+
controller.params[:page] = 1
126+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | default_pagination }}{% endpaginate %}", %|<span class=\"page current\">1</span> <span class=\"page\"><a href=\"/?page=2\">2</a></span> <span class=\"page\"><a href=\"/?page=3\">3</a></span> <span class=\"next\"><a href=\"/?page=2\" rel=\"next\">Next &raquo;</a></span>|, { 'post' => @post_drop })
127+
end
128+
129+
it 'is in the second_page' do
130+
controller.params[:page] = 2
131+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | default_pagination }}{% endpaginate %}", %|<span class=\"prev\"><a href=\"/?page=1\" rel=\"prev\">&laquo; Previous</a></span> <span class=\"page\"><a href=\"/?page=1\">1</a></span> <span class=\"page current\">2</span> <span class=\"page\"><a href=\"/?page=3\">3</a></span> <span class=\"next\"><a href=\"/?page=3\" rel=\"next\">Next &raquo;</a></span>|, { 'post' => @post_drop })
132+
end
133+
134+
it 'is in the last_page' do
135+
controller.params[:page] = 3
136+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | default_pagination }}{% endpaginate %}", %|<span class=\"prev\"><a href=\"/?page=2\" rel=\"prev\">&laquo; Previous</a></span> <span class=\"page\"><a href=\"/?page=1\">1</a></span> <span class=\"page\"><a href=\"/?page=2\">2</a></span> <span class=\"page current\">3</span>|, { 'post' => @post_drop })
137+
end
138+
end
139+
140+
context 'bootstrap_pagination' do
141+
after(:all) { controller.params[:page] = nil }
142+
143+
it 'is in the first_page' do
144+
controller.params[:page] = 1
145+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | bootstrap_pagination }}{% endpaginate %}", %|<nav><ul class="pagination "> <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li> <li class="active"><a href="#">1</a></li> <li><a href="/?page=2">2</a></li> <li><a href="/?page=3">3</a></li> <li><a href="/?page=2" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a></li> </nav></ul>|, { 'post' => @post_drop })
146+
end
147+
148+
it 'is in the second_page' do
149+
controller.params[:page] = 2
150+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | bootstrap_pagination }}{% endpaginate %}", %|<nav><ul class="pagination "> <li><a href="/?page=1" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li> <li><a href="/?page=1">1</a></li> <li class="active"><a href="#">2</a></li> <li><a href="/?page=3">3</a></li> <li><a href="/?page=3" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a></li> </nav></ul>|, { 'post' => @post_drop })
151+
end
152+
153+
it 'is in the last_page' do
154+
controller.params[:page] = 3
155+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate | bootstrap_pagination }}{% endpaginate %}", %|<nav><ul class="pagination "> <li><a href="/?page=2" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li> <li><a href="/?page=1">1</a></li> <li><a href="/?page=2">2</a></li> <li class="active"><a href="#">3</a></li> <li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a></li> </nav></ul>|, { 'post' => @post_drop })
156+
end
157+
end
106158
end
107159
end
108160
end

0 commit comments

Comments
 (0)