major refactoring of post sets and pagination, incomplete

This commit is contained in:
albert
2011-06-03 19:47:16 -04:00
parent ce0695c606
commit ca3e9bb6db
21 changed files with 475 additions and 204 deletions

View File

@@ -0,0 +1,58 @@
module Paginators
class Numbered
attr_reader :template, :source
delegate :url, :total_pages, :current_page, :to => :source
def initialize(template, source)
@template = template
@source = source
end
def pagination_html
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << pagination_item(page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << pagination_item(page, current_page)
end
html << pagination_item("...", current_page)
html << pagination_item(total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << pagination_item(1, current_page)
html << pagination_item("...", current_page)
(current_page - window).upto(total_pages) do |page|
html << pagination_item(page, current_page)
end
else
html << pagination_item(1, current_page)
html << pagination_item("...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << pagination_item(page, current_page)
end
html << pagination_item("...", current_page)
html << pagination_item(total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
protected
def pagination_item(page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << template.link_to(page, url(template, :page => page))
end
html << "</li>"
html.html_safe
end
end
end

View File

@@ -15,6 +15,7 @@ module Paginators
[1, post_set.page].max
end
# TODO: this is not compatible with paginating favorites
def sequential_link(template)
template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
end

View File

@@ -0,0 +1,29 @@
module Paginators
class Sequential
attr_reader :template, :source
delegate :url, :to => :source
def initialize(template, source)
@template = template
@source = source
end
def pagination_html
html = "<menu>"
html << '<li>' + template.link_to("&laquo; Previous", prev_url) + '</li>'
if next_url
html << '<li>' + template.link_to("Next &raquo;", next_url) + '</li>'
end
html << "</menu>"
html.html_safe
end
def prev_url
template.request.env["HTTP_REFERER"]
end
def next_url
@next_url ||= url(template)
end
end
end