]> git.openstreetmap.org Git - rails.git/blob - app/controllers/reports_controller.rb
Merge branch 'master' into messages
[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
20                     .create_with(:assigned_role => default_assigned_role)
21                     .find_or_initialize_by(issue_params)
22
23     if @report.save
24       @report.issue.assigned_role = "administrator" if default_assigned_role == "administrator"
25       @report.issue.reopen unless @report.issue.open?
26       @report.issue.save!
27
28       redirect_to helpers.reportable_url(@report.issue.reportable), :notice => t(".successful_report")
29     else
30       redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t(".provide_details")
31     end
32   end
33
34   private
35
36   def required_new_report_params_present?
37     create_new_report_params["reportable_id"].present? && create_new_report_params["reportable_type"].present?
38   end
39
40   def create_new_report_params
41     params.permit(:reportable_id, :reportable_type)
42   end
43
44   def report_params
45     params.require(:report).permit(:details, :category)
46   end
47
48   def issue_params
49     params.require(:report).require(:issue).permit(:reportable_id, :reportable_type)
50   end
51
52   def default_assigned_role
53     case issue_params[:reportable_type]
54     when "Note" then "moderator"
55     when "User" then case report_params[:category]
56                      when "vandal" then "moderator"
57                      else "administrator"
58                      end
59     else "administrator"
60     end
61   end
62 end