mailboxer 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/app/models/mailboxer_conversation.rb +44 -10
- data/app/models/mailboxer_mailbox.rb +16 -13
- data/lib/mailboxer/models/messageable.rb +14 -121
- data/mailboxer.gemspec +1 -1
- data/spec/models/mailboxer_mailbox_spec.rb +28 -28
- metadata +3 -3
@@ -2,10 +2,44 @@ class MailboxerConversation < ActiveRecord::Base
|
|
2
2
|
attr_reader :originator, :original_message, :last_sender, :last_message, :users
|
3
3
|
has_many :mailboxer_messages
|
4
4
|
has_many :mailboxer_mails, :through => :mailboxer_messages
|
5
|
-
before_create :clean
|
5
|
+
# before_create :clean
|
6
6
|
scope :participant, lambda {|participant|
|
7
|
-
joins(:
|
7
|
+
joins(:mailboxer_mails).select('DISTINCT mailboxer_conversations.*').where('mailboxer_mails.receiver_id' => participant.id,'mailboxer_mails.receiver_type' => participant.class.to_s)
|
8
8
|
}
|
9
|
+
scope :inbox, lambda {|participant|
|
10
|
+
joins(:mailboxer_mails).select('DISTINCT mailboxer_conversations.*').where('mailboxer_mails.receiver_id' => participant.id,'mailboxer_mails.receiver_type' => participant.class.to_s, 'mailboxer_mails.mailbox_type' => 'inbox')
|
11
|
+
}
|
12
|
+
scope :sentbox, lambda {|participant|
|
13
|
+
joins(:mailboxer_mails).select('DISTINCT mailboxer_conversations.*').where('mailboxer_mails.receiver_id' => participant.id,'mailboxer_mails.receiver_type' => participant.class.to_s, 'mailboxer_mails.mailbox_type' => 'sentbox')
|
14
|
+
}
|
15
|
+
scope :trash, joins(:mailboxer_mails).select('DISTINCT mailboxer_conversations.*').where('mailboxer_mails.trashed' => true)
|
16
|
+
scope :unread, joins(:mailboxer_mails).select('DISTINCT mailboxer_conversations.*').where('mailboxer_mails.read' => false)
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def total
|
20
|
+
count('DISTINCT mailboxer_conversations.id')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def mark_as_read(participant)
|
25
|
+
return if participant.nil?
|
26
|
+
return MailboxerMail.conversation(self).receiver(participant).mark_as_read
|
27
|
+
end
|
28
|
+
|
29
|
+
def mark_as_unread(participant)
|
30
|
+
return if participant.nil?
|
31
|
+
return MailboxerMail.conversation(self).receiver(participant).mark_as_unread
|
32
|
+
end
|
33
|
+
|
34
|
+
def move_to_trash(participant)
|
35
|
+
return if participant.nil?
|
36
|
+
return MailboxerMail.conversation(self).receiver(participant).move_to_trash
|
37
|
+
end
|
38
|
+
|
39
|
+
def untrash(participant)
|
40
|
+
return if participant.nil?
|
41
|
+
return MailboxerMail.conversation(self).receiver(participant).untrash
|
42
|
+
end
|
9
43
|
|
10
44
|
#originator of the conversation.
|
11
45
|
def originator
|
@@ -53,12 +87,12 @@ class MailboxerConversation < ActiveRecord::Base
|
|
53
87
|
return MailboxerMessage.conversation(self).count
|
54
88
|
end
|
55
89
|
|
56
|
-
protected
|
57
|
-
#[empty method]
|
58
|
-
#
|
59
|
-
#this gets called before_create. Implement this if you wish to clean out illegal content such as scripts or anything that will break layout. This is left empty because what is considered illegal content varies.
|
60
|
-
def clean
|
61
|
-
return if subject.nil?
|
62
|
-
#strip all illegal content here. (scripts, shit that will break layout, etc.)
|
63
|
-
end
|
90
|
+
# protected
|
91
|
+
# #[empty method]
|
92
|
+
# #
|
93
|
+
# #this gets called before_create. Implement this if you wish to clean out illegal content such as scripts or anything that will break layout. This is left empty because what is considered illegal content varies.
|
94
|
+
# def clean
|
95
|
+
# return if subject.nil?
|
96
|
+
# #strip all illegal content here. (scripts, shit that will break layout, etc.)
|
97
|
+
# end
|
64
98
|
end
|
@@ -15,25 +15,25 @@ class MailboxerMailbox
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def conversations(options = {})
|
18
|
-
return MailboxerConversation.where(options).participant(@messageable)
|
19
|
-
end
|
20
|
-
|
21
|
-
def mail(options = {})
|
22
|
-
return MailboxerMail.where(options).receiver(@messageable)
|
18
|
+
return MailboxerConversation.where(options).participant(@messageable).uniq
|
23
19
|
end
|
24
20
|
|
25
|
-
def inbox(options
|
26
|
-
return
|
21
|
+
def inbox(options={})
|
22
|
+
return MailboxerConversation.where(options).inbox(@messageable).uniq
|
27
23
|
end
|
28
24
|
|
29
|
-
def sentbox(options
|
30
|
-
return
|
25
|
+
def sentbox(options={})
|
26
|
+
return MailboxerConversation.where(options).sentbox(@messageable).uniq
|
31
27
|
end
|
32
28
|
|
33
|
-
def trash(options
|
34
|
-
return
|
29
|
+
def trash(options={})
|
30
|
+
return MailboxerConversation.where(options).participant(@messageable).trash.uniq
|
35
31
|
end
|
36
32
|
|
33
|
+
def mail(options = {})
|
34
|
+
return MailboxerMail.where(options).receiver(@messageable)
|
35
|
+
end
|
36
|
+
|
37
37
|
def [](mailbox_type)
|
38
38
|
self.type = mailbox_type
|
39
39
|
return self
|
@@ -54,7 +54,7 @@ class MailboxerMailbox
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def empty_trash(options = {})
|
57
|
-
return self.trash(options).delete_all
|
57
|
+
return self.mail.trash(options).delete_all
|
58
58
|
end
|
59
59
|
|
60
60
|
def has_conversation?(conversation)
|
@@ -62,7 +62,10 @@ class MailboxerMailbox
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def is_trashed?(conversation)
|
65
|
-
return self.trash.conversation(conversation).count!=0
|
65
|
+
return self.mail.trash.conversation(conversation).count!=0
|
66
|
+
end
|
67
|
+
def is_completely_trashed?(conversation)
|
68
|
+
return self.mail.trash.conversation(conversation).count==self.mail.conversation(conversation).count
|
66
69
|
end
|
67
70
|
|
68
71
|
end
|
@@ -5,67 +5,25 @@ module Mailboxer
|
|
5
5
|
def self.included(mod)
|
6
6
|
mod.extend(ClassMethods)
|
7
7
|
end
|
8
|
-
|
9
|
-
# will load the relevant instance methods
|
10
|
-
# defined below when invoked
|
8
|
+
|
11
9
|
module ClassMethods
|
12
|
-
|
13
|
-
#some modifications to the migrations and model classes will need to be made to use a model of different type
|
14
|
-
#
|
15
|
-
#====options:
|
16
|
-
#* :received - the mailbox type to store received messages (defaults to :inbox)
|
17
|
-
#
|
18
|
-
#* :sent - the mailbox type to store sent messages (defaults to :sentbox)
|
19
|
-
#
|
20
|
-
#* :deleted - the mailbox type to store deleted messages (defaults to :trash)
|
21
|
-
#
|
22
|
-
#====example:
|
23
|
-
# acts_as_messageable :received => :in, :sent => :sent, :deleted => :garbage
|
10
|
+
|
24
11
|
def acts_as_messageable
|
25
12
|
has_many :mailboxer_messages
|
26
|
-
cattr_accessor :mailbox_types
|
27
13
|
has_many :mailboxer_mails, :order => 'created_at DESC', :dependent => :delete_all
|
28
14
|
|
29
15
|
include Mailboxer::Models::Messageable::InstanceMethods
|
30
16
|
end
|
31
17
|
end
|
32
|
-
|
33
|
-
#Adds class methods
|
34
|
-
#module SingletonMethods
|
35
|
-
#end
|
36
|
-
|
37
|
-
# Adds instance methods.
|
18
|
+
|
38
19
|
module InstanceMethods
|
39
|
-
|
40
|
-
#see Mailbox for more details.
|
41
|
-
#
|
42
|
-
#====example:
|
43
|
-
# phil = User.find(3123)
|
44
|
-
# phil.mailbox[:inbox].unread_mail #returns all unread mail in your inbox
|
45
|
-
# phil.mailbox[:sentbox].mail #returns all sent mail messages
|
46
|
-
#
|
20
|
+
|
47
21
|
def mailbox
|
48
22
|
@mailbox = MailboxerMailbox.new(self) if @mailbox.nil?
|
49
23
|
@mailbox.type = :all
|
50
24
|
return @mailbox
|
51
25
|
end
|
52
|
-
|
53
|
-
#
|
54
|
-
#====params:
|
55
|
-
#recipients::
|
56
|
-
# a single user object or array of users to deliver the message to.
|
57
|
-
#msg_body::
|
58
|
-
# the body of the message.
|
59
|
-
#subject::
|
60
|
-
# the subject of the message, defaults to empty string if not provided.
|
61
|
-
#====returns:
|
62
|
-
#the sent Mail.
|
63
|
-
#
|
64
|
-
#====example:
|
65
|
-
# phil = User.find(3123)
|
66
|
-
# todd = User.find(4141)
|
67
|
-
# phil.send_message(todd, 'whats up for tonight?', 'hey guy') #sends a Mail message to todd's inbox, and a Mail message to phil's sentbox
|
68
|
-
#
|
26
|
+
|
69
27
|
def send_message(recipients, msg_body, subject = '')
|
70
28
|
convo = MailboxerConversation.create({:subject => subject})
|
71
29
|
message = MailboxerMessage.create({:sender => self, :mailboxer_conversation => convo, :body => msg_body, :subject => subject})
|
@@ -73,24 +31,7 @@ module Mailboxer
|
|
73
31
|
message.deliver(:inbox)
|
74
32
|
return mailbox[:sentbox] << message
|
75
33
|
end
|
76
|
-
|
77
|
-
#
|
78
|
-
#*explicitly calling this method is rare unless you are replying to a subset of the users involved in the conversation or
|
79
|
-
#if you are including someone that is not currently in the conversation.
|
80
|
-
#reply_to_sender, reply_to_all, and reply_to_conversation will suffice in most cases.
|
81
|
-
#
|
82
|
-
#====params:
|
83
|
-
#conversation::
|
84
|
-
# the Conversation object that the mail you are responding to belongs.
|
85
|
-
#recipients::
|
86
|
-
# a single User object or array of Users to deliver the reply message to.
|
87
|
-
#reply_body::
|
88
|
-
# the body of the reply message.
|
89
|
-
#subject::
|
90
|
-
# the subject of the message, defaults to 'RE: [original subject]' if one isnt given.
|
91
|
-
#====returns:
|
92
|
-
#the sent Mail.
|
93
|
-
#
|
34
|
+
|
94
35
|
def reply(conversation, recipients, reply_body, subject = nil)
|
95
36
|
return nil if(reply_body.blank?)
|
96
37
|
subject = subject || "RE: #{conversation.subject}"
|
@@ -99,33 +40,11 @@ module Mailboxer
|
|
99
40
|
response.deliver(:inbox)
|
100
41
|
return mailbox[:sentbox] << response
|
101
42
|
end
|
102
|
-
|
103
|
-
#
|
104
|
-
#====params:
|
105
|
-
#mail::
|
106
|
-
# the Mail object that you are replying to.
|
107
|
-
#reply_body::
|
108
|
-
# the body of the reply message.
|
109
|
-
#subject::
|
110
|
-
# the subject of the message, defaults to 'RE: [original subject]' if one isnt given.
|
111
|
-
#====returns:
|
112
|
-
#the sent Mail.
|
113
|
-
#
|
43
|
+
|
114
44
|
def reply_to_sender(mail, reply_body, subject = nil)
|
115
45
|
return reply(mail.mailboxer_conversation, mail.mailboxer_message.sender, reply_body, subject)
|
116
46
|
end
|
117
|
-
|
118
|
-
#
|
119
|
-
#====params:
|
120
|
-
#mail::
|
121
|
-
# the Mail object that you are replying to.
|
122
|
-
#reply_body::
|
123
|
-
# the body of the reply message.
|
124
|
-
#subject::
|
125
|
-
# the subject of the message, defaults to 'RE: [original subject]' if one isnt given.
|
126
|
-
#====returns:
|
127
|
-
#the sent Mail.
|
128
|
-
#
|
47
|
+
|
129
48
|
def reply_to_all(mail, reply_body, subject = nil)
|
130
49
|
msg = mail.mailboxer_message
|
131
50
|
recipients = msg.get_recipients
|
@@ -137,20 +56,7 @@ module Mailboxer
|
|
137
56
|
end
|
138
57
|
return reply(mail.mailboxer_conversation, recipients, reply_body, subject)
|
139
58
|
end
|
140
|
-
|
141
|
-
#
|
142
|
-
#*this may have undesired effects if users have been added to the conversation after it has begun.
|
143
|
-
#
|
144
|
-
#====params:
|
145
|
-
#conversation::
|
146
|
-
# the Conversation object that the mail you are responding to belongs.
|
147
|
-
#reply_body::
|
148
|
-
# the body of the reply message.
|
149
|
-
#subject::
|
150
|
-
# the subject of the message, defaults to 'RE: [original subject]' if one isnt given.
|
151
|
-
#====returns:
|
152
|
-
#the sent Mail.
|
153
|
-
#
|
59
|
+
|
154
60
|
def reply_to_conversation(conversation, reply_body, subject = nil)
|
155
61
|
#move conversation to inbox if it is currently in the trash - doesnt make much sense replying to a trashed convo.
|
156
62
|
if(mailbox.is_trashed?(conversation))
|
@@ -165,29 +71,16 @@ module Mailboxer
|
|
165
71
|
end
|
166
72
|
end
|
167
73
|
return reply(conversation,recipients, reply_body, subject)
|
168
|
-
end
|
169
|
-
|
74
|
+
end
|
75
|
+
|
170
76
|
def read_mail(mail)
|
171
77
|
return mail.mark_as_read if mail.receiver == self
|
172
|
-
end
|
173
|
-
|
78
|
+
end
|
79
|
+
|
174
80
|
def unread_mail(mail)
|
175
81
|
return mail.mark_as_unread if mail.receiver == self
|
176
82
|
end
|
177
|
-
|
178
|
-
#All mail is marked as read but the returning array is built before this so you can see which messages were unread when viewing the conversation.
|
179
|
-
#
|
180
|
-
#???This returns deleted/trashed messages as well for the purpose of reading trashed convos, to disable this send the option ':conditions => "mail.trashed != true"'
|
181
|
-
#
|
182
|
-
#====params:
|
183
|
-
#conversation::
|
184
|
-
# the Conversation object that you want to read.
|
185
|
-
#options::
|
186
|
-
# any options to filter the conversation, these are used as find options so all valid options for find will work.
|
187
|
-
#
|
188
|
-
#====returns:
|
189
|
-
#array of Mail belonging to the given conversation.
|
190
|
-
#
|
83
|
+
|
191
84
|
def read_conversation(conversation, options = {})
|
192
85
|
mails = conversation.mailboxer_mails.receiver(self)
|
193
86
|
mails_clone = mails.clone
|
data/mailboxer.gemspec
CHANGED
@@ -45,55 +45,55 @@ describe MailboxerMailbox do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should return sentbox" do
|
48
|
-
assert @entity1.mailbox.inbox
|
49
|
-
@entity1.mailbox.sentbox.count.should==2
|
50
|
-
@entity1.mailbox.sentbox[0].should==@mail1
|
51
|
-
@entity1.mailbox.sentbox[1].should==@mail3
|
48
|
+
assert @entity1.mailbox.mail.inbox
|
49
|
+
@entity1.mailbox.mail.sentbox.count.should==2
|
50
|
+
@entity1.mailbox.mail.sentbox[0].should==@mail1
|
51
|
+
@entity1.mailbox.mail.sentbox[1].should==@mail3
|
52
52
|
|
53
|
-
assert @entity2.mailbox.inbox
|
54
|
-
@entity2.mailbox.sentbox.count.should==2
|
55
|
-
@entity2.mailbox.sentbox[0].should==@mail2
|
56
|
-
@entity2.mailbox.sentbox[1].should==@mail4
|
53
|
+
assert @entity2.mailbox.mail.inbox
|
54
|
+
@entity2.mailbox.mail.sentbox.count.should==2
|
55
|
+
@entity2.mailbox.mail.sentbox[0].should==@mail2
|
56
|
+
@entity2.mailbox.mail.sentbox[1].should==@mail4
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should return inbox" do
|
60
|
-
assert @entity1.mailbox.inbox
|
61
|
-
@entity1.mailbox.inbox.count.should==2
|
62
|
-
@entity1.mailbox.inbox[0].should==MailboxerMail.receiver(@entity1).inbox.conversation(@conversation)[0]
|
63
|
-
@entity1.mailbox.inbox[1].should==MailboxerMail.receiver(@entity1).inbox.conversation(@conversation)[1]
|
60
|
+
assert @entity1.mailbox.mail.inbox
|
61
|
+
@entity1.mailbox.mail.inbox.count.should==2
|
62
|
+
@entity1.mailbox.mail.inbox[0].should==MailboxerMail.receiver(@entity1).inbox.conversation(@conversation)[0]
|
63
|
+
@entity1.mailbox.mail.inbox[1].should==MailboxerMail.receiver(@entity1).inbox.conversation(@conversation)[1]
|
64
64
|
|
65
|
-
assert @entity2.mailbox.inbox
|
66
|
-
@entity2.mailbox.inbox.count.should==2
|
67
|
-
@entity2.mailbox.inbox[0].should==MailboxerMail.receiver(@entity2).inbox.conversation(@conversation)[0]
|
68
|
-
@entity2.mailbox.inbox[1].should==MailboxerMail.receiver(@entity2).inbox.conversation(@conversation)[1]
|
65
|
+
assert @entity2.mailbox.mail.inbox
|
66
|
+
@entity2.mailbox.mail.inbox.count.should==2
|
67
|
+
@entity2.mailbox.mail.inbox[0].should==MailboxerMail.receiver(@entity2).inbox.conversation(@conversation)[0]
|
68
|
+
@entity2.mailbox.mail.inbox[1].should==MailboxerMail.receiver(@entity2).inbox.conversation(@conversation)[1]
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should return trashed mails" do
|
72
72
|
@entity1.mailbox.mail.move_to_trash
|
73
73
|
|
74
|
-
assert @entity1.mailbox.trash
|
75
|
-
@entity1.mailbox.trash.count.should==4
|
76
|
-
@entity1.mailbox.trash[0].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[0]
|
77
|
-
@entity1.mailbox.trash[1].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[1]
|
78
|
-
@entity1.mailbox.trash[2].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[2]
|
79
|
-
@entity1.mailbox.trash[3].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[3]
|
74
|
+
assert @entity1.mailbox.mail.trash
|
75
|
+
@entity1.mailbox.mail.trash.count.should==4
|
76
|
+
@entity1.mailbox.mail.trash[0].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[0]
|
77
|
+
@entity1.mailbox.mail.trash[1].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[1]
|
78
|
+
@entity1.mailbox.mail.trash[2].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[2]
|
79
|
+
@entity1.mailbox.mail.trash[3].should==MailboxerMail.receiver(@entity1).conversation(@conversation)[3]
|
80
80
|
|
81
|
-
assert @entity2.mailbox.trash
|
82
|
-
@entity2.mailbox.trash.count.should==0
|
81
|
+
assert @entity2.mailbox.mail.trash
|
82
|
+
@entity2.mailbox.mail.trash.count.should==0
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should delete trashed mails" do
|
86
86
|
@entity1.mailbox.mail.move_to_trash
|
87
87
|
@entity1.mailbox.empty_trash
|
88
88
|
|
89
|
-
assert @entity1.mailbox.trash
|
90
|
-
@entity1.mailbox.trash.count.should==0
|
89
|
+
assert @entity1.mailbox.mail.trash
|
90
|
+
@entity1.mailbox.mail.trash.count.should==0
|
91
91
|
|
92
92
|
assert @entity2.mailbox.mail
|
93
93
|
@entity2.mailbox.mail.count.should==4
|
94
94
|
|
95
|
-
assert @entity2.mailbox.trash
|
96
|
-
@entity2.mailbox.trash.count.should==0
|
95
|
+
assert @entity2.mailbox.mail.trash
|
96
|
+
@entity2.mailbox.mail.trash.count.should==0
|
97
97
|
end
|
98
98
|
|
99
99
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailboxer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Eduardo Casanova Cuesta
|