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