1
0
mirror of https://github.com/danbee/danbarberphoto synced 2025-03-04 08:49:07 +00:00

Use Kaminari instead of WillPaginate

This is to avoid any conflict with Kaminari in Administrate. We also add
some specs for pagination in the front end.
This commit is contained in:
Daniel Barber 2015-10-22 13:00:26 +01:00
parent 5d15bfaf5f
commit e2c4ebf0e9
6 changed files with 38 additions and 16 deletions

View File

@ -2,7 +2,7 @@ class CategoriesController < ApplicationController
# GET /categories
# GET /categories.xml
def index
@categories = Category.order('sort ASC').paginate page: params[:page], per_page: 4
@categories = Category.order('sort ASC').page(params[:page]).per(4)
@photos = Photo.featured.limit(2).order('RANDOM()')

View File

@ -28,13 +28,13 @@ class PhotosController < ApplicationController
def for_category(category_id)
@category = Category.find_by_id(category_id)
@photos = @category.photos.enabled.order { taken_at.desc }
.paginate(page: params[:page], per_page: 11)
.page(params[:page])
@page_title = @category.name
end
def all
@photos = Photo.enabled.order { taken_at.desc }
.paginate(page: params[:page], per_page: 11)
.page(params[:page])
@page_title = 'All Photos'
end
end

View File

@ -2,12 +2,12 @@
<% content_for :navigation do %>
<div class="sg-5 page-links">
<% if @categories.previous_page -%>
<%= link_to content_tag(:div, e(:larr)), { page: @categories.previous_page }, class: 'prev-link' %>
<% if !@categories.first_page? -%>
<%= link_to content_tag(:div, e(:larr)), { page: @categories.prev_page }, class: 'prev-link' %>
<% end %>
</div>
<div class="sg-5 page-links">
<% if @categories.next_page -%>
<% if !@categories.last_page? -%>
<%= link_to content_tag(:div, e(:rarr)), { page: @categories.next_page }, class: 'next-link' %>
<% end %>
</div>

View File

@ -2,12 +2,12 @@
<% content_for :navigation do %>
<div class="sg-5 page-links">
<% if @photos.previous_page -%>
<%= link_to content_tag(:div, e(:larr)), { page: @photos.previous_page }, class: 'prev-link' %>
<% if !@photos.first_page? -%>
<%= link_to content_tag(:div, e(:larr)), { page: @photos.prev_page }, class: 'prev-link' %>
<% end %>
</div>
<div class="sg-5 page-links">
<% if @photos.next_page -%>
<% if !@photos.last_page? -%>
<%= link_to content_tag(:div, e(:rarr)), { page: @photos.next_page }, class: 'next-link' %>
<% end %>
</div>

View File

@ -2,6 +2,6 @@
FactoryGirl.define do
factory :category do
name 'Test Category'
sequence(:name) { |n| "Test Category #{n}" }
end
end

View File

@ -1,19 +1,21 @@
require 'spec_helper'
feature 'visitor navigates site' do
let!(:category) { create(:category) }
let!(:photo) { create(:photo, featured: true, categories: [category]) }
it 'shows the featured image on the home page' do
category = create(:category)
photo = create(:photo, featured: true, categories: [category])
visit root_path
expect(page).to have_selector("a[data-id='#{photo.id}']")
end
it 'increments the view counter when an image is displayed', js: true do
category = create(:category)
photo = create(:photo, featured: true, categories: [category])
visit root_path
selector = "a[data-id='#{photo.id}']"
expect(page).to have_selector(selector)
page.find(selector).click
@ -26,14 +28,22 @@ feature 'visitor navigates site' do
end
it 'shows the categories' do
visit root_path
categories = create_list(:category, 5)
visit root_path
click_link 'portfolio'
expect(page).to have_link(category.name.downcase)
expect(page).to have_link(categories.first.name.downcase)
click_link '→'
expect(page).to have_link(categories.last.name.downcase)
end
it 'shows the photos for the category' do
category = create(:category)
photo = create(:photo, featured: true, categories: [category])
visit categories_path
click_link category.name.downcase
@ -41,4 +51,16 @@ feature 'visitor navigates site' do
selector = "a[data-id='#{photo.id}']"
expect(page).to have_selector(selector)
end
it 'shows the second page of photos' do
category = create(:category)
photos = create_list(:photo, 12, featured: true, categories: [category])
visit url_for([category, :photos])
click_link '→'
selector = "a[data-id='#{photos.last.id}']"
expect(page).to have_selector(selector)
end
end