Skip to content

Commit

Permalink
all mail attachments are saved to file system using paperclip
Browse files Browse the repository at this point in the history
  • Loading branch information
cristi committed Jul 23, 2009
1 parent 065711b commit c17452b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 47 deletions.
9 changes: 6 additions & 3 deletions app/controllers/database_mailer_processing.rb
Expand Up @@ -5,10 +5,13 @@ def process_mail_with_database(mail, config)
plain_body = (page.part( :email ) ? page.render_part( :email ) : page.render_part( :email_plain ))

if config[:save_to_database] || config[:save_to_database].nil?
fd = FormData.create(mail.data.merge(:url => mail.page.url, :blob => plain_body))
fd.form_data_assets.create(:attachment => mail.data["attachment"]) if mail.data["attachment"]
fd = FormData.create(mail.data.merge(:url => mail.page.url, :blob => plain_body))
mail.data.each do |k, v|
if v.class.to_s == "Tempfile"
fd.form_data_assets.create(:field_name => k, :attachment => v)
end
end
end

process_mail_without_database(mail, config)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/form_data_asset.rb
@@ -1,7 +1,7 @@
class FormDataAsset < ActiveRecord::Base
belongs_to :form_data

has_attached_file :attachment, :styles => { :thumb => '100x100>', :normal => '640x480>'}
has_attached_file :attachment, :styles => { :thumb => '100x100>' }

IMAGE_CONTENT_TYPES = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg']

Expand Down
20 changes: 13 additions & 7 deletions app/views/admin/form_datas/show.html.haml
Expand Up @@ -11,13 +11,19 @@
.right
%h3 Attachments
- unless @form_data.form_data_assets.empty?
%ul
%table.attachments
%tr.grey_bg
%th Field Name
%th Link
%th Thumbnail
- @form_data.form_data_assets.each do |att|
- if att.image?
%li
= image_tag att.attachment.url(:thumb)
= link_to att.attachment_file_name, att.attachment.url(:normal)
- else
%li
%tr
%td= att.field_name.blank? ? "Field:" : att.field_name.capitalize
%td
= link_to att.attachment_file_name, att.attachment.url
- if att.image?
%td
= image_tag att.attachment.url(:thumb)
- else
%td &nbsp;
= render :partial => "popups"
1 change: 1 addition & 0 deletions db/migrate/20090722135916_create_form_data_assets.rb
Expand Up @@ -2,6 +2,7 @@ class CreateFormDataAssets < ActiveRecord::Migration
def self.up
create_table :form_data_assets do |t|
t.references :form_data
t.string :field_name
t.string :attachment_file_name
t.string :attachment_content_type
t.integer :attachment_file_size
Expand Down
4 changes: 4 additions & 0 deletions public/stylesheets/sass/database_mailer.sass
Expand Up @@ -186,6 +186,9 @@ table.export td.sep
:vertical-align top
td
:padding 3px 5px
table.attachments
th
:text-align center
a
:color #000
.left
Expand All @@ -194,6 +197,7 @@ table.export td.sep
:padding-right 20px
a
:float right
:margin-right 30px
.right
:float left
ul
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/mail_controller_spec.rb
Expand Up @@ -6,6 +6,7 @@
before do
login_as :developer
@form_data = mock_model(FormData)
@form_data.stub!(:form_data_assets)
FormData.stub!(:create)
end

Expand All @@ -23,6 +24,11 @@ def do_post(options={})
do_post
end

it "creates a new FormDataAsset if an attachment is present" do
FormDataAsset.should_receive(:create).and_return(@form_data.form_data_assets)
do_post
end

it "passes the page url to the new form data" do
FormData.should_receive(:create).with("url" => "/contact/", "blob" => "").and_return(@form_data)
do_post
Expand Down
117 changes: 81 additions & 36 deletions spec/datasets/mailers.rb
Expand Up @@ -2,41 +2,86 @@ class MailersDataset < Dataset::Base
uses :home_page

def load
create_record :page, :contact,
{:title => "Contact",
:breadcrumb => "contact",
:slug => "/contact",
:class_name => nil}

create_record :page_part, :mailer,
{:id => 1,
:name => "mailer",
:content => %Q{
subject: Contact email
from: test@example.com
redirect_to: /contact-success
recipients:
- foo@bar.com
},
:page_id => page_id(:contact)}

create_record :page, :contact_no_save,
{:title => "Contact",
:breadcrumb => "contact",
:slug => "/contact",
:class_name => nil}

create_record :page_part, :mailer,
{:id => 2,
:name => "mailer",
:content => %Q{
subject: Contact email
from: test@example.com
redirect_to: /contact-success
save_to_database: false
recipients:
- foo@bar.com
},
:page_id => page_id(:contact_no_save)}
create_page "Contact" do
create_page_part "contact_body",
:name => "body",
:content => %Q{
<r:mailer:form>
Name:<br/>
<r:mailer:text name="name" /> <br/>
Email:<br/>
<r:mailer:text name="email" /> <br/>
Message:<br/>
<r:mailer:textarea name="message" /> <br/>
File:<br/>
<r:mailer:file name="attached_file" /> <br/>
<input type="submit" value="Send" />
</r:mailer:form>}
create_page_part "mailer",
:content => {
'subject' => 'From the website of Whatever',
'from' => 'no_reply@aissac.ro',
'redirect_to' => '/contact/thank-you',
'recipients' => 'example@aissac.ro'}.to_yaml
create_page_part "email",
:content => %Q{
<r:mailer>
Name: <r:get name="name" />
Email: <r:get name="email" />
Message: <r:get name="message" />
</r:mailer>
}
create_page "Thank You", :body => "Thank you!"
end


create_page "Contact No Save" do
create_page_part "mailer_no_save",
:name => 'mailer',
:content => {
'subject' => 'From the website of Whatever',
'from' => 'no_reply@aissac.ro',
'redirect_to' => '/contact/thank-you',
'recipients' => 'example@aissac.ro',
'save_to_database' => 'false'}.to_yaml
create_page "Thank You", :body => "Thank you!"
end

# create_record :page, :contact,
# {:title => "Contact",
# :breadcrumb => "contact",
# :slug => "/contact",
# :class_name => nil}
#
# create_record :page_part, :mailer,
# {:id => 1,
# :name => "mailer",
# :content => %Q{
# subject: Contact email
# from: test@example.com
# redirect_to: /contact-success
# recipients:
# - foo@bar.com
# },
# :page_id => page_id(:contact)}
#
# create_record :page, :contact_no_save,
# {:title => "Contact",
# :breadcrumb => "contact",
# :slug => "/contact",
# :class_name => nil}
#
# create_record :page_part, :mailer,
# {:id => 2,
# :name => "mailer",
# :content => %Q{
# subject: Contact email
# from: test@example.com
# redirect_to: /contact-success
# save_to_database: false
# recipients:
# - foo@bar.com
# },
# :page_id => page_id(:contact_no_save)}
end
end

0 comments on commit c17452b

Please sign in to comment.