Add a redirect and error message if user ends up trying to report something without...
[rails.git] / app / controllers / reports_controller.rb
1 class ReportsController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6
7   def new
8     if required_new_report_params_present?
9       @report = Report.new
10       @report.issue = Issue.find_or_initialize_by(create_new_report_params)
11     else
12       redirect_to root_path, :notice => t("reports.new.missing_params")
13     end
14   end
15
16   def create
17     @report = current_user.reports.new(report_params)
18     @report.issue = Issue.find_or_initialize_by(:reportable_id => params[:report][:issue][:reportable_id], :reportable_type => params[:report][:issue][:reportable_type])
19
20     if @report.save
21       @report.issue.save
22       @report.issue.reopen! unless @report.issue.open?
23       redirect_to root_path, :notice => t("issues.create.successful_report")
24     else
25       redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t("issues.create.provide_details")
26     end
27   end
28
29   private
30
31   def required_new_report_params_present?
32     create_new_report_params['reportable_id'].present? && create_new_report_params['reportable_type'].present?
33   end
34
35   def create_new_report_params
36     params.permit(:reportable_id, :reportable_type)
37   end
38
39   def report_params
40     params[:report].permit(:details, :category)
41   end
42 end