Why: Somehow we're seeing communities without proposals at production. We must find why and fix it, but first we need to throw a 404 at the user instead of a 500 internal server error How: First catching the scenario of non-existent communitable at the controller and raising a 404 error. Secondly preventing the author_id access over a possibly nil object, this is a smell but it can't be easily fixed right now... we need to correctly implement a relation between Community and communitable and avoid the multiple occurences of `community.from_proposal?` in the codebase that makes it impossible to extend to a fourth communitable model.
35 lines
830 B
Ruby
35 lines
830 B
Ruby
class Community < ActiveRecord::Base
|
|
has_one :proposal
|
|
has_one :investment, class_name: Budget::Investment
|
|
has_many :topics
|
|
|
|
def participants
|
|
users_participants = users_who_commented +
|
|
users_who_topics_author +
|
|
author_from_community
|
|
users_participants.uniq
|
|
end
|
|
|
|
def from_proposal?
|
|
proposal.present?
|
|
end
|
|
|
|
private
|
|
|
|
def users_who_commented
|
|
topics_ids = topics.pluck(:id)
|
|
query = "comments.commentable_id IN (?)and comments.commentable_type = 'Topic'"
|
|
User.by_comments(query, topics_ids)
|
|
end
|
|
|
|
def users_who_topics_author
|
|
author_ids = topics.pluck(:author_id)
|
|
User.by_authors(author_ids)
|
|
end
|
|
|
|
def author_from_community
|
|
from_proposal? ? User.where(id: proposal&.author_id) : User.where(id: investment&.author_id)
|
|
end
|
|
|
|
end
|