Skip to content

Commit 22349b5

Browse files
committed
Merge branch 'master' of https://github.com/coreypurcell/hackety-hack.com into coreypurcell-master
2 parents 1d4a83e + 792c3b9 commit 22349b5

6 files changed

Lines changed: 48 additions & 1 deletion

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ gem "pony", "~>1.0.1"
1818
gem "sinatra-content-for"
1919
gem "exceptional"
2020
gem "newrelic_rpm"
21+
gem "will_paginate"
2122

2223
group :development do
2324
gem "rocco"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ GEM
8989
nokogiri (>= 1.2.0)
9090
rack (>= 1.0)
9191
rack-test (>= 0.5.3)
92+
will_paginate (2.3.15)
9293

9394
PLATFORMS
9495
ruby
@@ -114,3 +115,4 @@ DEPENDENCIES
114115
sinatra-content-for
115116
steak (~> 1.0.0)
116117
webrat (~> 0.7.1)
118+
will_paginate

hackety.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
# [a Sinatra version](https://github.com/foca/sinatra-content-for).
101101
require 'sinatra/content_for'
102102

103+
# will_paginate can work with Sinatra
104+
# https://gist.github.com/553473
105+
require 'will_paginate'
106+
require 'will_paginate/collection'
107+
require 'will_paginate/view_helpers'
108+
109+
103110
# We moved lots of helpers into a separate file. These are all things that are
104111
# useful throughout the rest of the application. This file
105112
require_relative 'helpers'
@@ -210,7 +217,14 @@
210217
# the content that others have posted. So we grab it all, and sort it in the
211218
# opposite order that it's been updated. Wouldn't want to see old stuff!
212219
get '/stream' do
213-
@content_list = Content.all.sort{|a, b| b.updated_at <=> a.updated_at }
220+
limit = 20
221+
page = params[:page] || 1
222+
page = page.to_i
223+
page -= 1
224+
offset = limit * page
225+
content_list = Content.sort(:updated_at.desc).all( :limit => limit, :offset => offset)
226+
227+
@content_list = WillPaginate::Collection.create(page + 1, limit, Content.count) {|p| p.replace content_list }
214228
haml :stream
215229
end
216230

helpers.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def gravatar_url_for email
7474
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.downcase)}"
7575
end
7676

77+
# Will_paginate helpers
78+
include WillPaginate::ViewHelpers
79+
7780
end
7881

7982
# This handy helper method lets us require an entire directory of `rb` files.

models/stream_renderer.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class StreamRenderer < WillPaginate::LinkRenderer
2+
3+
def prepare col, opt, template
4+
@collection = col
5+
@options = opt
6+
@options[:container] = nil
7+
@template = template
8+
@total_pages = @collection.total_pages
9+
end
10+
11+
def page_link(page, text, attr = {})
12+
"<a href='/#{root_page}?page=#{page}'>#{text}</a>"
13+
end
14+
15+
def root_page
16+
"stream"
17+
end
18+
19+
def page_span(page, text, attr = {})
20+
"<span>#{text}</span>"
21+
end
22+
end
23+
24+
WillPaginate::ViewHelpers.pagination_options[:renderer] = StreamRenderer
25+

views/stream.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
shared a
5454
%a{:href => "/content/#{content.id}"}= content.type
5555
\: "#{content.body[0,50]}..." (#{content.comments.length} comments)
56+
!= will_paginate @content_list
57+
5658

5759
- content_for :head do
5860
:javascript

0 commit comments

Comments
 (0)