post index noe works

This commit is contained in:
albert
2011-06-24 18:22:54 -04:00
parent 9ac7f85f0b
commit 1ad075c05a
17 changed files with 91 additions and 136 deletions

View File

@@ -30,24 +30,6 @@ class PostsController < ApplicationController
end end
private private
def extend_post_set(post_set)
@post_set.extend(PostSets::Post)
if use_sequential_paginator?
@post_set.extend(PostSets::Sequential)
else
@post_set.extend(PostSets::Numbered)
end
end
def use_sequential_paginator?
if params[:page].to_i > 1000
true
else
false
end
end
def save_recent_tags def save_recent_tags
if params[:tags] || (params[:post] && params[:post][:tags]) if params[:tags] || (params[:post] && params[:post][:tags])
tags = Tag.scan_tags(params[:tags] || params[:post][:tags]) tags = Tag.scan_tags(params[:tags] || params[:post][:tags])

View File

@@ -12,7 +12,7 @@ class UploadsController < ApplicationController
def index def index
@search = Upload.search(params[:search]) @search = Upload.search(params[:search])
@uploads = @search.paginate(:page => params[:page]) @uploads = @search.order("id desc").paginate(params[:page])
respond_with(@uploads) respond_with(@uploads)
end end

View File

@@ -1,28 +1,28 @@
module PaginationHelper module PaginationHelper
def smart_paginator(records, &block)
if records.is_sequential_paginator? || params[:page].to_i > 200
sequential_paginator(records)
else
numbered_paginator(records, &block)
end
end
def sequential_paginator(records) def sequential_paginator(records)
html = "<menu>" html = "<menu>"
unless records.is_first_page? if records.any?
html << '<li>' + link_to("&laquo; Previous", params.merge(:page => "b#{records.before_id}")) + '</li>' if params[:page] =~ /[ab]/
end html << '<li>' + link_to("< Previous", params.merge(:page => "a#{records[0].id}")) + '</li>'
end
unless records.is_last_page? html << '<li>' + link_to("Next >", params.merge(:page => "b#{records[-1].id}")) + '</li>'
html << '<li>' + link_to("Next &raquo;", params.merge(:page => "a#{records.after_id}")) + '</li>'
end end
html << "</menu>" html << "</menu>"
html.html_safe html.html_safe
end end
def use_sequential_paginator?(records)
params[:page] =~ /[ab]/ || records.current_page > Danbooru.config.max_numbered_pages
end
def numbered_paginator(records, &block) def numbered_paginator(records, &block)
if use_sequential_paginator?(records)
return sequential_paginator(records)
end
html = "<menu>" html = "<menu>"
window = 3 window = 3
if records.total_pages <= (window * 2) + 5 if records.total_pages <= (window * 2) + 5
@@ -55,7 +55,7 @@ module PaginationHelper
end end
def numbered_paginator_final_item(total_pages, current_page, &block) def numbered_paginator_final_item(total_pages, current_page, &block)
if total_pages <= 200 if total_pages <= Danbooru.config.max_numbered_pages
numbered_paginator_item(total_pages, current_page, &block) numbered_paginator_item(total_pages, current_page, &block)
else else
"" ""
@@ -67,7 +67,7 @@ module PaginationHelper
if page == "..." if page == "..."
html << "..." html << "..."
elsif page == current_page elsif page == current_page
html << page.to_s html << '<span>' + page.to_s + '</span>'
else else
html << capture(page, &block) html << capture(page, &block)
end end

View File

@@ -4,7 +4,7 @@ module PostSets
def initialize(params) def initialize(params)
@tag_array = Tag.scan_query(params[:tags]) @tag_array = Tag.scan_query(params[:tags])
@page = [params[:page].to_i, 1].max @page = params[:page]
@posts = ::Post.tag_match(tag_string).paginate(page) @posts = ::Post.tag_match(tag_string).paginate(page)
end end
@@ -13,7 +13,7 @@ module PostSets
end end
def has_wiki? def has_wiki?
if tags.any? if tag_array.any?
::WikiPage.titled(tag_string).exists? ::WikiPage.titled(tag_string).exists?
else else
false false
@@ -21,7 +21,7 @@ module PostSets
end end
def wiki_page def wiki_page
if tags.any? if tag_array.any?
::WikiPage.titled(tag_string).first ::WikiPage.titled(tag_string).first
else else
nil nil

View File

