From b56681a609c668cdf826085e5dc27d7b7c88f8c4 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Tue, 7 Aug 2018 13:31:45 -0700 Subject: [PATCH] refactor solution for #3804 --- app/helpers/uploads_helper.rb | 15 +++++++++++++++ app/models/upload.rb | 8 ++++++++ app/presenters/upload_presenter.rb | 14 -------------- app/views/uploads/index.html.erb | 2 +- app/views/uploads/show.html.erb | 2 +- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/helpers/uploads_helper.rb b/app/helpers/uploads_helper.rb index f4f8250f5..8840d6103 100644 --- a/app/helpers/uploads_helper.rb +++ b/app/helpers/uploads_helper.rb @@ -1,2 +1,17 @@ module UploadsHelper + def render_status(upload) + case upload.status + when /duplicate: (\d+)/ + dup_post_id = $1 + link_to(upload.status.gsub(/error: RuntimeError - /, ""), post_path(dup_post_id)) + + when /\Aerror: / + search_params = params[:search].permit! + link_to(upload.sanitized_status, uploads_path(search: search_params.merge({ status: upload.sanitized_status }))) + + else + search_params = params[:search].permit! + link_to(upload.status, uploads_path(search: search_params.merge({ status: upload.status }))) + end + end end diff --git a/app/models/upload.rb b/app/models/upload.rb index 8e0cefacf..1a59188db 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -117,6 +117,14 @@ class Upload < ApplicationRecord status =~ /error:/ end + def sanitized_status + if is_errored? + status.sub(/DETAIL:.+/m, "...") + else + status + end + end + def duplicate_post_id @duplicate_post_id ||= status[/duplicate: (\d+)/, 1] end diff --git a/app/presenters/upload_presenter.rb b/app/presenters/upload_presenter.rb index 36f9babd3..44a1cce2e 100644 --- a/app/presenters/upload_presenter.rb +++ b/app/presenters/upload_presenter.rb @@ -2,18 +2,4 @@ class UploadPresenter < Presenter def initialize(upload) @upload = upload end - - def status(template) - case @upload.status - when /duplicate: (\d+)/ - dup_post_id = $1 - template.link_to(@upload.status.gsub(/error: RuntimeError - /, ""), template.__send__(:post_path, dup_post_id)) - - when /\Aerror: / - @upload.status.gsub(/DETAIL:.+/m, "...") - - else - @upload.status - end - end end diff --git a/app/views/uploads/index.html.erb b/app/views/uploads/index.html.erb index f41568d3f..16c59d2e0 100644 --- a/app/views/uploads/index.html.erb +++ b/app/views/uploads/index.html.erb @@ -67,7 +67,7 @@
<%= time_ago_in_words_tagged upload.created_at %> - <%= upload.presenter.status(self) %> + <%= render_status(upload) %> <% end %> diff --git a/app/views/uploads/show.html.erb b/app/views/uploads/show.html.erb index f6d59d28d..414ed9a40 100644 --- a/app/views/uploads/show.html.erb +++ b/app/views/uploads/show.html.erb @@ -28,7 +28,7 @@ <% elsif @upload.is_duplicate? %>

This upload is a duplicate: <%= link_to "post ##{@upload.duplicate_post_id}", post_path(@upload.duplicate_post_id) %>

<% else %> -

An error occurred: <%= @upload.presenter.status(self) %>

+

An error occurred: <%= render_status(@upload) %>

<% if CurrentUser.user.is_builder? %>