From: Andy Allan Date: Wed, 6 Sep 2017 17:06:21 +0000 (+0100) Subject: Refactor creating a new report to use a ReportsController X-Git-Tag: live~2953^2~101 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/d7612f42d04727c037deb3ef66ac421ea347d54b Refactor creating a new report to use a ReportsController It's really a report that the user is creating, the issue is created indirectly. Similar refactoring will be required for issue comments. --- diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 346755fe3..67c0d5b6e 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -62,54 +62,6 @@ class IssuesController < ApplicationController @related_issues = @issue.reported_user.issues.where(:issue_type => @user_role) end - def new - if create_new_issue_params.present? - @issue = Issue.find_or_initialize_by(create_new_issue_params) - path = "issues.report_strings." + @issue.reportable.class.name.to_s - @report_strings_yaml = t(path) - end - end - - def create - @issue = Issue.find_by(:reportable_id => params[:reportable_id], :reportable_type => params[:reportable_type]) - # Check if Issue already exists - unless @issue - @issue = Issue.find_or_initialize_by(issue_params) - @issue.updated_by = nil - - # Reassign to moderators if it is a moderator issue - @issue.issue_type = "administrator" - reassign_issue if @moderator_issues.include? @issue.reportable.class.name - end - - # Check if details provided are sufficient - if check_report_params - @report = @issue.reports.build(report_params) - details = report_details - @report.reporter_user_id = current_user.id - @report.details = details - # Checking if instance has been updated since last report - @last_report = @issue.reports.order(:updated_at => :desc).last - if check_if_updated - @issue.save! if @issue.reopen - end - - if @issue.save! - @issue.report_count = @issue.reports.count - @issue.save! - - @admins_or_mods = UserRole.where(:role => @issue.issue_type) - @admins_or_mods.each do |user| - Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now - end - - redirect_back :fallback_location => "/", :notice => t("issues.create.successful_report") - end - else - redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id), :notice => t("issues.create.provide_details") - end - end - def update @issue = Issue.find_by(issue_params) # Check if details provided are sufficient @@ -246,10 +198,6 @@ class IssuesController < ApplicationController end end - def create_new_issue_params - params.permit(:reportable_id, :reportable_type) - end - def issue_params params[:issue].permit(:reportable_id, :reportable_type) end diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb new file mode 100644 index 000000000..6c62deb30 --- /dev/null +++ b/app/controllers/reports_controller.rb @@ -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 diff --git a/app/views/diary_entry/_diary_entry.html.erb b/app/views/diary_entry/_diary_entry.html.erb index 496d07c9e..872e31b3a 100644 --- a/app/views/diary_entry/_diary_entry.html.erb +++ b/app/views/diary_entry/_diary_entry.html.erb @@ -6,8 +6,8 @@

<%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %>

- <% if current_user and diary_entry.user.id != current_user.id %> - <%= link_to new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, referer: request.fullpath), :title => t('diary_entry.diary_entry.report') do %> + <% if current_user and diary_entry.user != current_user %> + <%= link_to new_report_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name), :title => t('diary_entry.diary_entry.report') do %>  ⚐ <% end %> <% end %> diff --git a/app/views/issues/new.html.erb b/app/views/reports/new.html.erb similarity index 78% rename from app/views/issues/new.html.erb rename to app/views/reports/new.html.erb index 4a3389959..6dad7aa43 100644 --- a/app/views/issues/new.html.erb +++ b/app/views/reports/new.html.erb @@ -1,5 +1,5 @@ <% content_for :heading do %> -

Report <%= reportable_url(@issue.reportable) %>

+

Report <%= reportable_url(@report.issue.reportable) %>

<% end %>
@@ -11,13 +11,13 @@
-<%= form_for(@issue) do |f| %> +<%= form_for(@report) do |f| %> <%= f.error_messages %>
-
- <%= f.hidden_field :reportable_id %> - <%= f.hidden_field :reportable_type %> -
+ <%= f.fields_for @report.issue do |issue_form| %> + <%= issue_form.hidden_field :reportable_id %> + <%= issue_form.hidden_field :reportable_type %> + <% end %>

<%= t('issues.new.select') %>:

diff --git a/config/routes.rb b/config/routes.rb index 2fec73c4e..469478969 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -300,6 +300,8 @@ OpenStreetMap::Application.routes.draw do end end + resources :reports + post "/comment" => "issues#comment" # redactions