@@ -14,9 +14,13 @@ class PostSetPresenter < Presenter
tag_set_presenter.tag_list_html(template) tag_set_presenter.tag_list_html(template)
end end
def post_previews_html def post_previews_html(template)
html = "" html = ""
if posts.empty?
return template.render(:partial => "post_sets/blank")
end
posts.each do |post| posts.each do |post|
html << PostPresenter.preview(post) html << PostPresenter.preview(post)
end end

View File

@@ -48,13 +48,13 @@
</nav> </nav>
</header> </header>
<%- if flash[:notice] -%>
<div class="ui-corner-all ui-state-highlight" id="notice"><span class="ui-icon ui-icon-info"></span> <%= flash[:notice] %></div>
<%- else -%>
<div class="ui-corner-all ui-state-highlight" id="notice" style="display: none;"><span class="ui-icon ui-icon-info"></span></div>
<%- end -%>
<div id="page"> <div id="page">
<%- if flash[:notice] -%>
<div class="ui-corner-all ui-state-highlight" id="notice"><span class="ui-icon ui-icon-info"></span> <%= flash[:notice] %></div>
<%- else -%>
<div class="ui-corner-all ui-state-highlight" id="notice" style="display: none;"><span class="ui-icon ui-icon-info"></span></div>
<%- end -%>
<%= yield :layout %> <%= yield :layout %>
</div> </div>

View File

@@ -0,0 +1,3 @@
<p>Nobody here but us chickens!</p>
<p><%= link_to "Go back", :back %>.</p>

View File

@@ -34,7 +34,7 @@
</section> </section>
<% content_for(:page_title) do %> <% content_for(:page_title) do %>
/<%= @post_set.tags.join(" ") %> /<%= @post_set.tag_string %>
<% end %> <% end %>
<%= render :partial => "posts/partials/common/secondary_links" %> <%= render :partial => "posts/partials/common/secondary_links" %>

View File

@@ -1,11 +1,11 @@
<h1>Posts</h1> <h1>Posts</h1>
<%= post_set.presenter.post_previews_html %> <%= post_set.presenter.post_previews_html(self) %>
<div class="clearfix"></div> <div class="clearfix"></div>
<div class="paginator"> <div class="paginator">
<%= smart_paginator(post_set.posts) do |page| %> <%= numbered_paginator(post_set.posts) do |page| %>
<%= link_to(page, posts_path(:page => page, :tags => post_set.tags)) %> <%= link_to(page, posts_path(:page => page, :tags => post_set.tag_string)) %>
<% end %> <% end %>
</div> </div>

View File

@@ -1,24 +1,32 @@
<table width="100%"> <div id="c-uploads">
<thead> <div id="a-index">
<tr> <table width="100%">
<th></th> <thead>
<th>Uploader</th> <tr>
<th>Status</th> <th></th>
<th>Date</th> <th>Uploader</th>
<th>Tags</th> <th>Status</th>
</tr> <th>Date</th>
</thead> <th>Tags</th>
<tbody> </tr>
<% @uploads.each do |upload| %> </thead>
<tr> <tbody>
<td><%= link_to upload.id, upload_path(upload) %></td> <% @uploads.each do |upload| %>
<td><%= link_to upload.uploader.name, user_path(upload.uploader) %></td> <tr>
<td><%= upload.presenter.status(self) %></td> <td><%= link_to upload.id, upload_path(upload) %></td>
<td><%= upload.created_at %></td> <td><%= link_to upload.uploader.name, user_path(upload.uploader) %></td>
<td><%= upload.tag_string %></td> <td><%= upload.presenter.status(self) %></td>
</tr> <td><%= upload.created_at %></td>
<% end %> <td><%= upload.tag_string %></td>
</tbody> </tr>
</table> <% end %>
</tbody>
</table>
<div class="paginator">
<%= sequential_paginator(@uploads) %>
</div>
</div>
</div>
<%= render :partial => "posts/partials/common/secondary_links" %> <%= render :partial => "posts/partials/common/secondary_links" %>

View File

@@ -16,7 +16,7 @@
<p> <p>
You can <%= link_to "upload another file", new_upload_path %> or <%= link_to "view your current uploads", uploads_path %>. You can <%= link_to "upload another file", new_upload_path %> or <%= link_to "view your current uploads", uploads_path %>.
<% if CurrentUser.user.is_moderator? %> <% if CurrentUser.user.is_moderator? && @upload.is_pending? %>
<%= link_to "Force update", upload_path(@upload, :format => "js"), :remote => true, :method => :put %>. <%= link_to "Force update", upload_path(@upload, :format => "js"), :remote => true, :method => :put %>.
<% end %> <% end %>
</p> </p>

