Created
March 30, 2016 03:34
-
-
Save ezrarush/e45f6e6f0da948ddd41200e1825dd4ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/controllers/albums_controller.rb b/app/controllers/albums_controller.rb | |
index 813b51f..10834cd 100644 | |
--- a/app/controllers/albums_controller.rb | |
+++ b/app/controllers/albums_controller.rb | |
@@ -4,12 +4,23 @@ class AlbumsController < ApplicationController | |
# GET /albums | |
# GET /albums.json | |
def index | |
- @albums = Album.all | |
+ params[:page] ||= "1" | |
+ @albums = Album.list_page(params[:page]) | |
+ @page = params[:page].to_i | |
+ @total_pages = Album.count | |
end | |
# GET /albums/1 | |
# GET /albums/1.json | |
def show | |
+ @page = params[:page].to_i | |
+ @total_pages = Album.count | |
+ end | |
+ | |
+ def list | |
+ params[:page] ||= "1" | |
+ @album = Album.list_page(params[:page]).first | |
+ redirect_to album_url(@album, page: params[:page]) | |
end | |
# GET /albums/new | |
diff --git a/app/models/album.rb b/app/models/album.rb | |
index ad6f6fe..b84e509 100644 | |
--- a/app/models/album.rb | |
+++ b/app/models/album.rb | |
@@ -6,6 +6,10 @@ class Album < ActiveRecord::Base | |
enum condition: [ :new_condition, :used_condition ] | |
belongs_to :artist | |
+ | |
+ def self.list_page(num = nil) | |
+ limit(1).offset(1 * ((num = num.to_i - 1) < 0 ? 0 : num)) | |
+ end | |
def self.Types | |
%w(Lp Cd) | |
diff --git a/app/views/albums/index.html.erb b/app/views/albums/index.html.erb | |
index 53d94ad..730c734 100644 | |
--- a/app/views/albums/index.html.erb | |
+++ b/app/views/albums/index.html.erb | |
@@ -31,3 +31,11 @@ | |
<br> | |
<%= link_to 'New Album', new_album_path %> | |
+ | |
+<span class="prev"> | |
+ <%= link_to_unless (@page == 1), "‹ Prev".html_safe, albums_path(page: (@page - 1)), rel: 'prev' %> | |
+</span> | |
+ | |
+<span class="next"> | |
+ <%= link_to_unless (@page == @total_pages), "Next ›".html_safe, albums_path(page: (@page + 1)), :rel => 'next'%> | |
+</span> | |
diff --git a/app/views/albums/show.html.erb b/app/views/albums/show.html.erb | |
index ff2abaa..a485e85 100644 | |
--- a/app/views/albums/show.html.erb | |
+++ b/app/views/albums/show.html.erb | |
@@ -22,3 +22,29 @@ | |
<%= link_to 'Edit', edit_album_path(@album) %> | | |
<%= link_to 'Back', albums_path %> | |
+ | |
+<nav class="pagination"> | |
+<span class="first"> | |
+ <%= link_to_unless (@page == 1), "« First".html_safe, list_albums_path(page: 1) %> | |
+</span> | |
+<span class="prev"> | |
+ <%= link_to_unless (@page == 1), "‹ Prev".html_safe, list_albums_path(page: (@page - 1)), rel: 'prev' %> | |
+</span> | |
+ | |
+<% each_page do |page| -%> | |
+ <% if page.left_outer? || page.right_outer? || page.inside_window? -%> | |
+ <span class="page<%= ' current' if page.current? %>"> | |
+ <%= link_to_unless (@page == page), page, list_albums_path(page: page), {:rel => page == @page + 1 ? 'next' : page == @page - 1 ? 'prev' : nil} %> | |
+ </span> | |
+ <% elsif !page.was_truncated? -%> | |
+ <span class="page gap"><%= "…".html_safe %></span> | |
+ <% end -%> | |
+<% end %> | |
+ | |
+<span class="next"> | |
+ <%= link_to_unless (@page == @total_pages), "Next ›".html_safe, list_albums_path(page: (@page + 1)), :rel => 'next'%> | |
+</span> | |
+<span class="last"> | |
+ <%= link_to_unless (page == @total_pages), "Last »".html_safe, list_albums_path(page: @total_pages) %> | |
+</span> | |
+</nav> | |
diff --git a/config/routes.rb b/config/routes.rb | |
index 597644a..2d021d6 100644 | |
--- a/config/routes.rb | |
+++ b/config/routes.rb | |
@@ -5,6 +5,8 @@ Rails.application.routes.draw do | |
end | |
resources :albums | |
+ | |
+ get '/list_albums', to: 'albums#list', as: 'list_albums' | |
root 'artists#index' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment