]> git.openstreetmap.org Git - rails.git/commitdiff
Refactor creating a new report to use a ReportsController
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Sep 2017 17:06:21 +0000 (18:06 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Sep 2017 17:06:21 +0000 (18:06 +0100)
It's really a report that the user is creating, the issue is created indirectly. Similar
refactoring will be required for issue comments.

app/controllers/issues_controller.rb
app/controllers/reports_controller.rb [new file with mode: 0644]
app/views/diary_entry/_diary_entry.html.erb
app/views/reports/new.html.erb [moved from app/views/issues/new.html.erb with 78% similarity]
config/routes.rb

index 346755fe3c015fc2a56df3e80785aebfe3323a3e..67c0d5b6ef8fe0d670a0a512c06ba95231ca67cc 100644 (file)
@@ -62,54 +62,6 @@ class IssuesController < ApplicationController
     @related_issues = @issue.reported_user.issues.where(:issue_type => @user_role)
   end
 
     @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
   def update
     @issue = Issue.find_by(issue_params)
     # Check if details provided are sufficient
@@ -246,10 +198,6 @@ class IssuesController < ApplicationController
     end
   end
 
     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
   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 (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
index 496d07c9ebef7efa3765fa9e32ce8dcf50440a93..872e31b3aa347c7e27c69b314ea62bbbeb5d347b 100644 (file)
@@ -6,8 +6,8 @@
 
     <h2><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></h2>
 
 
     <h2><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></h2>
 
-    <% 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 %>
             &nbsp;&#9872;
           <% end %>
     <% end %>
             &nbsp;&#9872;
           <% end %>
     <% end %>
similarity index 78%
rename from app/views/issues/new.html.erb
rename to app/views/reports/new.html.erb
index 4a33899599115f5bbf4058d9f214ce0282e8b7e8..6dad7aa43a314069ddbcd2f05a112aacd37ef467 100644 (file)
@@ -1,5 +1,5 @@
 <% content_for :heading do %>
 <% content_for :heading do %>
-  <h1>Report <%= reportable_url(@issue.reportable) %></h1>
+  <h1>Report <%= reportable_url(@report.issue.reportable) %></h1>
 <% end %>
 
 <div class="disclaimer">
 <% end %>
 
 <div class="disclaimer">
     </ul>
 </div>
 
     </ul>
 </div>
 
-<%= form_for(@issue) do |f| %>
+<%= form_for(@report) do |f| %>
   <%= f.error_messages %>
   <fieldset>
   <%= f.error_messages %>
   <fieldset>
-    <div class='form-row'>
-      <%= f.hidden_field :reportable_id %>
-      <%= f.hidden_field :reportable_type %>
-    </div>
+    <%= f.fields_for @report.issue do |issue_form| %>
+      <%= issue_form.hidden_field :reportable_id %>
+      <%= issue_form.hidden_field :reportable_type %>
+    <% end %>
 
     <div class='form-row' style='width:600px'>
       <p><%= t('issues.new.select') %>:</p>
 
     <div class='form-row' style='width:600px'>
       <p><%= t('issues.new.select') %>:</p>
index 2fec73c4e7dbc66c1dedae25764646531f89781b..4694789694e8ddf26a745e8607b0eb562e96d648 100644 (file)
@@ -300,6 +300,8 @@ OpenStreetMap::Application.routes.draw do
     end
   end
 
     end
   end
 
+  resources :reports
+
   post "/comment" => "issues#comment"
 
   # redactions
   post "/comment" => "issues#comment"
 
   # redactions