Refactor creating a new report to use a ReportsController
[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 create_new_report_params.present?
9       @report = Report.new
10       @report.issue = Issue.find_or_initialize_by(create_new_report_params)
11       path = "issues.report_strings." + @report.issue.reportable.class.name.to_s
12       @report_strings_yaml = t(path)
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       # FIXME: reopen issue if necessary
23       # FIXME: new issue notification (or via model observer)
24       redirect_to root_path, :notice => t("issues.create.successful_report")
25     else
26       redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t("issues.create.provide_details")
27     end
28   end
29
30   private
31
32   def create_new_report_params
33     params.permit(:reportable_id, :reportable_type)
34   end
35
36   def report_params
37     params[:report].permit(:details)
38   end
39 end