Permalink
Browse files

Add posts pagination links

  • Loading branch information...
edwardloveall committed Aug 22, 2016
1 parent 003200f commit 74d31398fba15c0ec29f65433eb537191ff43c76
View
@@ -9,6 +9,7 @@ gem 'flutie'
gem 'high_voltage'
gem 'jquery-rails'
gem 'jquery-ui-rails'
+gem 'kaminari'
gem 'neat'
gem 'monban'
gem 'normalize-rails'
View
@@ -124,6 +124,9 @@ GEM
railties (>= 3.2.16)
json (1.8.3)
json_pure (1.8.3)
+ kaminari (0.17.0)
+ actionpack (>= 3.0.0)
+ activesupport (>= 3.0.0)
launchy (2.4.3)
addressable (~> 2.3)
loofah (2.0.3)
@@ -296,6 +299,7 @@ DEPENDENCIES
i18n-tasks
jquery-rails
jquery-ui-rails
+ kaminari
launchy
monban
neat
@@ -5,7 +5,12 @@ footer {
padding: $base-spacing;
text-align: center;
+ a {
+ color: inherit;
+ }
+
a:hover {
+ color: inherit;
text-decoration: underline;
}
}
@@ -16,3 +16,4 @@
@import 'blog/elements';
@import 'blog/posts';
@import 'blog/tomorrow-theme';
+@import 'blog/pagination';
@@ -15,8 +15,14 @@ h2 a {
}
a {
- color: inherit;
+ @include transition(color .1s ease);
+ color: $accent-color;
text-decoration: none;
+
+ &:hover {
+ @include transition(color .3s ease);
+ color: $post-text-color;
+ }
}
ol,
@@ -0,0 +1,18 @@
+nav.pagination {
+ display: flex;
+ justify-content: space-between;
+ margin: $base-spacing;
+
+ a {
+ color: $accent-color;
+ flex-grow: 1;
+
+ &:hover {
+ color: $post-text-color;
+ }
+ }
+
+ a.next {
+ text-align: right;
+ }
+}
@@ -10,16 +10,6 @@ article {
text-align: center;
}
- a {
- @include transition(color .1s ease);
- color: $accent-color;
-
- &:hover {
- @include transition(color .3s ease);
- color: $post-text-color;
- }
- }
-
img {
display: block;
margin: 0 auto 1em;
@@ -2,7 +2,8 @@ class PostsController < ApplicationController
layout 'blog'
def index
- @posts = Post.all
+ page = params[:page] || 1
+ @posts = Post.page(page).per(10)
end
def show
@@ -1 +1,5 @@
<%= render @posts %>
+<nav class="pagination">
+ <%= link_to_previous_page @posts, 'Newer', class: :previous %>
+ <%= link_to_next_page @posts, 'Older', class: :next %>
+</nav>
@@ -15,6 +15,26 @@
expect(assigns(:posts)).to eq([post])
end
+
+ context 'pagination' do
+ it 'returns maximum 10 posts' do
+ posts = create_list(:post, 10)
+ create(:post)
+
+ get :index
+
+ expect(assigns(:posts)).to eq(posts)
+ end
+
+ it 'returns posts from a page offset' do
+ create_list(:post, 10)
+ post = create(:post)
+
+ get :index, page: 2
+
+ expect(assigns(:posts)).to eq([post])
+ end
+ end
end
describe 'GET #show' do
@@ -0,0 +1,19 @@
+require 'rails_helper'
+
+RSpec.feature 'User visits posts' do
+ scenario 'and sees pagination links' do
+ create_list(:post, 11)
+
+ visit posts_url(subdomain: 'blog')
+
+ expect(page).to have_link('Older', posts_url(subdomain: 'blog', page: 2))
+ end
+
+ scenario 'and sees pagination links' do
+ create_list(:post, 11)
+
+ visit posts_url(subdomain: 'blog', page: 2)
+
+ expect(page).to have_link('Newer', posts_url(subdomain: 'blog', page: 1))
+ end
+end

0 comments on commit 74d3139

Please sign in to comment.