Skip to content

Commit e309218

Browse files
author
Dominik Zduńczyk
committed
Added kaminari paginations of posts with bootstrap views.
1 parent bbb29d8 commit e309218

File tree

11 files changed

+65
-5
lines changed

11 files changed

+65
-5
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
33

44
ruby '2.5.1'
55

6+
# gem for smart pagination
7+
gem 'kaminari'
68
# use libary letter opener for development testing email
79
gem "letter_opener", :group => :development
810
# use quilljs to edit html posts

Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ GEM
110110
rails-dom-testing (>= 1, < 3)
111111
railties (>= 4.2.0)
112112
thor (>= 0.14, < 2.0)
113+
kaminari (1.1.1)
114+
activesupport (>= 4.1.0)
115+
kaminari-actionview (= 1.1.1)
116+
kaminari-activerecord (= 1.1.1)
117+
kaminari-core (= 1.1.1)
118+
kaminari-actionview (1.1.1)
119+
actionview
120+
kaminari-core (= 1.1.1)
121+
kaminari-activerecord (1.1.1)
122+
activerecord
123+
kaminari-core (= 1.1.1)
124+
kaminari-core (1.1.1)
113125
launchy (2.4.3)
114126
addressable (~> 2.3)
115127
less (2.6.0)
@@ -260,6 +272,7 @@ DEPENDENCIES
260272
devise
261273
jbuilder (~> 2.5)
262274
jquery-rails (~> 4.3, >= 4.3.3)
275+
kaminari
263276
letter_opener
264277
listen (>= 3.0.5, < 3.2)
265278
paperclip (~> 6.1)

app/controllers/posts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
if params[:tag]
1010
@posts = Post.tagged_with(params[:tag]).order("created_at DESC")
1111
else
12-
@posts = Post.all.order("created_at DESC")
12+
@posts = Post.page(params[:page]).per(4).order("created_at DESC")
1313
end
1414
end
1515

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<li>
2+
<%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, remote: remote %>
3+
</li>

app/views/kaminari/_gap.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<li class='disabled'>
2+
<%= content_tag :a, raw(t 'views.pagination.truncate') %>
3+
</li>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<li>
2+
<%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, { remote: remote } %>
3+
</li>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<li>
2+
<%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote %>
3+
</li>

app/views/kaminari/_page.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% if page.current? %>
2+
<li class='active'>
3+
<%= content_tag :a, page, data: { remote: remote }, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)) %>
4+
</li>
5+
<% else %>
6+
<li>
7+
<%= link_to page, url, remote: remote, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)) %>
8+
</li>
9+
<% end %>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%= paginator.render do -%>
2+
<ul class="pagination">
3+
<%= first_page_tag unless current_page.first? %>
4+
<%= prev_page_tag unless current_page.first? %>
5+
<% each_page do |page| -%>
6+
<% if page.left_outer? || page.right_outer? || page.inside_window? -%>
7+
<%= page_tag page %>
8+
<% elsif !page.was_truncated? -%>
9+
<%= gap_tag %>
10+
<% end -%>
11+
<% end -%>
12+
<%= next_page_tag unless current_page.last? %>
13+
<%= last_page_tag unless current_page.last? %>
14+
</ul>
15+
<% end -%>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<li>
2+
<%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, rel: 'prev', remote: remote %>
3+
</li>

0 commit comments

Comments
 (0)