work
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -19,5 +19,4 @@ gem "simple_form"
|
||||
gem "mechanize"
|
||||
gem "nokogiri"
|
||||
gem "meta_search", :git => "git://github.com/ernie/meta_search.git"
|
||||
gem "will_paginate", :git => "git://github.com/mmack/will_paginate.git", :branch => "rails3.1"
|
||||
gem "silent-postgres"
|
||||
|
||||
@@ -7,13 +7,6 @@ GIT
|
||||
activerecord (~> 3.1.0.alpha)
|
||||
activesupport (~> 3.1.0.alpha)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/mmack/will_paginate.git
|
||||
revision: 5816b4e8d4382b4b167621a9aea4b8fe72983c37
|
||||
branch: rails3.1
|
||||
specs:
|
||||
will_paginate (3.1.0)
|
||||
|
||||
GIT
|
||||
remote: http://github.com/EmmanuelOga/ffaker.git
|
||||
revision: 52feff4ecddbe8834b63beb122b153b65833e309
|
||||
@@ -143,4 +136,3 @@ DEPENDENCIES
|
||||
simple_form
|
||||
simplecov
|
||||
super_exception_notifier
|
||||
will_paginate!
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
class PostVersionsController < ApplicationController
|
||||
def index
|
||||
@search = PostVersion.search(params[:search])
|
||||
@post_versions = @search.paginate(:page => params[:page], :order => "updated_at DESC")
|
||||
@search = PostVersion.search(params[:search]).paginate(params[:paginate])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,9 +4,8 @@ class PostsController < ApplicationController
|
||||
respond_to :html, :xml, :json
|
||||
|
||||
def index
|
||||
@post_set = PostSets::Base.new(params)
|
||||
extend_post_set(@post_set)
|
||||
respond_with(@post_set)
|
||||
@posts = Post.search(params[:search]).paginate(params[:page])
|
||||
respond_with(@posts)
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
@@ -1,64 +1,61 @@
|
||||
module PaginationHelper
|
||||
def smart_paginator(set, &block)
|
||||
if params[:page] && set.page > 1000
|
||||
set.extend(PostSets::Sequential)
|
||||
sequential_paginator(set)
|
||||
def smart_paginator(records, &block)
|
||||
if records.is_sequential_paginator? || params[:page].to_i > 200
|
||||
sequential_paginator(records)
|
||||
else
|
||||
set.extend(PostSets::Numbered)
|
||||
numbered_paginator(set, &block)
|
||||
numbered_paginator(records, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def sequential_paginator(set)
|
||||
def sequential_paginator(records)
|
||||
html = "<menu>"
|
||||
|
||||
unless set.is_first_page?
|
||||
html << '<li>' + link_to("« Previous", params.merge(:after_id => set.first_id)) + '</li>'
|
||||
unless records.is_first_page?
|
||||
html << '<li>' + link_to("« Previous", params.merge(:after_id => records.first_id)) + '</li>'
|
||||
end
|
||||
|
||||
unless set.is_last_page?
|
||||
html << '<li>' + link_to("Next »", params.merge(:before_id => set.last_id)) + '</li>'
|
||||
unless records.is_last_page?
|
||||
html << '<li>' + link_to("Next »", params.merge(:before_id => records.last_id)) + '</li>'
|
||||
end
|
||||
|
||||
html << "</menu>"
|
||||
html.html_safe
|
||||
end
|
||||
|
||||
def numbered_paginator(set, &block)
|
||||
def numbered_paginator(records, &block)
|
||||
html = "<menu>"
|
||||
window = 3
|
||||
if set.total_pages <= (window * 2) + 5
|
||||
1.upto(set.total_pages) do |page|
|
||||
html << numbered_paginator_item(page, set.current_page, &block)
|
||||
if records.total_pages <= (window * 2) + 5
|
||||
1.upto(records.total_pages) do |page|
|
||||
html << numbered_paginator_item(page, records.current_page, &block)
|
||||
end
|
||||
elsif set.current_page <= window + 2
|
||||
1.upto(set.current_page + window) do |page|
|
||||
html << numbered_paginator_item(page, set.current_page, &block)
|
||||
elsif records.current_page <= window + 2
|
||||
1.upto(records.current_page + window) do |page|
|
||||
html << numbered_paginator_item(page, records.current_page, &block)
|
||||
end
|
||||
html << numbered_paginator_item("...", set.current_page, &block)
|
||||
html << numbered_paginator_final_item(set.total_pages, set.current_page, &block)
|
||||
|
||||
elsif set.current_page >= set.total_pages - (window + 1)
|
||||
html << numbered_paginator_item(1, set.current_page, &block)
|
||||
html << numbered_paginator_item("...", set.current_page, &block)
|
||||
(set.current_page - window).upto(set.total_pages) do |page|
|
||||
html << numbered_paginator_item(page, set.current_page, &block)
|
||||
html << numbered_paginator_item("...", records.current_page, &block)
|
||||
html << numbered_paginator_final_item(records.total_pages, records.current_page, &block)
|
||||
elsif records.current_page >= records.total_pages - (window + 1)
|
||||
html << numbered_paginator_item(1, records.current_page, &block)
|
||||
html << numbered_paginator_item("...", records.current_page, &block)
|
||||
(records.current_page - window).upto(records.total_pages) do |page|
|
||||
html << numbered_paginator_item(page, records.current_page, &block)
|
||||
end
|
||||
else
|
||||
html << numbered_paginator_item(1, set.current_page, &block)
|
||||
html << numbered_paginator_item("...", set.current_page, &block)
|
||||
(set.current_page - window).upto(set.current_page + window) do |page|
|
||||
html << numbered_paginator_item(page, set.current_page, &block)
|
||||
html << numbered_paginator_item(1, records.current_page, &block)
|
||||
html << numbered_paginator_item("...", records.current_page, &block)
|
||||
(records.current_page - window).upto(records.current_page + window) do |page|
|
||||
html << numbered_paginator_item(page, records.current_page, &block)
|
||||
end
|
||||
html << numbered_paginator_item("...", set.current_page, &block)
|
||||
html << numbered_paginator_final_item(set.total_pages, set.current_page, &block)
|
||||
html << numbered_paginator_item("...", records.current_page, &block)
|
||||
html << numbered_paginator_final_item(records.total_pages, records.current_page, &block)
|
||||
end
|
||||
html << "</menu>"
|
||||
html.html_safe
|
||||
end
|
||||
|
||||
def numbered_paginator_final_item(total_pages, current_page, &block)
|
||||
if total_pages <= 1000
|
||||
if total_pages <= 200
|
||||
numbered_paginator_item(total_pages, current_page, &block)
|
||||
else
|
||||
""
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
# A PostSet represents a paginated slice of posts. It is used in conjunction
|
||||
# with the helpers to render the paginator.
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# @post_set = PostSets::Base.new(params)
|
||||
# @post_set.extend(PostSets::Sequential)
|
||||
# @post_set.extend(PostSets::Post)
|
||||
|
||||
module PostSets
|
||||
class Base
|
||||
attr_reader :params, :posts
|
||||
delegate :to_xml, :to_json, :to => :posts
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
# Should a return a paginated array of posts. This means it should have
|
||||
# at most <limit> elements.
|
||||
def posts
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# Does this post set have a valid wiki page representation?
|
||||
def has_wiki?
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# Should return an array of strings representing the tags.
|
||||
def tags
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# Given an ActiveRelation object, perform the necessary pagination to
|
||||
# extract at most <limit> elements. Should return an array.
|
||||
def slice(relation)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# For cases where we're not relying on the default pagination
|
||||
# implementation (for example, if the ids are cached in a string)
|
||||
# then pass in the offset/before_id/after_id parameters here.
|
||||
def pagination_options
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# This method should throw an exception if for whatever reason the query
|
||||
# is invalid or forbidden.
|
||||
def validate
|
||||
end
|
||||
|
||||
# Clear out any memoized instance variables.
|
||||
def reload
|
||||
@posts = nil
|
||||
@presenter = nil
|
||||
@tag_string = nil
|
||||
end
|
||||
|
||||
def tag_string
|
||||
@tag_string ||= tags.join(" ")
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
posts.size == 0
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
module PostSets
|
||||
class Error < Exception
|
||||
end
|
||||
end
|
||||
@@ -1,33 +0,0 @@
|
||||
module PostSets
|
||||
module Favorite
|
||||
def user
|
||||
@user ||= ::User.find(params[:id])
|
||||
end
|
||||
|
||||
def tags
|
||||
@tags ||= ["fav:#{user.name}"]
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
false
|
||||
end
|
||||
|
||||
def reload
|
||||
super
|
||||
@user = nil
|
||||
@count = nil
|
||||
end
|
||||
|
||||
def count
|
||||
@count ||= relation.count
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= slice(relation).map(&:post)
|
||||
end
|
||||
|
||||
def relation
|
||||
::Favorite.model_for(user.id).where("user_id = ?", user.id).includes(:post).order("id desc")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,51 +0,0 @@
|
||||
module PostSets
|
||||
module Numbered
|
||||
def total_pages
|
||||
@total_pages ||= (count / limit.to_f).ceil.to_i
|
||||
end
|
||||
|
||||
def reload
|
||||
super
|
||||
@total_pages = nil
|
||||
end
|
||||
|
||||
def slice(relation)
|
||||
relation.offset(offset).limit(limit).all
|
||||
end
|
||||
|
||||
def pagination_options
|
||||
{:offset => offset}
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
offset == 0
|
||||
end
|
||||
|
||||
def validate
|
||||
super
|
||||
validate_page
|
||||
end
|
||||
|
||||
def validate_page
|
||||
if page > 1_000
|
||||
raise Error.new("You cannot explicitly specify the page after page 1000")
|
||||
end
|
||||
end
|
||||
|
||||
def page
|
||||
@page ||= params[:page] ? params[:page].to_i : 1
|
||||
end
|
||||
|
||||
def total_pages
|
||||
(count / limit.to_f).ceil
|
||||
end
|
||||
|
||||
def current_page
|
||||
page
|
||||
end
|
||||
|
||||
def offset
|
||||
((page < 1) ? 0 : (page - 1)) * limit
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,30 +0,0 @@
|
||||
# This only works with the numbered paginator because of the way
|
||||
# the association is stored.
|
||||
module PostSets
|
||||
module Pool
|
||||
def pool
|
||||
@pool ||= ::Pool.find(params[:id])
|
||||
end
|
||||
|
||||
def tags
|
||||
["pool:#{pool.name}"]
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
true
|
||||
end
|
||||
|
||||
def count
|
||||
pool.post_count
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= pool.posts(pagination_options.merge(:limit => limit)).all
|
||||
end
|
||||
|
||||
def reload
|
||||
super
|
||||
@pool = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,63 +0,0 @@
|
||||
module PostSets
|
||||
module Post
|
||||
def tag_string
|
||||
@tag_string ||= params[:tags].to_s.downcase
|
||||
end
|
||||
|
||||
def count
|
||||
@count ||= ::Post.fast_count(tag_string)
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= slice(::Post.tag_match(tag_string))
|
||||
end
|
||||
|
||||
def reload
|
||||
super
|
||||
@tag_array = nil
|
||||
@tag_string = nil
|
||||
@count = nil
|
||||
@wiki_page = nil
|
||||
@artist = nil
|
||||
end
|
||||
|
||||
def wiki_page
|
||||
@wiki_page ||= ::WikiPage.titled(tag_string).first
|
||||
end
|
||||
|
||||
def artist
|
||||
@artist ||= ::Artist.find_by_name(tag_string)
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
is_single_tag?
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= ::Tag.scan_query(tag_string)
|
||||
end
|
||||
|
||||
def tags
|
||||
tag_array
|
||||
end
|
||||
|
||||
def validate
|
||||
super
|
||||
validate_query_count
|
||||
end
|
||||
|
||||
def validate_query_count
|
||||
if !CurrentUser.is_privileged? && tag_array.size > 2
|
||||
raise Error.new("You can only search up to two tags at once with a basic account")
|
||||
end
|
||||
|
||||
if tag_array.size > 6
|
||||
raise Error.new("You can only search up to six tags at once")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,45 +0,0 @@
|
||||
module PostSets
|
||||
module Sequential
|
||||
def before_id
|
||||
params[:before_id]
|
||||
end
|
||||
|
||||
def after_id
|
||||
params[:after_id]
|
||||
end
|
||||
|
||||
def slice(relation)
|
||||
if before_id
|
||||
relation.where("id < ?", before_id).order("id desc").limit(limit).all
|
||||
elsif after_id
|
||||
relation.where("id > ?", after_id).order("id asc").limit(limit).all.reverse
|
||||
else
|
||||
relation.limit(limit).all
|
||||
end
|
||||
end
|
||||
|
||||
def first_id
|
||||
if posts.any?
|
||||
posts.first.id
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def last_id
|
||||
if posts.any?
|
||||
posts.last.id
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def pagination_options
|
||||
{:before_id => before_id, :after_id => after_id}
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
before_id.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,29 +0,0 @@
|
||||
module PostSets
|
||||
module WikiPage
|
||||
def wiki_page
|
||||
@wiki_page ||= begin
|
||||
if params[:id]
|
||||
::WikiPage.find(params[:id])
|
||||
elsif params[:tags]
|
||||
::WikiPage.titled(params[:tags]).first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
true
|
||||
end
|
||||
|
||||
def tags
|
||||
@tags ||= ::Tag.scan_query(wiki_page.title)
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= slice(::Post.tag_match(tag_string))
|
||||
end
|
||||
|
||||
def count
|
||||
@count ||= ::Post.fast_count(tag_string)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -39,11 +39,23 @@ class Post < ActiveRecord::Base
|
||||
scope :for_user, lambda {|user_id| where(["uploader_string = ?", "uploader:#{user_id}"])}
|
||||
scope :available_for_moderation, lambda {where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :before_id, lambda {|id| id.present? ? where(["posts.id < ?", id]) : where("TRUE")}
|
||||
scope :after_id, lambda {|id| id.present? ? where("posts.id > ?", id) : where("true")}
|
||||
scope :tag_match, lambda {|query| Post.tag_match_helper(query)}
|
||||
search_methods :tag_match
|
||||
|
||||
scope :after_id, Proc.new {|num|
|
||||
if num.present?
|
||||
where("id > ?", num.to_i).reorder("id asc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
}
|
||||
scope :before_id, Proc.new {|num|
|
||||
if num.present?
|
||||
where("id < ?", num.to_i).reorder("id desc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
}
|
||||
|
||||
module FileMethods
|
||||
def delete_files
|
||||
FileUtils.rm_f(file_path)
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
require 'pp'
|
||||
|
||||
class PostSetPresenter < Presenter
|
||||
attr_accessor :post_set, :tag_set_presenter
|
||||
attr_accessor :posts, :tag_set_presenter
|
||||
|
||||
def initialize(post_set)
|
||||
@post_set = post_set
|
||||
def initialize(posts)
|
||||
@posts = posts
|
||||
@tag_set_presenter = TagSetPresenter.new(RelatedTagCalculator.calculate_from_sample_to_array(@post_set.tags).map {|x| x[0]})
|
||||
end
|
||||
|
||||
def posts
|
||||
post_set.posts
|
||||
end
|
||||
|
||||
def tag_list_html(template)
|
||||
tag_set_presenter.tag_list_html(template)
|
||||
end
|
||||
|
||||
@@ -1,81 +1,57 @@
|
||||
<div>
|
||||
<div style="margin-bottom: 1em;">
|
||||
<div class="history-search-row">
|
||||
<div class="history-search-label">
|
||||
<label for="user_name">User</label>
|
||||
</div>
|
||||
<div>
|
||||
<%= form_tag(post_versions_path, :method => :get) do %>
|
||||
<%= text_field_tag "user_name", params[:user_name], :id => "user_name", :size => 20 %> <%= submit_tag "Search" %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div id="c-post-versions">
|
||||
<div id="a-index">
|
||||
<div id="search">
|
||||
<%= simple_form_for(@search) do |f| %>
|
||||
<%= f.input :updater_name_eq, "User" %>
|
||||
<%= f.input :post_id_eq, :label => "Post" %>
|
||||
<%= f.button :submit %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="history-search-row">
|
||||
<div class="history-search-label">
|
||||
<label for="post_id">Post ID</label>
|
||||
</div>
|
||||
<div>
|
||||
<%= form_tag(post_versions_path, :method => :get) do %>
|
||||
<%= text_field_tag "post_id", params[:post_id], :id => "post_id", :size => 10 %> <%= submit_tag "Search" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div style="clear: left;">
|
||||
<table width="100%" class="highlightable" id="history">
|
||||
<thead>
|
||||
<tr>
|
||||
<th width="1%"></th>
|
||||
<th width="4%">Post</th>
|
||||
<th width="15%">Date</th>
|
||||
<th width="10%">User</th>
|
||||
<th width="5%">Rating</th>
|
||||
<th width="5%">Parent</th>
|
||||
<th width="10%">IP Address</th>
|
||||
<th width="50%">Tags</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="8">
|
||||
<%= button_to_function "Undo", "PostTagHistory.undo()", :id => "undo" %>
|
||||
<%= button_to_function "Revert to", "PostTagHistory.revert()", :id => "revert" %>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
<tbody>
|
||||
<% @change_list.each do |change| %>
|
||||
<tr class="<%= cycle 'even', 'odd' %>" id="r<%= change[:change].id %>" >
|
||||
<td style="background: <%= id_to_color(change[:change].post_id) %>;"></td>
|
||||
<td><%= link_to change[:change].post_id, :controller => "post", :action => "show", :id => change[:change].post_id %></td>
|
||||
<td><%= change[:change].created_at.strftime("%Y-%m-%d %H:%M") %></td>
|
||||
<td><%= link_to change[:change].author, :controller => "user", :action => "show", :id => change[:change].user_id %></td>
|
||||
<td><%= change[:change].rating %></td>
|
||||
<td><%= change[:parent_id] %></td>
|
||||
<td>
|
||||
<% if @current_user.is_admin? %>
|
||||
<%= change[:ip_addr] %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<span class="added-tags"><%= tag_list(change[:added_tags], :obsolete => change[:obsolete_added_tags], :prefix => "+") %></span>
|
||||
<span class="removed-tags"><%= tag_list(change[:removed_tags], :obsolete=>change[:obsolete_removed_tags], :prefix=>"-") %></span>
|
||||
<span class="unchanged-tags"><%= tag_list(change[:unchanged_tags], :prefix => "") %></span>
|
||||
</td>
|
||||
|
||||
<div class="listing">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th width="5%">Post</th>
|
||||
<th width="15%">Date</th>
|
||||
<th width="10%">User</th>
|
||||
<th width="5%">Rating</th>
|
||||
<th width="5%">Parent</th>
|
||||
<th width="10%">IP Address</th>
|
||||
<th width="50%">Tags</th>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @post_versions.each do |post_version| %>
|
||||
<tr id="post-version-<%= post_version.id %>">
|
||||
<td><%= link_to(post_version.post_id, post_path(post_version.post_id)) %></td>
|
||||
<td><%= post_version.updated_at.strftime("%Y-%m-%d %H:%M") %></td>
|
||||
<td><%= link_to(post_version.updater.name, user_path(post_version.updater_id)) %></td>
|
||||
<td><%= post_version.rating %></td>
|
||||
<td><%= post_version.parent_id %></td>
|
||||
<td>
|
||||
<% if CurrentUser.is_admin? %>
|
||||
<%= post_version.updater_ip_addr %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% post_version.add_tag_array.each do |tag| %>
|
||||
<ins><%= tag %></ins>
|
||||
<% end %>
|
||||
|
||||
<% post_version.del_tag_array.each do |tag| %>
|
||||
<del><%= tag %></del>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="paginator">
|
||||
<%= sequential_paginator(@post_versions) %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
<% @change_list.each do |change| %>
|
||||
PostTagHistory.add_change(<%= change[:change].id %>, <%= change[:change].post_id %>, '<%= escape_javascript(change[:change].author) %>')
|
||||
<% end %>
|
||||
PostTagHistory.init()
|
||||
</script>
|
||||
|
||||
<div id="paginator">
|
||||
<%= tag_history_pagination_links(@changes) %>
|
||||
</div>
|
||||
|
||||
1
config/initializers/lib.rb
Normal file
1
config/initializers/lib.rb
Normal file
@@ -0,0 +1 @@
|
||||
require 'danbooru/paginator'
|
||||
6
lib/danbooru/paginator.rb
Normal file
6
lib/danbooru/paginator.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
require "danbooru/paginator/active_record_extension"
|
||||
require "danbooru/paginator/collection_extension"
|
||||
require "danbooru/paginator/numbered_collection_extension"
|
||||
require "danbooru/paginator/sequential_collection_extension"
|
||||
|
||||
ActiveRecord::Base.__send__(:include, Danbooru::Paginator::ActiveRecordExtension)
|
||||
68
lib/danbooru/paginator/active_record_extension.rb
Normal file
68
lib/danbooru/paginator/active_record_extension.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
require 'active_support/concern'
|
||||
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module ActiveRecordExtension
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def paginate(page)
|
||||
if use_sequential_paginator?(page)
|
||||
paginate_sequential(page)
|
||||
else
|
||||
paginate_numbered(page)
|
||||
end
|
||||
end
|
||||
|
||||
def use_sequential_paginator?(page)
|
||||
page =~ /[ab]\d+/i
|
||||
end
|
||||
|
||||
def paginate_sequential(page)
|
||||
if page =~ /b(\d+)/
|
||||
paginate_sequential_before($1)
|
||||
elsif page =~ /a(\d+)/
|
||||
paginate_sequential_after($1)
|
||||
else
|
||||
paginate_numbered(page)
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_sequential_before(before_id)
|
||||
limit(records_per_page).where("id < ?", before_id.to_i).reorder("id desc").tap do |obj|
|
||||
obj.extend(SequentialCollectionExtension)
|
||||
obj.sequential_paginator_mode = :before
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_sequential_after(after_id)
|
||||
limit(records_per_page).where("id > ?", after_id.to_i).reorder("id asc").tap do |obj|
|
||||
obj.extend(SequentialCollectionExtension)
|
||||
obj.sequential_paginator_mode = :after
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_numbered(page)
|
||||
page = [page.to_i, 1].max
|
||||
limit(records_per_page).offset((page - 1) * records_per_page).tap do |obj|
|
||||
obj.extend(NumberedCollectionExtension)
|
||||
obj.total_pages = (obj.total_count / records_per_page.to_f).ceil
|
||||
obj.current_page = page
|
||||
end
|
||||
end
|
||||
|
||||
def records_per_page
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
# taken from kaminari (https://github.com/amatsuda/kaminari)
|
||||
def total_count
|
||||
c = except(:offset, :limit, :order)
|
||||
c = c.reorder(nil)
|
||||
c = c.count
|
||||
c.respond_to?(:count) ? c.count : c
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/danbooru/paginator/collection_extension.rb
Normal file
6
lib/danbooru/paginator/collection_extension.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module CollectionExtension
|
||||
end
|
||||
end
|
||||
end
|
||||
27
lib/danbooru/paginator/numbered_collection_extension.rb
Normal file
27
lib/danbooru/paginator/numbered_collection_extension.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module NumberedCollectionExtension
|
||||
attr_accessor :current_page, :total_pages
|
||||
|
||||
def self.extended(obj)
|
||||
obj.extend(Danbooru::Paginator::CollectionExtension)
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
current_page == 1
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
current_page == total_pages
|
||||
end
|
||||
|
||||
def is_sequential_paginator?
|
||||
false
|
||||
end
|
||||
|
||||
def is_numbered_paginator?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
51
lib/danbooru/paginator/sequential_collection_extension.rb
Normal file
51
lib/danbooru/paginator/sequential_collection_extension.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module SequentialCollectionExtension
|
||||
attr_accessor :sequential_paginator_mode
|
||||
|
||||
def self.extended(obj)
|
||||
obj.extend(Danbooru::Paginator::CollectionExtension)
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
size == 0
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
size == 0
|
||||
end
|
||||
|
||||
def is_sequential_paginator?
|
||||
true
|
||||
end
|
||||
|
||||
def is_numbered_paginator?
|
||||
false
|
||||
end
|
||||
|
||||
def to_a
|
||||
if sequential_paginator_mode == :before
|
||||
super
|
||||
else
|
||||
super.reverse
|
||||
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
|
||||
Reference in New Issue
Block a user