Skip to content

Commit

Permalink
Drop Carrierwave in favor of an attachments model.
Browse files Browse the repository at this point in the history
  • Loading branch information
rposborne committed May 26, 2017
1 parent ecf0e59 commit c8afe5e
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ class Cylon < ActiveRecord::Base
end
```

## Attachments

We are "uploader" agnostic but we require that the attachment is attached to a
polymorphic object that exposes two methods, a name and a file.


## Mailboxer API

### Warning for version 0.8.0
Expand Down
7 changes: 7 additions & 0 deletions app/mailers/mailboxer/base_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ def strip_tags(text)
::Mailboxer::Cleaner.instance.strip_tags(text)
end

def attach_message_attachments
return unless @message.respond_to?(:attachments)
@message.attachments.each do |attachment|
attachments[attachment.send(Mailboxer.attachment_filename_method)] =
attachment.send(Mailboxer.attachment_file_method)
end
end
end
4 changes: 4 additions & 0 deletions app/mailers/mailboxer/message_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def new_message_email(message,receiver)
@message = message
@receiver = receiver
set_subject(message)
attach_message_attachments

mail :to => receiver.send(Mailboxer.email_method, message),
:subject => t('mailboxer.message_mailer.subject_new', :subject => @subject),
:template_name => 'new_message_email'
Expand All @@ -25,6 +27,8 @@ def reply_message_email(message,receiver)
@message = message
@receiver = receiver
set_subject(message)
attach_message_attachments

mail :to => receiver.send(Mailboxer.email_method, message),
:subject => t('mailboxer.message_mailer.subject_reply', :subject => @subject),
:template_name => 'reply_message_email'
Expand Down
1 change: 1 addition & 0 deletions app/mailers/mailboxer/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def new_notification_email(notification, receiver)
@notification = notification
@receiver = receiver
set_subject(notification)
attach_message_attachments
mail :to => receiver.send(Mailboxer.email_method, notification),
:subject => t('mailboxer.notification_mailer.subject', :subject => @subject),
:template_name => 'new_notification_email'
Expand Down
8 changes: 5 additions & 3 deletions app/models/mailboxer/message.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class Mailboxer::Message < Mailboxer::Notification
attr_accessible :attachment if Mailboxer.protected_attributes?
self.table_name = :mailboxer_notifications

belongs_to :conversation, :validate => true, :autosave => true

if Mailboxer.attachments_model
has_many :attachments, as: Mailboxer.attachments_model.table_name.singularize.downcase + 'able', class_name: Mailboxer.attachments_model.to_s
end

validates_presence_of :sender

class_attribute :on_deliver_callback
Expand All @@ -11,8 +15,6 @@ class Mailboxer::Message < Mailboxer::Notification
where(:conversation_id => conversation.id)
}

mount_uploader :attachment, Mailboxer::AttachmentUploader

class << self
#Sets the on deliver callback method.
def on_deliver(callback_method)
Expand Down
3 changes: 0 additions & 3 deletions app/uploaders/mailboxer/attachment_uploader.rb

This file was deleted.

1 change: 0 additions & 1 deletion db/migrate/20110511145103_create_mailboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def self.up
t.column :draft, :boolean, :default => false
t.string :notification_code, :default => nil
t.references :notified_object, :polymorphic => true, index: { name: 'mailboxer_notifications_notified_object' }
t.column :attachment, :string
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
t.boolean :global, default: false
Expand Down
5 changes: 5 additions & 0 deletions lib/mailboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module Models
mattr_accessor :notification_mailer
mattr_accessor :message_mailer
mattr_accessor :custom_deliver_proc
mattr_accessor :attachments_model
mattr_accessor :attachment_file_method
@@attachment_file_method = :file
mattr_accessor :attachment_filename_method
@@attachment_file_method = :filename

class << self
def setup
Expand Down
1 change: 0 additions & 1 deletion lib/mailboxer/engine.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'carrierwave'
begin
require 'sunspot_rails'
rescue LoadError
Expand Down
20 changes: 10 additions & 10 deletions lib/mailboxer/models/messageable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def unread_inbox_count

#Sends a messages, starting a new conversation, with the messageable
#as originator
def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)
def send_message(recipients, msg_body, subject, sanitize_text=true, attachments=nil, message_timestamp = Time.now)
convo = Mailboxer::ConversationBuilder.new({
:subject => subject,
:created_at => message_timestamp,
Expand All @@ -70,7 +70,7 @@ def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=n
:recipients => recipients,
:body => msg_body,
:subject => subject,
:attachment => attachment,
:attachments => attachments,
:created_at => message_timestamp,
:updated_at => message_timestamp
}).build
Expand All @@ -80,41 +80,41 @@ def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=n

#Basic reply method. USE NOT RECOMENDED.
#Use reply_to_sender, reply_to_all and reply_to_conversation instead.
def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true, attachment=nil)
def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true, attachments=nil)
subject = subject || "#{conversation.subject}"
response = Mailboxer::MessageBuilder.new({
:sender => self,
:conversation => conversation,
:recipients => recipients,
:body => reply_body,
:subject => subject,
:attachment => attachment
:attachments => attachments
}).build

response.recipients.delete(self)
response.deliver true, sanitize_text
end

#Replies to the sender of the message in the conversation
def reply_to_sender(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
reply(receipt.conversation, receipt.message.sender, reply_body, subject, sanitize_text, attachment)
def reply_to_sender(receipt, reply_body, subject=nil, sanitize_text=true, attachments=nil)
reply(receipt.conversation, receipt.message.sender, reply_body, subject, sanitize_text, attachments)
end

#Replies to all the recipients of the message in the conversation
def reply_to_all(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
reply(receipt.conversation, receipt.message.recipients, reply_body, subject, sanitize_text, attachment)
def reply_to_all(receipt, reply_body, subject=nil, sanitize_text=true, attachments=nil)
reply(receipt.conversation, receipt.message.recipients, reply_body, subject, sanitize_text, attachments)
end

#Replies to all the recipients of the last message in the conversation and untrash any trashed message by messageable
#if should_untrash is set to true (this is so by default)
def reply_to_conversation(conversation, reply_body, subject=nil, should_untrash=true, sanitize_text=true, attachment=nil)
def reply_to_conversation(conversation, reply_body, subject=nil, should_untrash=true, sanitize_text=true, attachments=nil)
#move conversation to inbox if it is currently in the trash and should_untrash parameter is true.
if should_untrash && mailbox.is_trashed?(conversation)
mailbox.receipts_for(conversation).untrash
mailbox.receipts_for(conversation).mark_as_not_deleted
end

reply(conversation, conversation.last_message.recipients, reply_body, subject, sanitize_text, attachment)
reply(conversation, conversation.last_message.recipients, reply_body, subject, sanitize_text, attachments)
end

#Mark the object as read for messageable.
Expand Down
1 change: 0 additions & 1 deletion mailboxer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |s|

# Development Gem dependencies
s.add_runtime_dependency('rails', '>= 5.0.0')
s.add_runtime_dependency('carrierwave', '>= 0.5.8')

if RUBY_ENGINE == "rbx" && RUBY_VERSION >= "2.1.0"
# Rubinius has it's own dependencies
Expand Down
12 changes: 12 additions & 0 deletions spec/dummy/app/models/fake_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# It's fake
class FakeAttachment < ActiveRecord::Base
belongs_to :fake_attachmentable, polymorphic: true

def file
File.read('spec/testfile.txt')
end

def filename
'testfile.txt'
end
end
5 changes: 5 additions & 0 deletions spec/dummy/config/initializers/mailboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
#Configures maximum length of the message subject and body
config.subject_max_length = 255
config.body_max_length = 32000

# Configure Attachments
config.attachments_model = FakeAttachment
config.attachment_file_method = :file
config.attachment_filename_method = :filename
end
16 changes: 16 additions & 0 deletions spec/dummy/db/migrate/20160306015107_create_fake_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateFakeAttachments < ActiveRecord::Migration[4.2]
def self.up
create_table :fake_attachments do |t|
t.integer :fake_attachmentable_id
t.string :fake_attachmentable_type
t.string :file
t.string :filename

t.timestamps null: false
end
end

def self.down
drop_table :fake_attachments
end
end
2 changes: 1 addition & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
t.string "notification_code"
t.integer "conversation_id"
t.boolean "draft", :default => false
t.string "attachment"
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.boolean "global", :default => false
Expand All @@ -62,6 +61,7 @@
create_table "mailboxer_receipts", :force => true do |t|
t.integer "receiver_id"
t.string "receiver_type"
t.string "message_id"
t.integer "notification_id", :null => false
t.boolean "is_read", :default => false
t.boolean "trashed", :default => false
Expand Down
4 changes: 2 additions & 2 deletions spec/models/mailboxer_models_messageable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@
end

it "should be able to read attachment" do
@receipt = @entity1.send_message(@entity2, "Body", "Subject", nil, File.open('spec/testfile.txt'))
@receipt = @entity1.send_message(@entity2, "Body", "Subject", nil, [FakeAttachment.create])
@conversation = @receipt.conversation
expect(@conversation.messages.first.attachment_identifier).to eq 'testfile.txt'
expect(@receipt.message.attachments.first.filename).to eq 'testfile.txt'
end

it "should be the same message time as passed" do
Expand Down

0 comments on commit c8afe5e

Please sign in to comment.