more work on paginators

This commit is contained in:
albert
2011-06-21 14:32:07 -04:00
parent fab37bc0d3
commit 2f1fdcb459
16 changed files with 89 additions and 20 deletions

View File

@@ -11,11 +11,11 @@ module PaginationHelper
html = "<menu>"
unless records.is_first_page?
html << '<li>' + link_to("&laquo; Previous", params.merge(:page => "b#{records.first_id}")) + '</li>'
html << '<li>' + link_to("&laquo; Previous", params.merge(:page => "b#{records.before_id}")) + '</li>'
end
unless records.is_last_page?
html << '<li>' + link_to("Next &raquo;", params.merge(:page => "a#{records.last_id}")) + '</li>'
html << '<li>' + link_to("Next &raquo;", params.merge(:page => "a#{records.after_id}")) + '</li>'
end
html << "</menu>"

View File

@@ -0,0 +1,37 @@
module PostSets
class Base
def has_wiki?
false
end
def wiki_page
nil
end
def presenter
@presenter ||= PostSetPresenter.new(self)
end
def is_single_tag?
false
end
def tag
tag_string
end
def arbitrary_sql_order_clause(ids, table_name)
if ids.empty?
return "#{table_name}.id desc"
end
conditions = []
ids.each_with_index do |x, n|
conditions << "when #{x} then #{n}"
end
"case #{table_name}.id " + conditions.join(" ") + " end"
end
end
end

View File

@@ -0,0 +1,24 @@
module PostSets
class Favorite < Base
attr_reader :user, :page, :favorites, :posts
def initialize(params)
@user = ::User.find(params[:id])
@page = [params[:page].to_i, 1].max
@favorites = ::Favorite.model_for(@user.id).for_user(@user.id).paginate(page)
@posts = ::Post.where("id in (?)", post_ids).order(arbitrary_sql_order_clause(post_ids, "posts")).paginate("a0")
end
def post_ids
@post_ids ||= favorites.map(&:post_id)
end
def offset
(page - 1) * records_per_page
end
def tag_string
@tag_string ||= "fav:#{user.name}"
end
end
end

View File

@@ -1,5 +1,5 @@
module PostSets
class Post
class Post < Base
attr_reader :tags, :page, :posts
def initialize(params)
@@ -31,13 +31,5 @@ module PostSets
def is_single_tag?
tags.size == 1
end
def tag
tag_string
end
def presenter
@presenter ||= PostSetPresenter.new(self)
end
end
end

View File

@@ -2,6 +2,8 @@ class Favorite < ActiveRecord::Base
TABLE_COUNT = 100
belongs_to :post
scope :for_user, lambda {|user_id| where("user_id = ?", user_id)}
def self.model_for(user_id)
mod = user_id.to_i % TABLE_COUNT
Object.const_get("Favorite#{mod}")

View File

@@ -30,7 +30,7 @@
</div>
<div id="paginator">
<%= will_paginate(@artist_versions) %>
<%= sequential_paginator(@artist_versions) %>
</div>
<%= render "artists/secondary_links" %>

View File

@@ -27,7 +27,9 @@
</table>
<div id="paginator">
<%= will_paginate(@artists) %>
<%= numbered_paginator(@artists) do |page| %>
<%= link_to(page, artists_path(:page => page, :search => params[:search])) %>
<% end %>
</div>
<%= render "secondary_links" %>

View File

@@ -7,6 +7,10 @@
<% end %>
</div>
</div>
<div id="paginator">
<%= sequential_paginator(@comments) %>
</div>
</div>
</div>

View File

@@ -9,6 +9,10 @@
<div class="clearfix"></div>
</div>
<% end %>
<div id="paginator">
<%= sequential_paginator(@posts) %>
</div>
</div>
</div>

View File

@@ -30,7 +30,7 @@
<div class="clearfix"></div>
<div class="paginator">
<%= @post_set.presenter.pagination_html(self) %>
<%= sequential_paginator(@post_set.favorites) %>
</div>
</section>

View File

@@ -20,7 +20,9 @@
</table>
<div id="paginator">
<%= will_paginate @forum_posts %>
<%= numbered_paginator(@forum_posts) do |page| %>
<%= link_to(page, forum_posts_path(:search => params[:search], :page => page)) %>
<% end %>
</div>
<%= render "forum_topics/secondary_links" %>

View File

@@ -1,4 +1,4 @@
<h3>Forum</h3>
<h1>Forum</h1>
<table width="100%" class="striped">
<thead>

View File

@@ -32,7 +32,7 @@
</table>
<div id="paginator">
<%= will_paginate(@note_versions) %>
<%= sequential_paginator(@note_versions) %>
</div>
<%= render :partial => "footer" %>

View File

@@ -34,7 +34,7 @@
</div>
<div id="paginator">
<%= will_paginate(@pool_versions) %>
<%= sequential_paginator(@pool_versions) %>
</div>
<%= render :partial => "pools/secondary_links" %>

View File

@@ -26,7 +26,7 @@
</table>
<div id="paginator">
<%= will_paginate(@pools) %>
<%= sequential_paginator(@pools) %>
</div>
<%= render "secondary_links" %>

View File

@@ -17,7 +17,9 @@
</table>
<div id="paginator">
<%= will_paginate(@wiki_pages) %>
<%= smart_paginator(@wiki_pages) do |page| %>
<%= link_to(page, wiki_pages_path(:page => page, :search => params[:search])) %>
<% end %>
</div>
</section>
</div>