diff --git a/Gemfile b/Gemfile index ef11867e..66715f63 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,8 @@ source "http://rubygems.org" - + gemspec gem 'haml' gem 'jquery-rails' gem 'devise' +gem 'carrierwave' diff --git a/app/models/message.rb b/app/models/message.rb index d22087ed..4e7f458e 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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) diff --git a/app/uploaders/attachment_uploader.rb b/app/uploaders/attachment_uploader.rb new file mode 100644 index 00000000..45917ee8 --- /dev/null +++ b/app/uploaders/attachment_uploader.rb @@ -0,0 +1,3 @@ +class AttachmentUploader < CarrierWave::Uploader::Base + storage :file +end diff --git a/db/migrate/20111204163911_add_attachments.rb b/db/migrate/20111204163911_add_attachments.rb new file mode 100644 index 00000000..08e02c02 --- /dev/null +++ b/db/migrate/20111204163911_add_attachments.rb @@ -0,0 +1,5 @@ +class AddAttachments < ActiveRecord::Migration + def change + add_column :notifications, :attachment, :string + end +end diff --git a/lib/mailboxer/models/messageable.rb b/lib/mailboxer/models/messageable.rb index c86c2762..0d7bb530 100644 --- a/lib/mailboxer/models/messageable.rb +++ b/lib/mailboxer/models/messageable.rb @@ -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 @@ -180,4 +180,4 @@ def untrash(obj) end end end -end \ No newline at end of file +end diff --git a/spec/dummy/db/migrate/20111205013142_add_attachments.rb b/spec/dummy/db/migrate/20111205013142_add_attachments.rb new file mode 100644 index 00000000..08e02c02 --- /dev/null +++ b/spec/dummy/db/migrate/20111205013142_add_attachments.rb @@ -0,0 +1,5 @@ +class AddAttachments < ActiveRecord::Migration + def change + add_column :notifications, :attachment, :string + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 2b9af3cb..373908bd 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -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 => "" @@ -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" diff --git a/spec/dummy/public/uploads/testfile.txt b/spec/dummy/public/uploads/testfile.txt new file mode 100644 index 00000000..1636f048 --- /dev/null +++ b/spec/dummy/public/uploads/testfile.txt @@ -0,0 +1 @@ +this is a dummy file to test file uploads diff --git a/spec/models/mailboxer_models_messageable_spec.rb b/spec/models/mailboxer_models_messageable_spec.rb index 196a1358..0455658b 100644 --- a/spec/models/mailboxer_models_messageable_spec.rb +++ b/spec/models/mailboxer_models_messageable_spec.rb @@ -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 diff --git a/spec/testfile.txt b/spec/testfile.txt new file mode 100644 index 00000000..1636f048 --- /dev/null +++ b/spec/testfile.txt @@ -0,0 +1 @@ +this is a dummy file to test file uploads