diff --git a/app/controllers/communities_controller.rb b/app/controllers/communities_controller.rb index f6f1e4816..0d1ac6890 100644 --- a/app/controllers/communities_controller.rb +++ b/app/controllers/communities_controller.rb @@ -7,6 +7,7 @@ class CommunitiesController < ApplicationController skip_authorization_check def show + raise ActionController::RoutingError, 'Not Found' unless communitable_exists? redirect_to root_path if Setting['feature.community'].blank? end @@ -31,4 +32,8 @@ class CommunitiesController < ApplicationController def valid_order? params[:order].present? && TOPIC_ORDERS.include?(params[:order]) end + + def communitable_exists? + @community.proposal.present? || @community.investment.present? + end end diff --git a/app/models/community.rb b/app/models/community.rb index 7d7073412..c9dd0ddfb 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -28,7 +28,7 @@ class Community < ActiveRecord::Base end 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 diff --git a/spec/features/communities_spec.rb b/spec/features/communities_spec.rb index 4841a473a..0b9fa2c01 100644 --- a/spec/features/communities_spec.rb +++ b/spec/features/communities_spec.rb @@ -145,6 +145,14 @@ feature 'Communities' do expect(page).to have_current_path(root_path) 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