diff --git a/.gitignore b/.gitignore index 7ddd068..cf435b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ bin/rspec coverage/ -log/test.log \ No newline at end of file +log diff --git a/app/models/answer.rb b/app/models/answer.rb index 013e7f5..61b4805 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -1,4 +1,5 @@ class Answer < ActiveRecord::Base + include Voting has_many :comments, :as => :commentable has_many :votes, :as => :votable @@ -6,25 +7,10 @@ class Answer < ActiveRecord::Base belongs_to :question def self.newest_answers - - Answer.order(:updated_at).reverse.group_by{|answer| answer.question_id}.map{|group| group[1][0]} - + where('answers.updated_at = (select max(updated_at) from answers a where a.question_id = answers.question_id)') end def self.top_voted_answer(question) - top = question.answers.select{|answer| answer.count_votes.is_a?(Integer)}.sort{|answer| answer.count_votes}.reverse - question.answers.each do |answer| - if top.exclude?(answer) - top << answer - end - end - if top.delete_if{|answer| answer.id == question.accepted_answer_id} - top.unshift(question.answers.select{|answer| answer.id == question.accepted_answer_id}.first) - end - top.compact - end - - def count_votes - votes.map{|vote| vote.value}.reduce(:+) + joins(:votes).where(question:question).group('answers.id').order('sum(votes.value) desc').first end end diff --git a/app/models/comment.rb b/app/models/comment.rb index bb0284f..87ea29f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,11 +1,6 @@ class Comment < ActiveRecord::Base - + include Voting has_many :votes, :as => :votable belongs_to :user belongs_to :commentable, polymorphic: true - - - def count_votes - votes.map{|vote| vote.value}.reduce(:+) - end end diff --git a/app/models/concerns/voting.rb b/app/models/concerns/voting.rb new file mode 100644 index 0000000..5c30c7a --- /dev/null +++ b/app/models/concerns/voting.rb @@ -0,0 +1,5 @@ +module Voting + def count_votes + votes.sum(:value) + end +end \ No newline at end of file diff --git a/app/models/question.rb b/app/models/question.rb index 4dbbe59..4acc33c 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,37 +1,40 @@ class Question < ActiveRecord::Base + include Voting belongs_to :user has_many :answers has_many :comments, :as => :commentable has_many :votes, :as => :votable def find_accepted_answer - self.answers.find(accepted_answer_id) + #maybe return nil rather than throwing error here + self.answers.find_by(id: accepted_answer_id) end def self.most_recent_questions - self.order(:created_at).reverse + self.order(created_at: :desc).reverse end - def self.trending_questions - Answer.newest_answers.map{|answer| answer.question} + def self.trending_questions(limit=20) + joins(:answers) + .where('answers.created_at = (select max(created_at) from answers where question_id = questions.id)') + .order('answers.created_at desc') + .limit(limit) end def self.top_voted_questions - Question.questions_sorted_by_most_votes + Question.questions_with_no_votes - end - - def count_votes - votes.map{|vote| vote.value}.reduce(:+) + joins("left join votes on votes.votable_id = questions.id \ + and votes.votable_type = 'Question'") + .group('questions.id').order('sum(votes.value) desc') end private def self.questions_sorted_by_most_votes - all.select{|question| question.count_votes.is_a?(Integer)}.sort_by{|question| question.count_votes}.reverse + joins(:votes).group('questions.id').order('count(votes.votable_id) desc') end def self.questions_with_no_votes - all.select{|question| question.count_votes == nil} + includes(:votes).where(votes: { :id => nil }) end end diff --git a/app/models/user.rb b/app/models/user.rb index 95a6434..5f69c84 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,11 +14,11 @@ def password_confirmation_matches end end + # This should probably be count_votes and covered by the voting module + # I can't see that you call this one anywhere def vote_count - votes_for.map{|vote| vote.value}.reduce(:+) + votes.sum(:value) end - - has_secure_password end