mailboxer 0.2.1 → 0.2.2

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.
@@ -8,6 +8,12 @@ class Notification < ActiveRecord::Base
8
8
 
9
9
  scope :recipient, lambda { |recipient|
10
10
  joins(:receipts).where('receipts.receiver_id' => recipient.id,'receipts.receiver_type' => recipient.class.to_s)
11
+ }
12
+ scope :not_trashed, lambda {
13
+ joins(:receipts).where('receipts.trashed' => false)
14
+ }
15
+ scope :unread, lambda {
16
+ joins(:receipts).where('receipts.read' => false)
11
17
  }
12
18
 
13
19
  class << self
@@ -43,7 +49,8 @@ class Notification < ActiveRecord::Base
43
49
  temp_receipts.each(&:save!) #Save receipts
44
50
  self.recipients=nil
45
51
  end
46
- return temp_receipts
52
+ return temp_receipts if temp_receipts.size > 1
53
+ return temp_receipts.first
47
54
  end
48
55
 
49
56
  #Returns the recipients of the Notification
@@ -62,6 +69,11 @@ class Notification < ActiveRecord::Base
62
69
  def receipt_for(participant)
63
70
  return Receipt.notification(self).recipient(participant)
64
71
  end
72
+
73
+ #Returns the receipt for the participant. Alias for receipt_for(participant)
74
+ def receipts_for(participant)
75
+ return receipt_for(participant)
76
+ end
65
77
 
66
78
  #Returns if the participant have read the Notification
67
79
  def is_unread?(participant)
@@ -69,6 +81,37 @@ class Notification < ActiveRecord::Base
69
81
  return self.receipt_for(participant).first.read
70
82
  end
71
83
 
84
+ #Returns if the participant have trashed the Notification
85
+ def is_trashed?(participant)
86
+ return false if participant.nil?
87
+ return self.receipt_for(participant).first.trashed
88
+ end
89
+
90
+ #Mark the notification as read
91
+ def mark_as_read(participant)
92
+ return if participant.nil?
93
+ return self.receipt_for(participant).mark_as_read
94
+ end
95
+
96
+ #Mark the notification as unread
97
+ def mark_as_unread(participant)
98
+ return if participant.nil?
99
+ return self.receipt_for(participant).mark_as_unread
100
+ end
101
+
102
+ #Move the notification to the trash
103
+ def move_to_trash(participant)
104
+ return if participant.nil?
105
+ return self.receipt_for(participant).move_to_trash
106
+ end
107
+
108
+ #Takes the notification out of the trash
109
+ def untrash(participant)
110
+ return if participant.nil?
111
+ return self.receipt_for(participant).untrash
112
+ end
113
+
114
+
72
115
  include ActionView::Helpers::SanitizeHelper
73
116
 
74
117
  #Sanitizes the body and subject
@@ -65,12 +65,13 @@ class Receipt < ActiveRecord::Base
65
65
  where(options).each do |rcp|
66
66
  ids << rcp.id
67
67
  end
68
- temp_ids = ids.clone
68
+ return if ids.empty?
69
+ conditions = [""].concat(ids)
69
70
  condition = "id = ? "
70
- temp_ids.drop(1).each do
71
+ ids.drop(1).each do
71
72
  condition << "OR id = ? "
72
73
  end
73
- conditions = [condition].concat(ids)
74
+ conditions[0] = condition
74
75
  Receipt.except(:where).except(:joins).where(conditions).update_all(updates)
75
76
  end
76
77
  end
@@ -111,6 +112,17 @@ class Receipt < ActiveRecord::Base
111
112
  return nil
112
113
  end
113
114
 
115
+ #Returns if the participant have read the Notification
116
+ def is_unread?
117
+ return !self.read
118
+ end
119
+
120
+ #Returns if the participant have trashed the Notification
121
+ def is_trashed?
122
+ return self.trashed
123
+ end
124
+
125
+
114
126
  protected
115
127
 
116
128
  #Removes the duplicate error about not present subject from Conversation if it has been already
