Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
add paging to places list and travels list
Browse files Browse the repository at this point in the history
  • Loading branch information
y-yagi committed Feb 5, 2015
1 parent 29800d3 commit 7525799
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ gem 'coffee-rails'
gem 'dotenv-rails'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'geocoder'
gem 'gon'
gem 'rails-footnotes', '~> 4.0'
gem 'geocoder'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'kaminari'
gem 'omniauth'
gem 'omniauth-github'
gem 'omniauth-twitter'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ GEM
thor (>= 0.14, < 2.0)
json (1.8.2)
jwt (1.2.1)
kaminari (0.16.2)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
kgio (2.9.3)
launchy (2.4.3)
addressable (~> 2.3)
Expand Down Expand Up @@ -305,6 +308,7 @@ DEPENDENCIES
guard-minitest
jbuilder (~> 2.0)
jquery-rails
kaminari
launchy
minitest-slow_test
minitest-sound
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/places_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class PlacesController < ApplicationController

def index
@places = if params[:already]
Place.mine(current_user).order('updated_at DESC').already_went
Place.mine(current_user).order('updated_at DESC')
.already_went.page(params[:page])
else
Place.mine(current_user).order('updated_at DESC').not_gone
Place.mine(current_user).order('updated_at DESC')
.not_gone.page(params[:page])
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/travels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TravelsController < ApplicationController
before_action :set_travel_by_owner, only: [:destroy]

def index
@travels = Travel.belong(current_user).order('start_date DESC')
@travels = Travel.belong(current_user).order('start_date DESC').page(params[:page])
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/models/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Place < ActiveRecord::Base
validates :latitude, presence: true, if: -> { address.present? }
validates :longitude, presence: true, if: -> { address.present? }

paginates_per 10

class << self
def build(params, user)
place = new(params)
Expand Down
1 change: 1 addition & 0 deletions app/models/travel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Travel < ActiveRecord::Base
validates :owner_id, presence: true
validates_with TravelDateValidator

paginates_per 10

class << self
def build(params, user)
Expand Down
10 changes: 10 additions & 0 deletions app/views/kaminari/_first_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/ Link to the "First" page
- available local variables
url : url to the first page
current_page : a page object for the currently displayed page
total_pages : total number of pages
per_page : number of items to fetch per page
remote : data-remote
li
== link_to_unless current_page.first?, t('views.pagination.first').html_safe, url, :remote => remote
'
9 changes: 9 additions & 0 deletions app/views/kaminari/_gap.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/ Non-link tag that stands for skipped pages...
- available local variables
current_page : a page object for the currently displayed page
total_pages : total number of pages
per_page : number of items to fetch per page
remote : data-remote
span.page.gap
== t('views.pagination.truncate').html_safe
'
10 changes: 10 additions & 0 deletions app/views/kaminari/_last_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/ Link to the "Last" page
- available local variables
url : url to the last page
current_page : a page object for the currently displayed page
total_pages : total number of pages
per_page : number of items to fetch per page
remote : data-remote
li
== link_to_unless current_page.last?, t('views.pagination.last').html_safe, url, :remote => remote
'
Empty file.
11 changes: 11 additions & 0 deletions app/views/kaminari/_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/ Link showing page number
- available local variables
page : a page object for "this" page
url : url to this page
current_page : a page object for the currently displayed page
total_pages : total number of pages
per_page : number of items to fetch per page
remote : data-remote
li class="#{'active' if page.current?}"
== link_to page, url, {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil}
'
19 changes: 19 additions & 0 deletions app/views/kaminari/_paginator.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/ The container tag
- available local variables
current_page : a page object for the currently displayed page
total_pages : total number of pages
per_page : number of items to fetch per page
remote : data-remote
paginator : the paginator that renders the pagination tags inside
== paginator.render do
ul.pagination
== first_page_tag unless current_page.first?
== prev_page_tag unless current_page.first?
- each_page do |page|
- if page.left_outer? || page.right_outer? || page.inside_window?
== page_tag page
- elsif !page.was_truncated?
== gap_tag
== next_page_tag unless current_page.last?
== last_page_tag unless current_page.last?
Empty file.
1 change: 1 addition & 0 deletions app/views/places/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ section.page-block.space-top.space-bottom
= link_to "参考URL#{i + 1}", url, target: '_blank'
br
= simple_format(place.memo)
= paginate @places
1 change: 1 addition & 0 deletions app/views/travels/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ section.page-block.space-top.space-bottom
= travel.formatted_schedule
br
= simple_format(travel.memo)
= paginate @travels
10 changes: 10 additions & 0 deletions config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Kaminari.configure do |config|
config.default_per_page = 10
# config.max_per_page = nil
config.window = 3
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
end
7 changes: 7 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@ ja:
travel_photo:
name: 名前
photo_service_album_id: アルバム名
views:
pagination:
first: "&laquo; 先頭"
last: "最後 &raquo;"
previous: "&lsaquo; 前"
next: "次 &rsaquo;"
truncate: "&hellip;"
8 changes: 8 additions & 0 deletions test/fixtures/places.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ shimogamo:
longitude: 135.45462
user: google

<% 10.times.each do |i| %>
places_<%= i %>:
name: $LABEL
address: 住所 $LABEL
latitude: 35.07182
longitude: 135.45462
user: google
<% end %>
13 changes: 11 additions & 2 deletions test/fixtures/travels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ kyoto:

okayama:
name: 岡山旅行
start_date: <%= 100.days.ago %>
end_date: <%= 95.days.ago %>
start_date: <%= 10.days.ago %>
end_date: <%= 5.days.ago %>
memo: 過去の旅行
owner: google

Expand All @@ -20,3 +20,12 @@ yamaguchi:
end_date: <%= 15.days.since %>
memo: 別ユーザの旅行
owner: twitter

<% 10.times.each do |i| %>
travels_<%= i %>:
name: $LABEL
start_date: <%= 100.days.ago %>
end_date: <%= 95.days.ago %>
memo: $LABEL
owner: google
<% end %>
8 changes: 8 additions & 0 deletions test/integration/place_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ def setup
assert_match '大麻比古神社', page.text
assert_match '伏見稲荷大社', page.text
end

test 'paging' do
assert_no_match 'places_9', page.text
click_link '最後'

assert_match 'places_9', page.text
assert_no_match 'places_0', page.text
end
end
8 changes: 8 additions & 0 deletions test/integration/travel_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,12 @@ def setup

assert_no_match 'バスで30分', page.text
end

test 'paging' do
assert_no_match 'travels_9', page.text
click_link '最後'

assert_match 'travels_9', page.text
assert_no_match 'travels_0', page.text
end
end

0 comments on commit 7525799

Please sign in to comment.