File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ gem "pony", "~>1.0.1"
1818gem "sinatra-content-for"
1919gem "exceptional"
2020gem "newrelic_rpm"
21+ gem "will_paginate"
2122
2223group :development do
2324 gem "rocco"
Original file line number Diff line number Diff line change 8989 nokogiri (>= 1.2.0 )
9090 rack (>= 1.0 )
9191 rack-test (>= 0.5.3 )
92+ will_paginate (2.3.15 )
9293
9394PLATFORMS
9495 ruby
@@ -114,3 +115,4 @@ DEPENDENCIES
114115 sinatra-content-for
115116 steak (~> 1.0.0 )
116117 webrat (~> 0.7.1 )
118+ will_paginate
Original file line number Diff line number Diff line change 100100# [a Sinatra version](https://github.com/foca/sinatra-content-for).
101101require '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
105112require_relative 'helpers'
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!
212219get '/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
215229end
216230
Original file line number Diff line number Diff line change @@ -70,6 +70,9 @@ def gravatar_url_for email
7070 "http://www.gravatar.com/avatar/#{ Digest ::MD5 . hexdigest ( email . downcase ) } "
7171 end
7272
73+ # Will_paginate helpers
74+ include WillPaginate ::ViewHelpers
75+
7376end
7477
7578# This handy helper method lets us require an entire directory of `rb` files.
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments