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 5d0442731733453d845790cc362ac8d9b2bfe15e..4d2220a26c21541bdb667d6e9538b4303a754232 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 d57bc65aafe3078227f59af41110376f273fee9a..afc100dc965222056b7c1a282d52e30d125c3cab 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 3be8b3fd7b65ce49da0cfdeee200b8c3655cb54b..e9262388a03255d6c331dad9c162e1c69ec05212 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