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:
@@ -10,9 +10,7 @@ class PostsController < ApplicationController
|
||||
format.html { redirect_to(@post) }
|
||||
end
|
||||
else
|
||||
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
|
||||
@random = params[:random] || params[:tags] =~ /(?:^|\s)order:random(?:$|\s)/
|
||||
@post_set = PostSets::Post.new(tag_query, params[:page], limit, raw: params[:raw], random: @random, format: params[:format], read_only: params[:ro])
|
||||
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], raw: params[:raw], random: params[:random], format: params[:format], read_only: params[:ro])
|
||||
@posts = @post_set.posts
|
||||
respond_with(@posts) do |format|
|
||||
format.atom
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -445,6 +445,13 @@ class Tag < ApplicationRecord
|
||||
tag.include?("*")
|
||||
end
|
||||
|
||||
def has_metatag?(tags, *metatags)
|
||||
return nil if tags.blank?
|
||||
|
||||
tags = scan_query(tags.to_str) if tags.respond_to?(:to_str)
|
||||
tags.grep(/\A#{Regexp.union(metatags.map(&:to_s))}:(.+)\z/i) { $1 }.first
|
||||
end
|
||||
|
||||
def parse_query(query, options = {})
|
||||
q = {}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% unless @random.present? %>
|
||||
<% unless post_set.is_random? %>
|
||||
<%= numbered_paginator(post_set.posts) %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user