mailboxer 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,13 @@
1
1
  class Conversation < ActiveRecord::Base
2
- attr_reader :originator, :original_message, :last_sender, :last_message, :users
2
+ attr_reader :originator, :original_message, :last_sender, :last_message
3
+ attr_accessor :recipients, :body
3
4
  has_many :messages
4
5
  has_many :receipts, :through => :messages
5
6
 
7
+ validates_presence_of :recipients, :subject, :body
8
+
6
9
  before_create :clean
10
+ after_create :clear_recipients
7
11
 
8
12
  # before_create :clean
9
13
  scope :participant, lambda {|participant|
@@ -47,6 +51,15 @@ class Conversation < ActiveRecord::Base
47
51
  return self.receipts(participant).untrash
48
52
  end
49
53
 
54
+ def recipients
55
+ if @recipients.blank? and self.last_message
56
+ recps = self.last_message.recipients
57
+ recps = recps.is_a?(Array) ? recps : [recps]
58
+ return recps
59
+ end
60
+ return @recipients
61
+ end
62
+
50
63
  #originator of the conversation.
51
64
  def originator
52
65
  @orignator = self.original_message.sender if @originator.nil?
@@ -76,15 +89,6 @@ class Conversation < ActiveRecord::Base
76
89
  return Receipt.conversation(self)
77
90
  end
78
91
 
79
- #all users involved in the conversation.
80
- def recipients
81
- return last_message.get_recipients
82
- end
83
-
84
- def get_recipients
85
- return self.recipients
86
- end
87
-
88
92
  def count_messages
89
93
  return Message.conversation(self).count
90
94
  end
@@ -93,20 +97,20 @@ class Conversation < ActiveRecord::Base
93
97
  return false if participant.nil?
94
98
  return self.receipts(participant).count != 0
95
99
  end
96
-
100
+
97
101
  def is_trashed?(participant)
98
102
  return false if participant.nil?
99
- return self.receipts(participant).trash.count!=0
103
+ return self.receipts(participant).trash.count!=0
100
104
  end
101
-
105
+
102
106
  def is_completely_trashed?(participant)
103
107
  return false if participant.nil?
104
- return self.receipts(participant).trash.count==self.receipts(participant).count
108
+ return self.receipts(participant).trash.count==self.receipts(participant).count
105
109
  end
106
-
110
+
107
111
  def is_unread?(participant)
108
112
  return false if participant.nil?
109
- return self.receipts(participant).unread.count!=0
113
+ return self.receipts(participant).unread.count!=0
110
114
  end
111
115
  # protected
112
116
  # #[empty method]
@@ -117,9 +121,16 @@ class Conversation < ActiveRecord::Base
117
121
  # #strip all illegal content here. (scripts, shit that will break layout, etc.)
118
122
  # end
119
123
 
124
+ protected
125
+
120
126
  include ActionView::Helpers::SanitizeHelper
121
127
 
122
128
  def clean
123
129
  self.subject = sanitize self.subject
124
130
  end
131
+
132
+ def clear_recipients
133
+ self.recipients=nil
134
+ end
135
+
125
136
  end
@@ -25,16 +25,20 @@ class Message < ActiveRecord::Base
25
25
  self.recipients.each do |r|
26
26
  r.mailbox[mailbox_type] << self
27
27
  end
28
+ self.recipients=nil
28
29
  self.on_deliver_callback.call(self, mailbox_type) unless self.on_deliver_callback.nil?
29
30
  end
30
-
31
- def get_recipients
32
- recipients_array = Array.new
33
- self.receipts.each do |receipt|
34
- recipients_array << receipt.receiver
35
- end
36
- return recipients_array.uniq
37
- end
31
+
32
+ def recipients
33
+ if @recipients.blank?
34
+ recipients_array = Array.new
35
+ self.receipts.each do |receipt|
36
+ recipients_array << receipt.receiver
37
+ end
38
+ return recipients_array
39
+ end
40
+ return @recipients
41
+ end
38
42
 
39
43
  def receipts(participant=nil)
40
44
  return Receipt.message(self).receiver(participant) if participant
@@ -21,10 +21,11 @@ module Mailboxer
21
21
  return @mailbox
22
22
  end
23
23
 
24
- def send_message(recipients, msg_body, subject = '')
25
- convo = Conversation.create({:subject => subject})
24
+ def send_message(recipients, msg_body, subject)
25
+ convo = Conversation.create!({:recipients => recipients, :body => msg_body, :subject => subject})
26
26
  message = Message.create({:sender => self, :conversation => convo, :body => msg_body, :subject => subject})
27
27
  message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
28
+ message.recipients = message.recipients.uniq
28
29
  message.deliver(:inbox)
29
30
  return mailbox[:sentbox] << message
30
31
  end
@@ -34,7 +35,8 @@ module Mailboxer
34
35
  conversation.update_attribute(:updated_at, Time.now)
35
36
  subject = subject || "RE: #{conversation.subject}"
36
37
  response = Message.create({:sender => self, :conversation => conversation, :body => reply_body, :subject => subject})
37
- response.recipients = recipients.is_a?(Array) ? recipients : [recipients]
38
+ response.recipients = recipients.is_a?(Array) ? recipients : [recipients]
39
+ response.recipients = response.recipients.uniq
38
40
  response.recipients.delete(self)
39
41
  response.deliver(:inbox)
40
42
  return mailbox[:sentbox] << response
@@ -45,9 +47,7 @@ module Mailboxer
45
47
  end
46
48
 
47
49
  def reply_to_all(receipt, reply_body, subject = nil)
48
- msg = receipt.message
49
- recipients = msg.get_recipients
50
- return reply(receipt.conversation, recipients, reply_body, subject)
50
+ return reply(receipt.conversation, receipt.message.recipients, reply_body, subject)
51
51
  end
52
52
 
53
53
  def reply_to_conversation(conversation, reply_body, subject = nil)
@@ -56,7 +56,7 @@ module Mailboxer
56
56
  mailbox.receipts.conversation(conversation).untrash
57
57
  end
58
58
  #remove self from recipients unless you are the originator of the convo
59
- recipients = conversation.get_recipients
59
+ recipients = conversation.last_message.recipients
60
60
  if(conversation.originator != self)
61
61
  recipients.delete(self)
62
62
  if(!recipients.include?(conversation.originator))
@@ -1,7 +1,7 @@
1
1
 
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "mailboxer"
4
- s.version = "0.0.12"
4
+ s.version = "0.0.13"
5
5
 
6
6
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
7
  s.authors = ["Eduardo Casanova Cuesta"]
@@ -42,7 +42,7 @@ describe "Messages And Receipts" do
42
42
  end
43
43
 
44
44
  it "should have the correct recipients" do
45
- recipients = @message1.get_recipients
45
+ recipients = @message1.recipients
46
46
  recipients.count.should==2
47
47
  recipients.count(@entity1).should==1
48
48
  recipients.count(@entity2).should==1
@@ -85,7 +85,7 @@ describe "Messages And Receipts" do
85
85
  end
86
86
 
87
87
  it "should have the correct recipients" do
88
- recipients = @message2.get_recipients
88
+ recipients = @message2.recipients
89
89
  recipients.count.should==2
90
90
  recipients.count(@entity1).should==1
91
91
  recipients.count(@entity2).should==1
@@ -131,7 +131,7 @@ describe "Messages And Receipts" do
131
131
  end
132
132
 
133
133
  it "should have the correct recipients" do
134
- recipients = @message2.get_recipients
134
+ recipients = @message2.recipients
135
135
  recipients.count.should==2
136
136
  recipients.count(@entity1).should==1
137
137
  recipients.count(@entity2).should==1
@@ -176,7 +176,7 @@ describe "Messages And Receipts" do
176
176
  end
177
177
 
178
178
  it "should have the correct recipients" do
179
- recipients = @message2.get_recipients
179
+ recipients = @message2.recipients
180
180
  recipients.count.should==2
181
181
  recipients.count(@entity1).should==1
182
182
  recipients.count(@entity2).should==1
@@ -228,7 +228,7 @@ describe "Messages And Receipts" do
228
228
  end
229
229
 
230
230
  it "should have the correct recipients" do
231
- recipients = @message1.get_recipients
231
+ recipients = @message1.recipients
232
232
  recipients.count.should==2
233
233
  recipients.count(@entity1).should==1
234
234
  recipients.count(@entity2).should==1
@@ -271,7 +271,7 @@ describe "Messages And Receipts" do
271
271
  end
272
272
 
273
273
  it "should have the correct recipients" do
274
- recipients = @message2.get_recipients
274
+ recipients = @message2.recipients
275
275
  recipients.count.should==2
276
276
  recipients.count(@entity1).should==1
277
277
  recipients.count(@entity2).should==1
@@ -317,7 +317,7 @@ describe "Messages And Receipts" do
317
317
  end
318
318
 
319
319
  it "should have the correct recipients" do
320
- recipients = @message2.get_recipients
320
+ recipients = @message2.recipients
321
321
  recipients.count.should==2
322
322
  recipients.count(@entity1).should==1
323
323
  recipients.count(@entity2).should==1
@@ -396,7 +396,7 @@ describe "Messages And Receipts" do
396
396
  end
397
397
 
398
398
  it "should have the correct recipients" do
399
- recipients = @message1.get_recipients
399
+ recipients = @message1.recipients
400
400
  recipients.count.should==3
401
401
  recipients.count(@entity1).should==1
402
402
  recipients.count(@entity2).should==1
@@ -445,7 +445,7 @@ describe "Messages And Receipts" do
445
445
  end
446
446
 
447
447
  it "should have the correct recipients" do
448
- recipients = @message2.get_recipients
448
+ recipients = @message2.recipients
449
449
  recipients.count.should==2
450
450
  recipients.count(@entity1).should==1
451
451
  recipients.count(@entity2).should==1
@@ -498,7 +498,7 @@ describe "Messages And Receipts" do
498
498
  end
499
499
 
500
500
  it "should have the correct recipients" do
501
- recipients = @message2.get_recipients
501
+ recipients = @message2.recipients
502
502
  recipients.count.should==3
503
503
  recipients.count(@entity1).should==1
504
504
  recipients.count(@entity2).should==1
@@ -578,7 +578,7 @@ describe "Messages And Receipts" do
578
578
  end
579
579
 
580
580
  it "should have the correct recipients" do
581
- recipients = @message1.get_recipients
581
+ recipients = @message1.recipients
582
582
  recipients.count.should==3
583
583
  recipients.count(@entity1).should==1
584
584
  recipients.count(@entity2).should==1
@@ -627,7 +627,7 @@ describe "Messages And Receipts" do
627
627
  end
628
628
 
629
629
  it "should have the correct recipients" do
630
- recipients = @message2.get_recipients
630
+ recipients = @message2.recipients
631
631
  recipients.count.should==2
632
632
  recipients.count(@entity1).should==1
633
633
  recipients.count(@entity2).should==1
@@ -680,7 +680,7 @@ describe "Messages And Receipts" do
680
680
  end
681
681
 
682
682
  it "should have the correct recipients" do
683
- recipients = @message2.get_recipients
683
+ recipients = @message2.recipients
684
684
  recipients.count.should==3
685
685
  recipients.count(@entity1).should==1
686
686
  recipients.count(@entity2).should==1
@@ -31,9 +31,10 @@ describe Conversation do
31
31
  end
32
32
 
33
33
  it "should have all conversation users" do
34
- @conversation.get_recipients.count.should==2
35
- @conversation.get_recipients.count(@entity1).should==1
36
- @conversation.get_recipients.count(@entity2).should==1
34
+ @conversation.recipients.count.should==2
35
+ @conversation.recipients.count.should==2
36
+ @conversation.recipients.count(@entity1).should==1
37
+ @conversation.recipients.count(@entity2).should==1
37
38
  end
38
39
 
39
40
  end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Message do
4
+
5
+ before do
6
+ @entity1 = Factory(:user)
7
+ @entity2 = Factory(:user)
8
+ @receipt1 = @entity1.send_message(@entity2,"Body","Subject")
9
+ @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1")
10
+ @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2")
11
+ @receipt4 = @entity2.reply_to_all(@receipt3,"Reply body 3")
12
+ @message1 = @receipt1.message
13
+ @message4 = @receipt4.message
14
+ @conversation = @message1.conversation
15
+ end
16
+
17
+ it "should have right recipients" do
18
+ @receipt1.message.recipients.count.should==2
19
+ @receipt2.message.recipients.count.should==2
20
+ @receipt3.message.recipients.count.should==2
21
+ @receipt4.message.recipients.count.should==2
22
+ end
23
+
24
+ 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: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 12
10
- version: 0.0.12
9
+ - 13
10
+ version: 0.0.13
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eduardo Casanova Cuesta
@@ -255,6 +255,7 @@ files:
255
255
  - spec/models/conversation_spec.rb
256
256
  - spec/models/mailbox_spec.rb
257
257
  - spec/models/mailboxer_models_messageable_spec.rb
258
+ - spec/models/message_spec.rb
258
259
  - spec/models/receipt_spec.rb
259
260
  - spec/spec_helper.rb
260
261
  has_rdoc: true