Merge pull request #3113 from evazion/fix-forum-stickies
Forum topics: don't list sticky topics first in json/atom responses.
This commit is contained in:
@@ -19,8 +19,11 @@ class ForumTopicsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
params[:search] ||= {}
|
||||||
|
params[:search][:order] ||= "sticky" if request.format == Mime::HTML
|
||||||
|
|
||||||
@query = ForumTopic.active.search(params[:search])
|
@query = ForumTopic.active.search(params[:search])
|
||||||
@forum_topics = @query.order("is_sticky DESC, updated_at DESC").paginate(params[:page], :limit => per_page, :search_count => params[:search])
|
@forum_topics = @query.paginate(params[:page], :limit => per_page, :search_count => params[:search])
|
||||||
|
|
||||||
respond_with(@forum_topics) do |format|
|
respond_with(@forum_topics) do |format|
|
||||||
format.html do
|
format.html do
|
||||||
|
|||||||
@@ -68,9 +68,12 @@ class ForumTopic < ActiveRecord::Base
|
|||||||
where("min_level <= ?", CurrentUser.level)
|
where("min_level <= ?", CurrentUser.level)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sticky_first
|
||||||
|
order(is_sticky: :desc, updated_at: :desc)
|
||||||
|
end
|
||||||
|
|
||||||
def search(params)
|
def search(params)
|
||||||
q = permitted
|
q = permitted
|
||||||
return q if params.blank?
|
|
||||||
|
|
||||||
if params[:id].present?
|
if params[:id].present?
|
||||||
q = q.where(id: params[:id].split(",").map(&:to_i))
|
q = q.where(id: params[:id].split(",").map(&:to_i))
|
||||||
@@ -92,6 +95,13 @@ class ForumTopic < ActiveRecord::Base
|
|||||||
q = q.where("title = ?", params[:title])
|
q = q.where("title = ?", params[:title])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
case params[:order]
|
||||||
|
when "sticky"
|
||||||
|
q = q.sticky_first
|
||||||
|
else
|
||||||
|
q = q.order(updated_at: :desc)
|
||||||
|
end
|
||||||
|
|
||||||
q
|
q
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -74,11 +74,23 @@ class ForumTopicsControllerTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "index action" do
|
context "index action" do
|
||||||
|
setup do
|
||||||
|
@topic1 = FactoryGirl.create(:forum_topic, :is_sticky => true, :creator => @user, :original_post_attributes => {:body => "xxx"})
|
||||||
|
@topic2 = FactoryGirl.create(:forum_topic, :creator => @user, :original_post_attributes => {:body => "xxx"})
|
||||||
|
end
|
||||||
|
|
||||||
should "list all forum topics" do
|
should "list all forum topics" do
|
||||||
get :index
|
get :index
|
||||||
assert_response :success
|
assert_response :success
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "not list stickied topics first for JSON responses" do
|
||||||
|
get :index, {format: :json}
|
||||||
|
forum_topics = JSON.parse(response.body)
|
||||||
|
|
||||||
|
assert_equal([@topic2.id, @topic1.id, @forum_topic.id], forum_topics.map {|t| t["id"]})
|
||||||
|
end
|
||||||
|
|
||||||
should "render for atom feed" do
|
should "render for atom feed" do
|
||||||
get :index, {:format => :atom}
|
get :index, {:format => :atom}
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|||||||
Reference in New Issue
Block a user