From 086edd79df54a36956188f3700e32af84d0d1b6c Mon Sep 17 00:00:00 2001
From: Alex Shulgin <ash@commandprompt.com>
Date: Tue, 9 Aug 2011 10:43:17 +0300
Subject: [PATCH 17/25] Fixed attachments handling when receiving a new issue by mail.

Creates a new journal if there is no one already.
---
 app/models/issue.rb        |   10 ++++++----
 app/models/mail_handler.rb |    6 +++++-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/app/models/issue.rb b/app/models/issue.rb
index e1ea27f..b439b1f 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -972,11 +972,13 @@ class Issue < ActiveRecord::Base
     end
   end
 
-  # Callback on attachment deletion
+  # Callback on attachment addition
   def attachment_added(obj)
-    if @current_journal && !obj.new_record?
-      @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
-    end
+    init_journal(User.current) unless @current_journal
+    @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
+    # Currently, this is only triggered from attachments#destroy, so
+    # there's no one else to save the issue's journal.
+    @current_journal.save
   end
 
   # Callback on attachment deletion
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index daa5b48..131f7df 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -166,7 +166,11 @@ class MailHandler < ActionMailer::Base
     add_watchers(issue)
     issue.save!
 
-    add_attachments(issue)
+    if add_attachments(issue)
+      # Adding attachments to a new issue creates a journal.  We need
+      # to save the record to trigger an issue update mail.
+      issue.save!
+    end
     logger.info "MailHandler: issue ##{issue.id} created by #{user}"
     issue
   end
-- 
1.7.0.4

