From 0419bc35047a62286961feb6e3e0177c9aa18cfa Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Mon, 7 Jan 2019 09:05:39 +1100 Subject: add attachments to the new inbound mail --- .../action_mailbox/inbound_emails_controller.rb | 6 +++- .../action_mailbox/inbound_emails/new.html.erb | 5 +++ .../inbound_emails_controller_test.rb | 34 +++++++++++++++++++++ actionmailbox/test/fixtures/files/avatar1.jpeg | Bin 0 -> 20535 bytes actionmailbox/test/fixtures/files/avatar2.jpeg | Bin 0 -> 12686 bytes 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb create mode 100644 actionmailbox/test/fixtures/files/avatar1.jpeg create mode 100644 actionmailbox/test/fixtures/files/avatar2.jpeg (limited to 'actionmailbox') diff --git a/actionmailbox/app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb b/actionmailbox/app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb index 30d6afe0c0..3537a983ef 100644 --- a/actionmailbox/app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb +++ b/actionmailbox/app/controllers/rails/conductor/action_mailbox/inbound_emails_controller.rb @@ -20,7 +20,11 @@ module Rails private def new_mail - Mail.new params.require(:mail).permit(:from, :to, :cc, :bcc, :in_reply_to, :subject, :body).to_h + Mail.new(params.require(:mail).permit(:from, :to, :cc, :bcc, :in_reply_to, :subject, :body).to_h).tap do |mail| + params[:mail][:attachments].to_a.each do |attachment| + mail.attachments[attachment.original_filename] = { filename: attachment.path, content_type: attachment.content_type } + end + end end def create_inbound_email(mail) diff --git a/actionmailbox/app/views/rails/conductor/action_mailbox/inbound_emails/new.html.erb b/actionmailbox/app/views/rails/conductor/action_mailbox/inbound_emails/new.html.erb index 7f1ec74496..a3c3d39064 100644 --- a/actionmailbox/app/views/rails/conductor/action_mailbox/inbound_emails/new.html.erb +++ b/actionmailbox/app/views/rails/conductor/action_mailbox/inbound_emails/new.html.erb @@ -38,5 +38,10 @@ <%= form.text_area :body, size: "40x20" %> +
+ <%= form.label :attachments, "Attachments" %>
+ <%= form.file_field :attachments, multiple: true %> +
+ <%= form.submit "Deliver inbound email" %> <% end %> diff --git a/actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb b/actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb new file mode 100644 index 0000000000..60077d86e2 --- /dev/null +++ b/actionmailbox/test/controllers/rails/action_mailbox/inbound_emails_controller_test.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "test_helper" + +class Rails::Conductor::ActionMailbox::InboundEmailsControllerTest < ActionDispatch::IntegrationTest + test "create inbound email" do + with_rails_env("development") do + assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do + post rails_conductor_inbound_emails_path, params: { + mail: { + from: "Jason Fried ", + to: "Replies ", + bcc: "", + in_reply_to: "<4e6e35f5a38b4_479f13bb90078178@small-app-01.mail>", + subject: "Discussion: Let's debate these attachments", + body: "Let's talk about these images:", + attachments: [fixture_file_upload("files/avatar1.jpeg"), fixture_file_upload("files/avatar2.jpeg")] + } + } + end + + assert_equal 2, ActionMailbox::InboundEmail.last.mail.attachments.size + end + end + + private + def with_rails_env(env) + old_rails_env = Rails.env + Rails.env = env + yield + ensure + Rails.env = old_rails_env + end +end diff --git a/actionmailbox/test/fixtures/files/avatar1.jpeg b/actionmailbox/test/fixtures/files/avatar1.jpeg new file mode 100644 index 0000000000..31111c3bc9 Binary files /dev/null and b/actionmailbox/test/fixtures/files/avatar1.jpeg differ diff --git a/actionmailbox/test/fixtures/files/avatar2.jpeg b/actionmailbox/test/fixtures/files/avatar2.jpeg new file mode 100644 index 0000000000..e844e7f3c6 Binary files /dev/null and b/actionmailbox/test/fixtures/files/avatar2.jpeg differ -- cgit v1.2.3