Return 404 accesing community without communitable
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.
This commit is contained in:
@@ -7,6 +7,7 @@ class CommunitiesController < ApplicationController
|
|||||||
skip_authorization_check
|
skip_authorization_check
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
raise ActionController::RoutingError, 'Not Found' unless communitable_exists?
|
||||||
redirect_to root_path if Setting['feature.community'].blank?
|
redirect_to root_path if Setting['feature.community'].blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -31,4 +32,8 @@ class CommunitiesController < ApplicationController
|
|||||||
def valid_order?
|
def valid_order?
|
||||||
params[:order].present? && TOPIC_ORDERS.include?(params[:order])
|
params[:order].present? && TOPIC_ORDERS.include?(params[:order])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def communitable_exists?
|
||||||
|
@community.proposal.present? || @community.investment.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class Community < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def author_from_community
|
def author_from_community
|
||||||
from_proposal? ? User.where(id: proposal.author_id) : User.where(id: investment.author_id)
|
from_proposal? ? User.where(id: proposal&.author_id) : User.where(id: investment&.author_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -145,6 +145,14 @@ feature 'Communities' do
|
|||||||
expect(page).to have_current_path(root_path)
|
expect(page).to have_current_path(root_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario "Accesing a community without associated communitable" do
|
||||||
|
proposal = create(:proposal)
|
||||||
|
community = proposal.community
|
||||||
|
proposal.really_destroy!
|
||||||
|
community.reload
|
||||||
|
|
||||||
|
expect { visit community_path(community) }.to raise_error(ActionController::RoutingError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user