Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bin/rspec
coverage/
log/test.log
log
20 changes: 3 additions & 17 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
class Answer < ActiveRecord::Base
include Voting

has_many :comments, :as => :commentable
has_many :votes, :as => :votable
belongs_to :user
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
7 changes: 1 addition & 6 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/models/concerns/voting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Voting
def count_votes
votes.sum(:value)
end
end
25 changes: 14 additions & 11 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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