Skip to content

Commit 7470940

Browse files
committed
Merge pull request #2 from yoolk/85679260-template-brave
translation filter: add interpolated string
2 parents dfcf37a + 7677e8d commit 7470940

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

lib/liquid-rails/filters/misc_filter.rb

Lines changed: 37 additions & 2 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"><a href="#{paginate['previous']['url']}" rel="prev">#{paginate['previous']['title']}</a></span>) if paginate['previous']
2929

3030
for part in paginate['parts']
3131
if part['is_link']
@@ -37,7 +37,42 @@ 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"><a href="#{paginate['next']['url']}" rel="next">#{paginate['next']['title']}</a></span>) if paginate['next']
41+
html.join(' ')
42+
end
43+
44+
# Bootstrap pagination filter
45+
#
46+
# @param [ paginate ]
47+
# @param [ size ]: .pagination-lg, .pagination-sm
48+
49+
def bootstrap_pagination(paginate, size='')
50+
html = []
51+
html << %{<nav><ul class="pagination #{size}">}
52+
53+
if paginate['previous']
54+
html << %(<li><a href="#{paginate['previous']['url']}" aria-label="Previous"><span aria-hidden="true">#{paginate['previous']['title']}</span></a></li>)
55+
else
56+
html << %(<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a></li>)
57+
end
58+
59+
for part in paginate['parts']
60+
if part['is_link']
61+
html << %(<li><a href="#{part['url']}">#{part['title']}</a></li>)
62+
elsif part['title'].to_i == paginate['current_page'].to_i
63+
html << %(<li class="active"><a href="#">#{part['title']}</a></li>)
64+
else
65+
html << %(<li><a href="#">#{part['title']}</a></li>)
66+
end
67+
end
68+
69+
if paginate['next']
70+
html << %(<li><a href="#{paginate['next']['url']}" aria-label="Next"><span aria-hidden="true">#{paginate['next']['title']}</span></a></li>)
71+
else
72+
html << %(<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a></li>)
73+
end
74+
75+
html << '</nav></ul>'
4176
html.join(' ')
4277
end
4378
end

lib/liquid-rails/filters/text_filter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Liquid
22
module Rails
33
module TextFilter
44
delegate \
5-
:truncate,
65
:highlight,
76
:excerpt,
87
:pluralize,

lib/liquid-rails/tags/paginate_tag.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ 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
5657
pagination['previous'] = link('&laquo; Previous'.html_safe, current_page-1 ) unless 1 >= current_page
57-
pagination['next'] = link('Next &raquo;'.html_safe, current_page+1 ) unless page_count <= current_page+1
58+
pagination['next'] = link('Next &raquo;'.html_safe, current_page+1 ) unless page_count < current_page+1
5859
pagination['parts'] = []
5960

6061
hellip_break = false

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

Lines changed: 64 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
@@ -52,8 +56,26 @@ module Rails
5256
end
5357
end
5458

59+
context 'second_page' do
60+
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
66+
67+
it '#previous' do
68+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.previous }}{% endpaginate %}", %|{"title"=>"&laquo; Previous", "url"=>"/?page=1", "is_link"=>true}|, { 'post' => @post_drop })
69+
end
70+
71+
it '#next' do
72+
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.next }}{% endpaginate %}", %|{"title"=>"Next &raquo;", "url"=>"/?page=3", "is_link"=>true}|, { 'post' => @post_drop })
73+
end
74+
end
75+
5576
context 'last_page' do
5677
before(:all) { controller.params[:page] = 3 }
78+
after(:all) { controller.params[:page] = nil }
5779

5880
it 'returns the page size' do
5981
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.collection | size }}{% endpaginate %}", '1', { 'post' => @post_drop })
@@ -67,6 +89,10 @@ module Rails
6789
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.current_offset }}{% endpaginate %}", '4', { 'post' => @post_drop })
6890
end
6991

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+
7096
it '#page_size' do
7197
expect_template_result("{% paginate post.comments by 2 %}{{ paginate.page_size }}{% endpaginate %}", '2', { 'post' => @post_drop })
7298
end
@@ -91,6 +117,44 @@ module Rails
91117
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 })
92118
end
93119
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
94158
end
95159
end
96160
end

0 commit comments

Comments
 (0)