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.
40 lines
944 B
Ruby
40 lines
944 B
Ruby
class CommunitiesController < ApplicationController
|
|
TOPIC_ORDERS = %w{newest most_commented oldest}.freeze
|
|
before_action :set_order, :set_community, :load_topics, :load_participants
|
|
|
|
has_orders TOPIC_ORDERS
|
|
|
|
skip_authorization_check
|
|
|
|
def show
|
|
raise ActionController::RoutingError, 'Not Found' unless communitable_exists?
|
|
redirect_to root_path if Setting['feature.community'].blank?
|
|
end
|
|
|
|
private
|
|
|
|
def set_order
|
|
@order = valid_order? ? params[:order] : "newest"
|
|
end
|
|
|
|
def set_community
|
|
@community = Community.find(params[:id])
|
|
end
|
|
|
|
def load_topics
|
|
@topics = @community.topics.send("sort_by_#{@order}").page(params[:page])
|
|
end
|
|
|
|
def load_participants
|
|
@participants = @community.participants
|
|
end
|
|
|
|
def valid_order?
|
|
params[:order].present? && TOPIC_ORDERS.include?(params[:order])
|
|
end
|
|
|
|
def communitable_exists?
|
|
@community.proposal.present? || @community.investment.present?
|
|
end
|
|
end
|