Skip to content

Commit

Permalink
added attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Dickey committed Dec 5, 2011
1 parent d4cff4f commit f38f592
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
source "http://rubygems.org"

gemspec

gem 'haml'
gem 'jquery-rails'
gem 'devise'
gem 'carrierwave'
3 changes: 3 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Message < Notification
scope :conversation, lambda { |conversation|
where(:conversation_id => conversation.id)
}

mount_uploader :attachment, AttachmentUploader

class << self
#Sets the on deliver callback method.
def on_deliver(callback_method)
Expand Down
3 changes: 3 additions & 0 deletions app/uploaders/attachment_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
end
5 changes: 5 additions & 0 deletions db/migrate/20111204163911_add_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAttachments < ActiveRecord::Migration
def change
add_column :notifications, :attachment, :string
end
end
6 changes: 3 additions & 3 deletions lib/mailboxer/models/messageable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil)

#Sends a messages, starting a new conversation, with the messageable
#as originator
def send_message(recipients, msg_body, subject,sanitize_text=true)
def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil)
convo = Conversation.new({:subject => subject})
message = Message.new({:sender => self, :conversation => convo, :body => msg_body, :subject => subject})
message = Message.new({:sender => self, :conversation => convo, :body => msg_body, :subject => subject, :attachment => attachment})
message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
message.recipients = message.recipients.uniq
return message.deliver false,sanitize_text
Expand Down Expand Up @@ -180,4 +180,4 @@ def untrash(obj)
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/dummy/db/migrate/20111205013142_add_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAttachments < ActiveRecord::Migration
def change
add_column :notifications, :attachment, :string
end
end
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20111204012607) do
ActiveRecord::Schema.define(:version => 20111205013142) do

create_table "conversations", :force => true do |t|
t.string "subject", :default => ""
Expand Down Expand Up @@ -46,6 +46,7 @@
t.integer "notified_object_id"
t.string "notified_object_type"
t.string "notification_code"
t.string "attachment"
end

add_index "notifications", ["conversation_id"], :name => "index_notifications_on_conversation_id"
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/public/uploads/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a dummy file to test file uploads
6 changes: 5 additions & 1 deletion spec/models/mailboxer_models_messageable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@
@conversation.receipts_for(@entity1).first.trashed.should==true
end

it "should be able to read attachment" do
@receipt = @entity1.send_message(@entity2, "Body", "Subject", nil, File.open('spec/testfile.txt'))
@conversation = @receipt.conversation
@conversation.messages.first.attachment_identifier.should=='testfile.txt'
end


end
1 change: 1 addition & 0 deletions spec/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a dummy file to test file uploads

0 comments on commit f38f592

Please sign in to comment.