From 119e6f55e7b48bb748738ef046e67ec9e00aacc7 Mon Sep 17 00:00:00 2001 From: albert Date: Sun, 17 Feb 2013 13:38:23 -0500 Subject: [PATCH] smarter logic for dups --- app/models/upload.rb | 16 +++++++++++++--- app/views/uploads/show.html.erb | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index d4f9417d6..5e7b76ef4 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -23,11 +23,14 @@ class Upload < ActiveRecord::Base # Because uploads are processed serially, there's no race condition here. def validate_md5_uniqueness md5_post = Post.find_by_md5(md5) - merge_tags(md5_post) if md5_post + if md5_post + merge_tags(md5_post) + update_attribute(:status, "duplicate: #{post.id}") + end end def validate_file_exists - unless File.exists?(file_path) + unless file_path && File.exists?(file_path) raise "file does not exist" end end @@ -106,7 +109,6 @@ class Upload < ActiveRecord::Base def merge_tags(post) post.tag_string += " #{tag_string}" post.save - update_attribute(:status, "duplicate: #{post.id}") end end @@ -293,6 +295,14 @@ class Upload < ActiveRecord::Base def is_completed? status == "completed" end + + def is_duplicate? + status =~ /duplicate/ + end + + def duplicate_post_id + @duplicate_post_id ||= status[/duplicate: (\d+)/, 1] + end end module UploaderMethods diff --git a/app/views/uploads/show.html.erb b/app/views/uploads/show.html.erb index 1313eab8b..e2b6c5045 100644 --- a/app/views/uploads/show.html.erb +++ b/app/views/uploads/show.html.erb @@ -12,6 +12,8 @@

This upload is waiting to be processed. Please wait a few seconds.

<% elsif @upload.is_processing? %>

This upload is being processed. Please wait a few seconds.

+<% elsif @upload.is_duplicate? %> +

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

<% else %>

An error occurred: <%= @upload.status %>