work
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user