]> git.openstreetmap.org Git - rails.git/blob - app/controllers/reports_controller.rb
Move more api-only methods into api_controller
[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
7   authorize_resource
8
9   def new
10     if required_new_report_params_present?
11       @report = Report.new
12       @report.issue = Issue.find_or_initialize_by(create_new_report_params)
13     else
14       redirect_to root_path, :notice => t(".missing_params")
15     end
16   end
17
18   def create
19     @report = current_user.reports.new(report_params)
20     @report.issue = Issue
21                     .create_with(:assigned_role => default_assigned_role)
22                     .find_or_initialize_by(issue_params)
23
24     if @report.save
25       @report.issue.assigned_role = "administrator" if default_assigned_role == "administrator"
26       @report.issue.reopen unless @report.issue.open?
27       @report.issue.save!
28
29       redirect_to helpers.reportable_url(@report.issue.reportable), :notice => t(".successful_report")
30     else
31       redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t(".provide_details")
32     end
33   end
34
35   private
36
37   def required_new_report_params_present?
38     create_new_report_params["reportable_id"].present? && create_new_report_params["reportable_type"].present?
39   end
40
41   def create_new_report_params
42     params.permit(:reportable_id, :reportable_type)
43   end
44
45   def report_params
46     params.require(:report).permit(:details, :category)
47   end
48
49   def issue_params
50     params.require(:report).require(:issue).permit(:reportable_id, :reportable_type)
51   end
52
53   def default_assigned_role
54     case issue_params[:reportable_type]
55     when "Note" then "moderator"
56     when "User" then case report_params[:category]
57                      when "vandal" then "moderator"
58                      else "administrator"
59                      end
60     else "administrator"
61     end
62   end
63 end