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) }
|
format.html { redirect_to(@post) }
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
|
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], raw: params[:raw], random: params[:random], format: params[:format], read_only: params[:ro])
|
||||||
@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])
|
|
||||||
@posts = @post_set.posts
|
@posts = @post_set.posts
|
||||||
respond_with(@posts) do |format|
|
respond_with(@posts) do |format|
|
||||||
format.atom
|
format.atom
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
module PostSets
|
module PostSets
|
||||||
class Post < PostSets::Base
|
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 = {})
|
def initialize(tags, page = 1, per_page = nil, options = {})
|
||||||
@tag_array = Tag.scan_query(tags)
|
@tag_array = Tag.scan_query(tags)
|
||||||
@page = page
|
@page = page
|
||||||
@per_page = (per_page || CurrentUser.per_page).to_i
|
@per_page = per_page
|
||||||
@per_page = 200 if @per_page > 200
|
|
||||||
@raw = options[:raw].present?
|
@raw = options[:raw].present?
|
||||||
@random = options[:random].present?
|
@random = options[:random].present?
|
||||||
@format = options[:format] || "html"
|
@format = options[:format] || "html"
|
||||||
@@ -102,6 +102,14 @@ module PostSets
|
|||||||
posts.select { |p| p.safeblocked? && !p.levelblocked? && !p.banblocked? }
|
posts.select { |p| p.safeblocked? && !p.levelblocked? && !p.banblocked? }
|
||||||
end
|
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?
|
def use_sequential_paginator?
|
||||||
unknown_post_count? && !CurrentUser.is_gold?
|
unknown_post_count? && !CurrentUser.is_gold?
|
||||||
end
|
end
|
||||||
@@ -129,7 +137,7 @@ module PostSets
|
|||||||
@posts ||= begin
|
@posts ||= begin
|
||||||
@post_count = get_post_count()
|
@post_count = get_post_count()
|
||||||
|
|
||||||
if random
|
if is_random?
|
||||||
temp = get_random_posts()
|
temp = get_random_posts()
|
||||||
elsif raw
|
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)
|
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?("*")
|
tag.include?("*")
|
||||||
end
|
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 = {})
|
def parse_query(query, options = {})
|
||||||
q = {}
|
q = {}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% unless @random.present? %>
|
<% unless post_set.is_random? %>
|
||||||
<%= numbered_paginator(post_set.posts) %>
|
<%= numbered_paginator(post_set.posts) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -89,6 +89,16 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
assert_redirected_to(@post)
|
assert_redirected_to(@post)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with a random search" do
|
||||||
|
should "render" do
|
||||||
|
get posts_path, params: { tags: "order:random" }
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
get posts_path, params: { random: "1" }
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "show_seq action" do
|
context "show_seq action" do
|
||||||
|
|||||||
@@ -161,6 +161,19 @@ module PostSets
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "#per_page method" do
|
||||||
|
should "take the limit from the params first, then the limit:<n> metatag, then the account settings" do
|
||||||
|
set = PostSets::Post.new("a limit:23 b", 1, 42)
|
||||||
|
assert_equal(42, set.per_page)
|
||||||
|
|
||||||
|
set = PostSets::Post.new("a limit:23 b", 1, nil)
|
||||||
|
assert_equal(23, set.per_page)
|
||||||
|
|
||||||
|
set = PostSets::Post.new("a", 1, nil)
|
||||||
|
assert_equal(CurrentUser.user.per_page, set.per_page)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user