make Comment#ordered_for_story_or_thread_for_user work on query scope

This commit is contained in:
Serge Paquet 2014-01-07 17:58:22 -05:00
parent 4ff4d53ae9
commit 5988038071
5 changed files with 23 additions and 19 deletions

View file

@ -232,7 +232,13 @@ class CommentsController < ApplicationController
end
@threads = @showing_user.recent_threads(20).map{|r|
cs = Comment.ordered_for_story_or_thread_for_user(nil, r, @showing_user)
cs = Comment.where(
:thread_id => r
).includes(
:user, :story
).arrange_for_user(
@showing_user
)
if @user && (@showing_user.id == @user.id)
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id,

View file

@ -126,8 +126,7 @@ class StoriesController < ApplicationController
@short_url = @story.short_id_url
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id, nil,
@user)
@comments = @story.comments.includes(:user).arrange_for_user(@user)
respond_to do |format|
format.html {
@ -155,8 +154,11 @@ class StoriesController < ApplicationController
return redirect_to @story.comments_url
end
@comments = Comment.ordered_for_story_or_thread_for_user(@story.id,
@showing_comment.thread_id, @user ? @user : nil)
@comments = @story.comments
if @showing_comment.thread_id
@comments = @comments.where(:thread_id => @showing_comment.thread_id)
end
@comments = @comments.includes(:user).arrange_for_user(@user)
@comments.each do |c,x|
if c.id == @showing_comment.id

View file

@ -1,6 +1,7 @@
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :story
belongs_to :story,
:inverse_of => :comments
has_many :votes,
:dependent => :delete_all
belongs_to :parent_comment,
@ -293,16 +294,10 @@ class Comment < ActiveRecord::Base
comment
end
def self.ordered_for_story_or_thread_for_user(story_id, thread_id, user)
def self.arrange_for_user(user)
parents = {}
if thread_id
cs = [ "thread_id = ?", thread_id ]
else
cs = [ "story_id = ?", story_id ]
end
Comment.where(*cs).order("confidence DESC").includes(:user).each do |c|
self.order("confidence DESC").each do |c|
(parents[c.parent_comment_id.to_i] ||= []).push c
end

View file

@ -2,7 +2,8 @@ class Story < ActiveRecord::Base
belongs_to :user
has_many :taggings,
:autosave => true
has_many :comments
has_many :comments,
:inverse_of => :story
has_many :tags, :through => :taggings
validates_length_of :title, :in => 3..150
@ -381,9 +382,9 @@ class Story < ActiveRecord::Base
end
def update_comments_count!
comments = self.comments.arrange_for_user(nil)
# calculate count after removing deleted comments and threads
self.update_column :comments_count,
Comment.ordered_for_story_or_thread_for_user(self.id, nil, nil).select{|c|
!c.is_gone? }.count
self.update_column :comments_count, comments.count{|c| !c.is_gone? }
end
end

View file

@ -153,7 +153,7 @@ last_comment_id, false, false).order(:id).each do |c|
thread = []
indent_level = 0
Comment.ordered_for_story_or_thread_for_user(nil, c.thread_id,
Comment.where(:thread_id => c.thread_id).arrange_for_user(
nil).reverse.each do |cc|
if indent_level > 0 && cc.indent_level < indent_level
thread.unshift cc