implemented numbered paginator

This commit is contained in:
albert
2010-03-19 17:03:14 -04:00
parent be10906044
commit f112ff7c67
4 changed files with 45 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
class UsersController < ApplicationController
respond_to :html, :xml, :json
before_filter :member_only, :only => [:edit, :show, :update, :destroy, :create]
def new
@user = User.new
@@ -21,7 +22,10 @@ class UsersController < ApplicationController
def create
@user = User.new(params[:user].merge(:ip_addr => request.remote_ip))
flash[:notice] = "You have succesfully created a new account." if @user.save
if @user.save
flash[:notice] = "You have succesfully created a new account."
session[:user_id] = @user.id
end
respond_with(@user)
end

View File

@@ -181,6 +181,7 @@ class Post < ActiveRecord::Base
increment_tags.each do |tag|
expire_cache(tag)
end
expire_cache("")
end
def set_tag_counts
@@ -515,20 +516,17 @@ class Post < ActiveRecord::Base
module CountMethods
def fast_count(tags)
count = Cache.get("pfc:#{Cache.sanitize(tags)}")
return count unless count.nil?
count = Post.find_by_tags(tags).count
expiry = (count < 100) ? 0 : (count * 4).minutes
Cache.put("pfc:#{Cache.sanitize(tags)}", count, expiry)
if count.nil?
count = Post.find_by_tags("#{tags}").count
if count > Danbooru.config.posts_per_page * 10
Cache.put("pfc:#{Cache.sanitize(tags)}", count, (count * 4).minutes)
end
end
count
end
def fast_delete_count(tags)
count = Cache.get("pfdc:#{Cache.sanitize(tags)}")
return count unless count.nil?
count = Post.find_by_tags("#{tags} status:deleted").count
expiry = (count < 100) ? 0 : (count * 4).minutes
Cache.put("pfc:#{Cache.sanitize(tags)}", count, expiry)
count
fast_count("#{tags} status:deleted")
end
end

View File

@@ -4,7 +4,7 @@ class User < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :password, :old_password, :ip_addr
attr_accessible :password, :old_password, :password_confirmation, :password_hash, :email, :last_logged_in_at, :last_forum_read_at, :has_mail, :receive_email_notifications, :comment_threshold, :always_resize_images, :favorite_tags, :blacklisted_tags, :name
attr_accessible :password, :old_password, :password_confirmation, :password_hash, :email, :last_logged_in_at, :last_forum_read_at, :has_mail, :receive_email_notifications, :comment_threshold, :always_resize_images, :favorite_tags, :blacklisted_tags, :name, :ip_addr
validates_length_of :name, :within => 2..20, :on => :create
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"
validates_uniqueness_of :name, :case_sensitive => false, :on => :create

View File

@@ -42,60 +42,47 @@ class PostSetPresenter < Presenter
def numbered_pagination_html(template)
total_pages = (@post_set.count.to_f / @post_set.limit.to_f).ceil
current_page = [1, @post_set.page].max
before_current_page = current_page - 1
after_current_page = current_page + 1
html = "<menu>"
current_page_min = [1, current_page - 2].max
current_page_max = [total_pages, current_page + 2].min
if current_page == 1
# do nothing
elsif current_page_min == 1
1.upto(before_current_page) do |i|
html << numbered_pagination_item(template, i)
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
1.upto(3) do |i|
html << numbered_pagination_item(template, i)
end
html << "<li>...</li>"
current_page_min.upto(before_current_page) do |i|
html << numbered_pagination_item(template, i)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << %{<li class="current-page">#{current_page}</li>}
if current_page == total_pages
# do nothing
elsif current_page_max == total_pages
after_current_page.upto(total_pages) do |i|
html << numbered_pagination_item(template, i)
end
else
after_current_page.upto(after_current_page + 2) do |i|
html << numbered_pagination_item(template, i)
end
if total_pages > 5
html << "<li>...</li>"
(after_current_page + 3).upto(total_pages) do |i|
html << numbered_pagination_item(template, i)
end
end
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_item(template, page)
def numbered_pagination_item(template, page, current_page)
html = "<li>"
html << template.link_to(page, template.__send__(:posts_path, :tags => template.params[:tags], :page => page))
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << template.link_to(page, template.__send__(:posts_path, :tags => template.params[:tags], :page => page))
end
html << "</li>"
html.html_safe
end