Add a redirect and error message if user ends up trying to report something without...
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 14 Mar 2018 09:09:57 +0000 (17:09 +0800)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 14 Mar 2018 09:09:57 +0000 (17:09 +0800)
app/controllers/reports_controller.rb
config/locales/en.yml
test/system/report_diary_entry_test.rb

index 5d04427..4d2220a 100644 (file)
@@ -5,9 +5,11 @@ class ReportsController < ApplicationController
   before_action :require_user
 
   def new
-    if create_new_report_params.present?
+    if required_new_report_params_present?
       @report = Report.new
       @report.issue = Issue.find_or_initialize_by(create_new_report_params)
+    else
+      redirect_to root_path, :notice => t("reports.new.missing_params")
     end
   end
 
@@ -26,6 +28,10 @@ class ReportsController < ApplicationController
 
   private
 
+  def required_new_report_params_present?
+    create_new_report_params['reportable_id'].present? && create_new_report_params['reportable_type'].present?
+  end
+
   def create_new_report_params
     params.permit(:reportable_id, :reportable_type)
   end
index d57bc65..afc100d 100644 (file)
@@ -1039,6 +1039,7 @@ en:
   reports:
     new:
       title_html: "Report %{link}"
+      missing_params: "Cannot create a new report"
     categories:
       diary_entry:
         spam: This diary entry is/contains spam
index 3be8b3f..e926238 100644 (file)
@@ -49,4 +49,10 @@ class ReportDiaryEntryTest < ApplicationSystemTestCase
     assert !issue.resolved?
     assert issue.open?
   end
+
+  def test_missing_report_params
+    sign_in_as(create(:user))
+    visit new_report_path
+    assert page.has_content? I18n.t("reports.new.missing_params")
+  end
 end