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.
This commit is contained in:
Andy Allan 2017-09-06 18:06:21 +01:00
parent 948fd8c8fa
commit d7612f42d0
5 changed files with 49 additions and 60 deletions

View file

@ -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

View file

@ -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