posts index: clean up limit:<n> / order:random parsing (#2894)

* Move the limit:<n> / order:random metatag parsing from the controller
  to the post set.

* Introduce `Tag.has_metatag?` and use it to parse these metatags
  instead of using a regex (#2894).
This commit is contained in:
evazion
2018-09-20 12:57:43 -05:00
parent 235271706b
commit 6fe883c316
6 changed files with 44 additions and 8 deletions

View File

@@ -1,12 +1,12 @@
module PostSets
class Post < PostSets::Base
attr_reader :tag_array, :page, :per_page, :raw, :random, :post_count, :format, :read_only
MAX_PER_PAGE = 200
attr_reader :tag_array, :page, :raw, :random, :post_count, :format, :read_only
def initialize(tags, page = 1, per_page = nil, options = {})
@tag_array = Tag.scan_query(tags)
@page = page
@per_page = (per_page || CurrentUser.per_page).to_i
@per_page = 200 if @per_page > 200
@per_page = per_page
@raw = options[:raw].present?
@random = options[:random].present?
@format = options[:format] || "html"
@@ -102,6 +102,14 @@ module PostSets
posts.select { |p| p.safeblocked? && !p.levelblocked? && !p.banblocked? }
end
def per_page
(@per_page || Tag.has_metatag?(tag_array, :limit) || CurrentUser.user.per_page).to_i.clamp(0, MAX_PER_PAGE)
end
def is_random?
random || Tag.has_metatag?(tag_array, :order) == "random"
end
def use_sequential_paginator?
unknown_post_count? && !CurrentUser.is_gold?
end
@@ -129,7 +137,7 @@ module PostSets
@posts ||= begin
@post_count = get_post_count()
if random
if is_random?
temp = get_random_posts()
elsif raw
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").where("true /* PostSets::Post#posts:1 */").paginate(page, :count => post_count, :limit => per_page)