@@ -107,11 +107,9 @@ module Mailboxer
107
107
  when Receipt
108
108
  return obj.mark_as_read if obj.receiver == self
109
109
  when Message, Notification
110
- receipt = obj.receipt_for(self)
111
- return receipt.mark_as_read
110
+ obj.mark_as_read(self)
112
111
  when Conversation
113
- receipts = obj.receipts_for(self)
114
- return receipts.mark_as_read
112
+ obj.mark_as_read(self)
115
113
  when Array
116
114
  obj.map{ |sub_obj| read(sub_obj) }
117
115
  else
@@ -132,17 +130,62 @@ module Mailboxer
132
130
  when Receipt
133
131
  return obj.mark_as_unread if obj.receiver == self
134
132
  when Message, Notification
135
- receipt = obj.receipt_for(self)
136
- return receipt.mark_as_unread
133
+ obj.mark_as_unread(self)
137
134
  when Conversation
138
- receipts = obj.receipts_for(self)
139
- return receipts.mark_as_unread
135
+ obj.mark_as_unread(self)
140
136
  when Array
141
137
  obj.map{ |sub_obj| unread(sub_obj) }
142
138
  else
143
139
  return nil
144
140
  end
145
141
  end
142
+
143
+
144
+ #Mark the object as trashed for messageable.
145
+ #
146
+ #Object can be:
147
+ #* A Receipt
148
+ #* A Message
149
+ #* A Notification
150
+ #* A Conversation
151
+ #* An array with any of them
152
+ def trash(obj)
153
+ case obj
154
+ when Receipt
155
+ return obj.move_to_trash if obj.receiver == self
156
+ when Message, Notification
157
+ obj.move_to_trash(self)
158
+ when Conversation
159
+ obj.move_to_trash(self)
160
+ when Array
161
+ obj.map{ |sub_obj| trash(sub_obj) }
162
+ else
163
+ return nil
164
+ end
165
+ end
166
+
167
+ #Mark the object as not trashed for messageable.
168
+ #
169
+ #Object can be:
170
+ #* A Receipt
171
+ #* A Message
172
+ #* A Notification
173
+ #* A Conversation
174
+ #* An array with any of them
175
+ def untrash(obj)
176
+ case obj
177
+ when Receipt
178
+ return obj.untrash if obj.receiver == self
179
+ when Message, Notification
180
+ obj.untrash(self)
181
+ when Conversation
182
+ obj.untrash(self)
183
+ when Array
184
+ obj.map{ |sub_obj| untrash(sub_obj) }
185
+ else
186
+ return nil
187
+ end
188
+ end
146
189
  end
147
190
  end
148
191
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "mailboxer"
3
- s.version = "0.2.1"
3
+ s.version = "0.2.2"
4
4
  s.authors = ["Eduardo Casanova Cuesta"]
5
5
  s.summary = "Messaging system for rails apps."
6
6
  s.description = "A Rails engine that allows any model to act as messageable, permitting it interchange messages with any other messageable model." +
@@ -97,4 +97,5 @@ describe Mailbox do
97
97
  @entity2.mailbox.receipts.trash.count.should==0
98
98
  end
99
99
 
100
+
100
101
  end
@@ -25,18 +25,16 @@ describe "Mailboxer::Models::Messageable through User" do
25
25
  assert @entity2.reply_to_all(@receipt,"Reply body")
26
26
  end
27
27
 
28
- it "should be able to reply to conversation (TODO)" do
29
- #TODO
30
- end
31
28
 
32
- it "should be able to unread an owned mail (mark as unread)" do
29
+
30
+ it "should be able to unread an owned Receipt (mark as unread)" do
33
31
  @receipt = @entity1.send_message(@entity2,"Body","Subject")
34
32
  @receipt.read.should==true
35
33
  @entity1.unread(@receipt)
36
34
  @receipt.read.should==false
37
35
  end
38
36
 
