diff options
author | Robin Houston <robin@lenny.robin> | 2011-07-29 05:09:48 +0100 |
---|---|---|
committer | Robin Houston <robin@lenny.robin> | 2011-07-29 05:09:48 +0100 |
commit | 62f863d6fbae6dc4922964b2530f151883ca2180 (patch) | |
tree | 8f0d431920357a233ca94bcbd2a44b1113330055 /app/models | |
parent | 642a3f74b7036383dd326584e411f98eb463cc0c (diff) | |
parent | 3ba811a9e576a50123930ccccda4dc01d5e3341e (diff) |
Merge branch 'master' into wdtk
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/info_request.rb | 4 | ||||
-rw-r--r-- | app/models/raw_email.rb | 39 |
2 files changed, 40 insertions, 3 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index de3b5bdc5..209954b16 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -428,11 +428,11 @@ public ActiveRecord::Base.transaction do raw_email = RawEmail.new - raw_email.data = raw_email_data incoming_message.raw_email = raw_email incoming_message.info_request = self - raw_email.save! incoming_message.save! + raw_email.data = raw_email_data + raw_email.save! self.awaiting_description = true self.log_event("response", { :incoming_message_id => incoming_message.id }) diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb index 2406cee7c..1d85cc7d9 100644 --- a/app/models/raw_email.rb +++ b/app/models/raw_email.rb @@ -20,17 +20,54 @@ class RawEmail < ActiveRecord::Base has_one :incoming_message + before_destroy :destroy_file_representation! # We keep the old data_text field (which is of type text) for backwards # compatibility. We use the new data_binary field because only it works # properly in recent versions of PostgreSQL (get seg faults escaping # some binary strings). + def directory + request_id = self.incoming_message.info_request.id.to_s + if ENV["RAILS_ENV"] = "test" + return 'files/raw_email_test' + else + return File.join(MySociety::Config.get('RAW_EMAILS_LOCATION', + 'files/raw_emails'), + request_id[0..2], request_id) + end + end + + def filepath + File.join(self.directory, self.incoming_message.id.to_s) + end + def data=(d) - write_attribute(:data_binary, d) + if !File.exists?(self.directory) + FileUtils.mkdir_p self.directory + end + File.open(self.filepath, "wb") { |file| + file.write d + } end def data + if !File.exists?(self.filepath) + dbdata + else + File.open(self.filepath, "rb" ).read + end + end + + def destroy_file_representation! + File.delete(self.filepath) + end + + def dbdata=(d) + write_attribute(:data_binary, d) + end + + def dbdata d = read_attribute(:data_binary) if !d.nil? return d |