diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 79d29553e..682e73b70 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -14,7 +14,7 @@ class PostsController < ApplicationController redirect_to post_path(@post) else 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 respond_with(@posts) do |format| format.atom diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index 2a97afa7a..121fda80d 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -1,13 +1,14 @@ module PostSets 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) @page = page @per_page = (per_page || CurrentUser.per_page).to_i @per_page = 200 if @per_page > 200 @raw = raw.present? + @random = random.present? end def tag_string @@ -68,12 +69,24 @@ module PostSets end @posts ||= begin - if raw - temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page) + if random + 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 - 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 - temp.each # hack to force rails to eager load temp end end diff --git a/app/views/posts/partials/index/_posts.html.erb b/app/views/posts/partials/index/_posts.html.erb index 016c1bb6b..c33b91acd 100644 --- a/app/views/posts/partials/index/_posts.html.erb +++ b/app/views/posts/partials/index/_posts.html.erb @@ -9,5 +9,7 @@ <% end %> - <%= numbered_paginator(post_set.posts) %> + <% unless params[:random].present? %> + <%= numbered_paginator(post_set.posts) %> + <% end %>