39
- it "should be able to read an owned mail (mark as read)" do
37
+ it "should be able to read an owned Receipt (mark as read)" do
40
38
  @receipt = @entity1.send_message(@entity2,"Body","Subject")
41
39
  @receipt.read.should==true
42
40
  @entity1.unread(@receipt)
@@ -44,14 +42,14 @@ describe "Mailboxer::Models::Messageable through User" do
44
42
  @receipt.read.should==true
45
43
  end
46
44
 
47
- it "should be able to unread anot owned mail (mark as unread)" do
45
+ it "should not be able to unread a not owned Receipt (mark as unread)" do
48
46
  @receipt = @entity1.send_message(@entity2,"Body","Subject")
49
47
  @receipt.read.should==true
50
48
  @entity2.unread(@receipt) #Should not change
51
49
  @receipt.read.should==true
52
50
  end
53
51
 
54
- it "should be able to read a not owned mail (mark as read)" do
52
+ it "should not be able to read a not owned Receipt (mark as read)" do
55
53
  @receipt = @entity1.send_message(@entity2,"Body","Subject")
56
54
  @receipt.read.should==true
57
55
  @entity1.unread(@receipt) #From read to unread
@@ -59,49 +57,246 @@ describe "Mailboxer::Models::Messageable through User" do
59
57
  @receipt.read.should==false
60
58
  end
61
59
 
