0
0
Fork 0

added pagination at the bottom of the index page

This commit is contained in:
Gibheer 2011-07-15 21:21:52 +02:00
parent 1787b4aa8d
commit 927e80cf1c
3 changed files with 25 additions and 7 deletions

View File

@ -16,11 +16,19 @@ class Post
first(:id => id, :released => true)
end
def self.get_all_released
all(:released => true)
end
def self.get_page page=0
all(:released => true, :limit => 10, :offset => (page * 10 + 1),
get_all_released.all(:limit => 10, :offset => (page * 10 + 1),
:order => [:written.desc])
end
def self.page_count
(get_all_released.count / 10).ceil
end
def self.find_of_day time
all(:written => time..(time+86400), :releaed => true)
end

View File

@ -6,10 +6,12 @@ class Blog < Sinatra::Base
get '/' do
if params.has_key? 'page'
@posts = Post.get_page(params['page'].to_i)
@current_page = params['page'].to_i
else
@posts = Post.get_page()
@current_page = 1
end
@page_count = Post.page_count
@posts = Post.get_page(@current_page - 1)
haml :index
end

View File

@ -1,5 +1,13 @@
-@posts.each do |post|
%article
%header
%h1=post.title
%section.content=markup(post.content, post.markup)
-@post = post
=haml :post_single, :layout => nil
.page
-if @current_page > 2
%a{:href => '/?page=1'}="<<"
-if @current_page > 1
%a{:href => "/?page=#{@current_page - 1}"}="<"
%span.bold=@current_page
-if @current_page < @page_count + 1
%a{:href => "/?page=#{@current_page + 1}"}=">"
-if @current_page < (@page_count)
%a{:href => "/?page=#{@page_count + 1}"}=">>"