fixes #1938, Add special random parameter

This commit is contained in:
Toks
2015-05-17 20:06:31 -04:00
parent 0409d6fd69
commit 0d24a45855
3 changed files with 23 additions and 8 deletions

View File

@@ -14,7 +14,7 @@ class PostsController < ApplicationController
redirect_to post_path(@post) redirect_to post_path(@post)
else else
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
@post_set = PostSets::Post.new(tag_query, params[:page], limit, params[:raw]) @post_set = PostSets::Post.new(tag_query, params[:page], limit, params[:raw], params[:random])
@posts = @post_set.posts @posts = @post_set.posts
respond_with(@posts) do |format| respond_with(@posts) do |format|
format.atom format.atom

View File

@@ -1,13 +1,14 @@
module PostSets module PostSets
class Post < PostSets::Base class Post < PostSets::Base
attr_reader :tag_array, :page, :per_page, :raw attr_reader :tag_array, :page, :per_page, :raw, :random
def initialize(tags, page = 1, per_page = nil, raw = false) def initialize(tags, page = 1, per_page = nil, raw = false, random = false)
@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 || CurrentUser.per_page).to_i
@per_page = 200 if @per_page > 200 @per_page = 200 if @per_page > 200
@raw = raw.present? @raw = raw.present?
@random = random.present?
end end
def tag_string def tag_string
@@ -68,12 +69,24 @@ module PostSets
end end
@posts ||= begin @posts ||= begin
if raw if random
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page) count = ::Post.fast_count(tag_string, :statement_timeout => CurrentUser.user.statement_timeout)
temp = []
limit = [per_page, count].min
limit.times do
post = ::Post.tag_match(tag_string).offset(rand(count)).first
if post
temp << post
end
end
else else
temp = ::Post.tag_match(tag_string).paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page) if raw
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page)
else
temp = ::Post.tag_match(tag_string).paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page)
end
temp.each # hack to force rails to eager load
end end
temp.each # hack to force rails to eager load
temp temp
end end
end end

View File

@@ -9,5 +9,7 @@
</div> </div>
<% end %> <% end %>
<%= numbered_paginator(post_set.posts) %> <% unless params[:random].present? %>
<%= numbered_paginator(post_set.posts) %>
<% end %>
</div> </div>