62
- =begin
63
- it "should be able to read owned mails of a conversation" do
64
- @receipt1 = @entity1.send_message(@entity2,"Body","Subject")
65
- @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1")
66
- @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2")
67
- @receipt4 = @entity2.reply_to_all(@receipt3,"Reply body 3")
68
- @message1 = @receipt1.message
69
- @conversation = @message1.conversation
70
-
71
- @conversation.mark_as_read(@entity1)
72
-
73
- @receipts = @conversation.receipts(@entity1)
74
-
75
- @receipts.each do |mail|
76
- mail.read.should==true
77
- end
78
- end
79
-
80
- it "should not be able to read not owned mails of a conversation" do
81
- @receipt1 = @entity1.send_message(@entity2,"Body","Subject")
82
- @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1")
83
- @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2")
84
- @receipt4 = @entity2.reply_to_all(@receipt3,"Reply body 3")
85
- @message1 = @receipt1.message
86
- @conversation = @message1.conversation
87
-
88
- @conversation.mark_as_read(@entity2)
89
-
90
- @receipts = @conversation.receipts(@entity1)
91
- @receipts_total = @conversation.receipts
92
-
93
- unread_mails = 0
94
-
95
- @receipts.each do |mail|
96
- unread_mails+=1 if !mail.read
97
- end
98
-
99
- unread_mails.should==2
100
-
101
-
102
-
103
- end
104
- =end
60
+ it "should be able to trash an owned Receipt" do
61
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
62
+ @receipt.trashed.should==false
63
+ @entity1.trash(@receipt)
64
+ @receipt.trashed.should==true
65
+ end
66
+
67
+ it "should be able to untrash an owned Receipt" do
68
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
69
+ @receipt.trashed.should==false
70
+ @entity1.trash(@receipt)
71
+ @entity1.untrash(@receipt)
72
+ @receipt.trashed.should==false
73
+ end
74
+
75
+ it "should not be able to trash a not owned Receipt" do
76
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
77
+ @receipt.trashed.should==false
78
+ @entity2.trash(@receipt) #Should not change
79
+ @receipt.trashed.should==false
80
+ end
81
+
82
+ it "should not be able to untrash a not owned Receipt" do
83
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
84
+ @receipt.trashed.should==false
85
+ @entity1.trash(@receipt) #From read to unread
86
+ @entity2.untrash(@receipt) #Should not change
87
+ @receipt.trashed.should==true
88
+ end
89
+
90
+
91
+
92
+ it "should be able to unread an owned Message (mark as unread)" do
93
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
94
+ @message = @receipt.message
95
+ @receipt.read.should==true
96
+ @entity1.unread(@message)
97
+ @message.receipt_for(@entity1).first.read.should==false
98
+ end
99
+
100
+ it "should be able to read an owned Message (mark as read)" do
101
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
102
+ @message = @receipt.message
103
+ @receipt.read.should==true
104
+ @entity1.unread(@message)
105
+ @entity1.read(@message)
106
+ @message.receipt_for(@entity1).first.read.should==true
107
+ end
108
+
109
+ it "should not be able to unread a not owned Message (mark as unread)" do
110
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
111
+ @message = @receipt.message
112
+ @receipt.read.should==true
113
+ @entity2.unread(@message) #Should not change
114
+ @message.receipt_for(@entity1).first.read.should==true
115
+ end
116
+
117
+ it "should not be able to read a not owned Message (mark as read)" do
118
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
119
+ @message = @receipt.message
120
+ @receipt.read.should==true
121
+ @entity1.unread(@message) #From read to unread
122
+ @entity2.read(@message) #Should not change
123
+ @message.receipt_for(@entity1).first.read.should==false
124
+ end
125
+
126
+ it "should be able to trash an owned Message" do
127
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
128
+ @message = @receipt.message
129
+ @receipt.trashed.should==false
130
+ @entity1.trash(@message)
131
+ @message.receipt_for(@entity1).first.trashed.should==true
132
+ end
133
+
134
+ it "should be able to untrash an owned Message" do
135
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
136
+ @message = @receipt.message
137
+ @receipt.trashed.should==false
138
+ @entity1.trash(@message)
139
+ @entity1.untrash(@message)
140
+ @message.receipt_for(@entity1).first.trashed.should==false
141
+ end
142
+
143
+ it "should not be able to trash a not owned Message" do
144
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
145
+ @message = @receipt.message
146
+ @receipt.trashed.should==false
147
+ @entity2.trash(@message) #Should not change
148
+ @message.receipt_for(@entity1).first.trashed.should==false
149
+ end
150
+
151
+ it "should not be able to untrash a not owned Message" do
152
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
153
+ @message = @receipt.message
154
+ @receipt.trashed.should==false
155
+ @entity1.trash(@message) #From read to unread
156
+ @entity2.untrash(@message) #Should not change
157
+ @message.receipt_for(@entity1).first.trashed.should==true
158
+ end
159
+
160
+
161
+
162
+ it "should be able to unread an owned Notification (mark as unread)" do
163
+ @receipt = @entity1.notify("Subject","Body")
164
+ @notification = @receipt.notification
165
+ @receipt.read.should==false
166
+ @entity1.read(@notification)
167
+ @entity1.unread(@notification)
168
+ @notification.receipt_for(@entity1).first.read.should==false
169
+ end
170
+
171
+ it "should be able to read an owned Notification (mark as read)" do
172
+ @receipt = @entity1.notify("Subject","Body")
173
+ @notification = @receipt.notification
174
+ @receipt.read.should==false
175
+ @entity1.read(@notification)
176
+ @notification.receipt_for(@entity1).first.read.should==true
177
+ end
178
+
179
+ it "should not be able to unread a not owned Notification (mark as unread)" do
180
+ @receipt = @entity1.notify("Subject","Body")
181
+ @notification = @receipt.notification
182
+ @receipt.read.should==false
183
+ @entity1.read(@notification)
184
+ @entity2.unread(@notification)
185
+ @notification.receipt_for(@entity1).first.read.should==true
186
+ end
187
+
188
+ it "should not be able to read a not owned Notification (mark as read)" do
189
+ @receipt = @entity1.notify("Subject","Body")
190
+ @notification = @receipt.notification
191
+ @receipt.read.should==false
192
+ @entity2.read(@notification)
193
+ @notification.receipt_for(@entity1).first.read.should==false
194
+ end
195
+
196
+ it "should be able to trash an owned Notification" do
197
+ @receipt = @entity1.notify("Subject","Body")
198
+ @notification = @receipt.notification
199
+ @receipt.trashed.should==false
200
+ @entity1.trash(@notification)
201
+ @notification.receipt_for(@entity1).first.trashed.should==true
202
+ end
203
+
204
+ it "should be able to untrash an owned Notification" do
205
+ @receipt = @entity1.notify("Subject","Body")
206
+ @notification = @receipt.notification
207
+ @receipt.trashed.should==false
208
+ @entity1.trash(@notification)
209
+ @entity1.untrash(@notification)
210
+ @notification.receipt_for(@entity1).first.trashed.should==false
211
+ end
212
+
213
+ it "should not be able to trash a not owned Notification" do
214
+ @receipt = @entity1.notify("Subject","Body")
215
+ @notification = @receipt.notification
216
+ @receipt.trashed.should==false
217
+ @entity2.trash(@notification)
218
+ @notification.receipt_for(@entity1).first.trashed.should==false
219
+ end
220
+
221
+ it "should not be able to untrash a not owned Notification" do
222
+ @receipt = @entity1.notify("Subject","Body")
223
+ @notification = @receipt.notification
224
+ @receipt.trashed.should==false
225
+ @entity1.trash(@notification)
226
+ @entity2.untrash(@notification)
227
+ @notification.receipt_for(@entity1).first.trashed.should==true
228
+ end
229
+
230
+
231
+
232
+ it "should be able to unread an owned Conversation (mark as unread)" do
233
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
234
+ @conversation = @receipt.conversation
235
+ @receipt.read.should==true
236
+ @entity1.unread(@conversation)
237
+ @conversation.receipts_for(@entity1).first.read.should==false
238
+ end
239
+
240
+ it "should be able to read an owned Conversation (mark as read)" do
241
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
242
+ @conversation = @receipt.conversation
243
+ @receipt.read.should==true
244
+ @entity1.unread(@conversation)
245
+ @entity1.read(@conversation)
246
+ @conversation.receipts_for(@entity1).first.read.should==true
247
+ end
248
+
249
+ it "should not be able to unread a not owned Conversation (mark as unread)" do
250
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
251
+ @conversation = @receipt.conversation
252
+ @receipt.read.should==true
253
+ @entity2.unread(@conversation)
254
+ @conversation.receipts_for(@entity1).first.read.should==true
255
+ end
256
+
257
+ it "should not be able to read a not owned Conversation (mark as read)" do
258
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
259
+ @conversation = @receipt.conversation
260
+ @receipt.read.should==true
261
+ @entity1.unread(@conversation)
262
+ @entity2.read(@conversation)
263
+ @conversation.receipts_for(@entity1).first.read.should==false
264
+ end
265
+
266
+ it "should be able to trash an owned Conversation" do
267
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
268
+ @conversation = @receipt.conversation
269
+ @receipt.trashed.should==false
270
+ @entity1.trash(@conversation)
271
+ @conversation.receipts_for(@entity1).first.trashed.should==true
272
+ end
273
+
274
+ it "should be able to untrash an owned Conversation" do
275
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
276
+ @conversation = @receipt.conversation
277
+ @receipt.trashed.should==false
278
+ @entity1.trash(@conversation)
279
+ @entity1.untrash(@conversation)
280
+ @conversation.receipts_for(@entity1).first.trashed.should==false
281
+ end
282
+
283
+ it "should not be able to trash a not owned Conversation" do
284
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
285
+ @conversation = @receipt.conversation
286
+ @receipt.trashed.should==false
287
+ @entity2.trash(@conversation)
288
+ @conversation.receipts_for(@entity1).first.trashed.should==false
289
+ end
105
290
 
291
+ it "should not be able to untrash a not owned Conversation" do
292
+ @receipt = @entity1.send_message(@entity2,"Body","Subject")
293
+ @conversation = @receipt.conversation
294
+ @receipt.trashed.should==false
295
+ @entity1.trash(@conversation)
296
+ @entity2.untrash(@conversation)
297
+ @conversation.receipts_for(@entity1).first.trashed.should==true
298
+ end
299
+
300
+
106
301
 
107
302
  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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eduardo Casanova Cuesta