View File

@@ -112,6 +112,11 @@ module Danbooru
200 200
end end
# After this many pages, the paginator will switch to sequential mode.
def max_numbered_pages
2
end
# Max number of tag subscriptions per user # Max number of tag subscriptions per user
def max_tag_subscriptions def max_tag_subscriptions
5 5

View File

@@ -1,5 +1,4 @@
require "danbooru/paginator/active_record_extension" require "danbooru/paginator/active_record_extension"
require "danbooru/paginator/collection_extension"
require "danbooru/paginator/numbered_collection_extension" require "danbooru/paginator/numbered_collection_extension"
require "danbooru/paginator/sequential_collection_extension" require "danbooru/paginator/sequential_collection_extension"

View File

@@ -6,29 +6,29 @@ module Danbooru
extend ActiveSupport::Concern extend ActiveSupport::Concern
module ClassMethods module ClassMethods
def page(p) def paginate(page)
if use_sequential_paginator?(p) if use_sequential_paginator?(page)
paginate_sequential(p) paginate_sequential(page)
else else
paginate_numbered(p) paginate_numbered(page)
end end
end end
def use_sequential_paginator?(p) def use_sequential_paginator?(page)
p =~ /[ab]\d+/i page =~ /[ab]\d+/i
end end
def paginate_sequential(p) def paginate_sequential(page)
if p =~ /b(\d+)/ if page =~ /b(\d+)/
paginate_sequential_before($1) paginate_sequential_before($1)
elsif p =~ /a(\d+)/ elsif page =~ /a(\d+)/
paginate_sequential_after($1) paginate_sequential_after($1)
else else
paginate_numbered(p) paginate_sequential_before
end end
end end
def paginate_sequential_before(before_id) def paginate_sequential_before(before_id = nil)
c = limit(records_per_page) c = limit(records_per_page)
if before_id.to_i > 0 if before_id.to_i > 0
@@ -48,17 +48,17 @@ module Danbooru
end end
end end
def paginate_numbered(p) def paginate_numbered(page)
p = [p.to_i, 1].max page = [page.to_i, 1].max
limit(records_per_page).offset((p - 1) * records_per_page).tap do |obj| limit(records_per_page).offset((page - 1) * records_per_page).tap do |obj|
obj.extend(NumberedCollectionExtension) obj.extend(NumberedCollectionExtension)
obj.total_pages = (obj.total_count / records_per_page.to_f).ceil obj.total_pages = (obj.total_count.to_f / records_per_page).ceil
obj.current_page = p obj.current_page = page
end end
end end
def records_per_page def records_per_page
Danbooru.config.posts_per_p Danbooru.config.posts_per_page
end end
# taken from kaminari (https://github.com/amatsuda/kaminari) # taken from kaminari (https://github.com/amatsuda/kaminari)

View File

@@ -1,6 +0,0 @@
module Danbooru
module Paginator
module CollectionExtension
end
end
end

View File

@@ -3,24 +3,12 @@ module Danbooru
module NumberedCollectionExtension module NumberedCollectionExtension
attr_accessor :current_page, :total_pages attr_accessor :current_page, :total_pages
def self.extended(obj)
obj.extend(Danbooru::Paginator::CollectionExtension)
end
def is_first_page? def is_first_page?
current_page == 1 current_page == 1
end end
def is_last_page? def is_last_page?
current_page == total_pages current_page >= total_pages
end
def is_sequential_paginator?
false
end
def is_numbered_paginator?
true
end end
end end
end end

View File

@@ -3,10 +3,6 @@ module Danbooru
module SequentialCollectionExtension module SequentialCollectionExtension
attr_accessor :sequential_paginator_mode attr_accessor :sequential_paginator_mode
def self.extended(obj)
obj.extend(Danbooru::Paginator::CollectionExtension)
end
def is_first_page? def is_first_page?
size == 0 size == 0
end end
@@ -15,14 +11,6 @@ module Danbooru
size == 0 size == 0
end end
def is_sequential_paginator?
true
end
def is_numbered_paginator?
false
end
def to_a def to_a
if sequential_paginator_mode == :before if sequential_paginator_mode == :before
super super
@@ -30,22 +18,6 @@ module Danbooru
super.reverse super.reverse
end end
end end
def before_id
if size > 0
self[-1].id
else
nil
end
end
def after_id
if size > 0
self[0].id
else
nil
end
end
end end
end end
end end