]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/reports_controller.rb
Refactor creating a new report to use a ReportsController
[rails.git] / app / controllers / reports_controller.rb
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb
new file mode 100644 (file)
index 0000000..6c62deb
--- /dev/null
@@ -0,0 +1,39 @@
+class ReportsController < ApplicationController
+  layout "site"
+
+  before_action :authorize_web
+  before_action :require_user
+
+  def new
+    if create_new_report_params.present?
+      @report = Report.new
+      @report.issue = Issue.find_or_initialize_by(create_new_report_params)
+      path = "issues.report_strings." + @report.issue.reportable.class.name.to_s
+      @report_strings_yaml = t(path)
+    end
+  end
+
+  def create
+    @report = current_user.reports.new(report_params)
+    @report.issue = Issue.find_or_initialize_by(:reportable_id => params[:report][:issue][:reportable_id], :reportable_type => params[:report][:issue][:reportable_type])
+
+    if @report.save
+      @report.issue.save
+      # FIXME: reopen issue if necessary
+      # FIXME: new issue notification (or via model observer)
+      redirect_to root_path, :notice => t("issues.create.successful_report")
+    else
+      redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t("issues.create.provide_details")
+    end
+  end
+
+  private
+
+  def create_new_report_params
+    params.permit(:reportable_id, :reportable_type)
+  end
+
+  def report_params
+    params[:report].permit(:details)
+  end
+end