From ed70ace9e7d7c8b64adfb54f14dc7e76a9b5d54f Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Tue, 5 Jan 2016 20:28:20 -0500 Subject: [PATCH 1/5] Add all logs to .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From d34c01fc8065743dfc39d77868d617e8a3c3dc5a Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Tue, 5 Jan 2016 20:29:11 -0500 Subject: [PATCH 2/5] Refactor Question class methods to use database more --- app/models/question.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/models/question.rb b/app/models/question.rb index 4dbbe59..49912cc 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -5,33 +5,39 @@ class Question < ActiveRecord::Base 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 + joins("left join votes on votes.votable_id = questions.id \ + and votes.votable_type = 'Question'") + .group('questions.id').order('sum(votes.value) desc') end def count_votes - votes.map{|vote| vote.value}.reduce(:+) + votes.sum(:value) 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 From 774c7d6b5c06fb67d352add2cd418a45e8a2fe6e Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Tue, 5 Jan 2016 20:50:47 -0500 Subject: [PATCH 3/5] Use database more in answer --- app/models/answer.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/app/models/answer.rb b/app/models/answer.rb index 013e7f5..bf4cbcb 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -6,25 +6,14 @@ 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 + joins(:votes).where(question:question).group('answers.id').order('sum(votes.value) desc').first end def count_votes - votes.map{|vote| vote.value}.reduce(:+) + votes.sum(:value) end end From d7ce3e8e63c1eab384c225d77c26c576472bd076 Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Tue, 5 Jan 2016 20:52:05 -0500 Subject: [PATCH 4/5] Pull count_votes into a Voting module --- app/models/answer.rb | 5 +---- app/models/comment.rb | 7 +------ app/models/concerns/voting.rb | 5 +++++ app/models/question.rb | 5 +---- 4 files changed, 8 insertions(+), 14 deletions(-) create mode 100644 app/models/concerns/voting.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index bf4cbcb..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 @@ -12,8 +13,4 @@ def self.newest_answers def self.top_voted_answer(question) joins(:votes).where(question:question).group('answers.id').order('sum(votes.value) desc').first end - - def count_votes - votes.sum(:value) - 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 49912cc..4acc33c 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,4 +1,5 @@ class Question < ActiveRecord::Base + include Voting belongs_to :user has_many :answers has_many :comments, :as => :commentable @@ -26,10 +27,6 @@ def self.top_voted_questions .group('questions.id').order('sum(votes.value) desc') end - def count_votes - votes.sum(:value) - end - private def self.questions_sorted_by_most_votes From 5c3f0149ce91c76869b2e7e5e50603c09f1e1efd Mon Sep 17 00:00:00 2001 From: Steven Cassidy Date: Tue, 5 Jan 2016 20:52:58 -0500 Subject: [PATCH 5/5] Fix the outlier user voting count --- app/models/user.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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