diff options
author | Louise Crow <louise.crow@gmail.com> | 2014-11-04 14:55:59 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2014-12-18 14:03:47 +0000 |
commit | 2a76e61f6ca78a867e4a01589287ec76a5c8f4f7 (patch) | |
tree | c1b529f9bf3fa0417993041f8c458c192696be0c | |
parent | 148eb5e7e36a3e069d73a85188298d26bee1b220 (diff) |
Make downloading a raw email a RESTful route
-rw-r--r-- | app/controllers/admin_raw_email_controller.rb | 46 | ||||
-rw-r--r-- | app/controllers/admin_request_controller.rb | 7 | ||||
-rw-r--r-- | app/views/admin_raw_email/show.html.erb | 2 | ||||
-rw-r--r-- | config/routes.rb | 2 | ||||
-rw-r--r-- | spec/controllers/admin_raw_email_controller_spec.rb | 22 |
5 files changed, 48 insertions, 31 deletions
diff --git a/app/controllers/admin_raw_email_controller.rb b/app/controllers/admin_raw_email_controller.rb index cfff2d0b6..1b3ee2871 100644 --- a/app/controllers/admin_raw_email_controller.rb +++ b/app/controllers/admin_raw_email_controller.rb @@ -8,29 +8,37 @@ class AdminRawEmailController < AdminController def show @raw_email = RawEmail.find(params[:id]) - # For the holding pen, try to guess where it should be ... - @holding_pen = false - if (@raw_email.incoming_message.info_request == InfoRequest.holding_pen_request && !@raw_email.incoming_message.empty_from_field?) - @holding_pen = true + respond_to do |format| + format.html do + # For the holding pen, try to guess where it should be ... + @holding_pen = false + if (@raw_email.incoming_message.info_request == InfoRequest.holding_pen_request && !@raw_email.incoming_message.empty_from_field?) + @holding_pen = true - # 1. Use domain of email to try and guess which public body it - # is associated with, so we can display that. - email = @raw_email.incoming_message.from_email - domain = PublicBody.extract_domain_from_email(email) + # 1. Use domain of email to try and guess which public body it + # is associated with, so we can display that. + email = @raw_email.incoming_message.from_email + domain = PublicBody.extract_domain_from_email(email) - if domain.nil? - @public_bodies = [] - else - @public_bodies = PublicBody.find(:all, :order => "name", - :conditions => [ "lower(request_email) like lower('%'||?||'%')", domain ]) - end + if domain.nil? + @public_bodies = [] + else + @public_bodies = PublicBody.find(:all, :order => "name", + :conditions => [ "lower(request_email) like lower('%'||?||'%')", domain ]) + end - # 2. Match the email address in the message without matching the hash - @info_requests = InfoRequest.guess_by_incoming_email(@raw_email.incoming_message) + # 2. Match the email address in the message without matching the hash + @info_requests = InfoRequest.guess_by_incoming_email(@raw_email.incoming_message) - # 3. Give a reason why it's in the holding pen - last_event = InfoRequestEvent.find_by_incoming_message_id(@raw_email.incoming_message.id) - @rejected_reason = last_event.params[:rejected_reason] || "unknown reason" + # 3. Give a reason why it's in the holding pen + last_event = InfoRequestEvent.find_by_incoming_message_id(@raw_email.incoming_message.id) + @rejected_reason = last_event.params[:rejected_reason] || "unknown reason" + end + end + format.text do + response.content_type = 'message/rfc822' + render :text => @raw_email.data + end end end diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb index 9ccbd2d6f..e63d5e80a 100644 --- a/app/controllers/admin_request_controller.rb +++ b/app/controllers/admin_request_controller.rb @@ -175,13 +175,6 @@ class AdminRequestController < AdminController redirect_to admin_request_url(info_request) end - def download_raw_email - @raw_email = RawEmail.find(params[:id]) - - response.content_type = 'message/rfc822' - render :text => @raw_email.data - end - def hide ActiveRecord::Base.transaction do subject = params[:subject] diff --git a/app/views/admin_raw_email/show.html.erb b/app/views/admin_raw_email/show.html.erb index 83c81b8fb..b78b16356 100644 --- a/app/views/admin_raw_email/show.html.erb +++ b/app/views/admin_raw_email/show.html.erb @@ -57,7 +57,7 @@ <h2>Raw email</h2> -<p><%= link_to "Download", admin_request_download_raw_email_path(@raw_email) %></p> +<p><%= link_to "Download", admin_raw_email_path(@raw_email, :format => 'txt') %></p> <pre><%=h(@raw_email.data).gsub(/\n/, '<br>').html_safe %></pre> diff --git a/config/routes.rb b/config/routes.rb index 40af4e7d4..e3e7090d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -220,9 +220,9 @@ Alaveteli::Application.routes.draw do post 'move', :on => :member post 'generate_upload_url', :on => :member post 'hide', :on => :member + end end - match '/admin/request/download_raw_email/:id' => 'admin_request#download_raw_email', :as => :admin_request_download_raw_email #### #### AdminComment controller diff --git a/spec/controllers/admin_raw_email_controller_spec.rb b/spec/controllers/admin_raw_email_controller_spec.rb index c1e939ee1..77c57c38b 100644 --- a/spec/controllers/admin_raw_email_controller_spec.rb +++ b/spec/controllers/admin_raw_email_controller_spec.rb @@ -4,9 +4,25 @@ describe AdminRawEmailController do describe :show do - it 'renders the show template' do - raw_email = FactoryGirl.create(:incoming_message).raw_email - get :show, :id => raw_email.id + before do + @raw_email = FactoryGirl.create(:incoming_message).raw_email + end + + describe 'html version' do + + it 'renders the show template' do + get :show, :id => @raw_email.id + end + + end + + describe 'text version' do + + it 'sends the email as an RFC-822 attachment' do + get :show, :id => @raw_email.id, :format => 'txt' + response.content_type.should == 'message/rfc822' + response.body.should == @raw_email.data + end end end |