acts-as-messageable 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,5 +9,9 @@ module ActsAsMessageable
9
9
  context.restore_message(message) if message.restored
10
10
  end
11
11
  end
12
+
13
+ def conversations
14
+ map { |r| r.root.subtree.order("id desc").first }.uniq
15
+ end
12
16
  end
13
17
  end
@@ -1,234 +1,266 @@
1
- require 'spec_helper'
2
-
3
- describe "ActsAsMessageable" do
4
- before do
5
- User.acts_as_messageable
6
- @message = send_message
7
- end
8
-
9
- describe "send messages" do
10
- it "alice should have one message" do
11
- @alice.messages.count.should == 1
12
- end
13
-
14
- it "alice should have one message from bob" do
15
- @alice.messages.are_from(@bob).count.should == 1
16
- end
17
-
18
- it "bob should have one message" do
19
- @bob.messages.count.should == 1
20
- end
21
-
22
- it "bob should have one message to alice in outbox" do
23
- @bob.sent_messages.are_to(@alice).count.should == 1
24
- end
25
-
26
- it "bob should have one open message from alice" do
27
- @alice.messages.are_from(@bob).process { |m| m.open }
28
- @alice.messages.readed.count.should == 1
29
- end
30
- end
31
-
32
- describe "inheritance models" do
33
- it "men send message to alice" do
34
- send_message(@men, @alice)
35
- @men.sent_messages.size.should be_equal(1)
36
- @alice.received_messages.size.should be_equal(2)
37
- end
38
-
39
- it "messages method should receive all messages connected with user" do
40
- send_message(@men, @alice)
41
- @men.messages.size.should be_equal(1)
42
- end
43
-
44
- it "men send message and receive from alice" do
45
- send_message(@men, @alice)
46
- send_message(@alice, @men)
47
-
48
- @men.messages.size.should be_equal(2)
49
- @men.sent_messages.size.should be_equal(1)
50
- @men.received_messages.size.should be_equal(1)
51
- end
52
- end
53
-
54
- describe "reply to messages" do
55
- it "pat should not be able to reply to a message from bob to alice" do
56
- @reply_message = @pat.reply_to(@message, "Re: Topic", "Body")
57
- @reply_message.should be_nil
58
- end
59
-
60
- it "alice should be able to reply to a message from bob to alice" do
61
- @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
62
- @reply_message.should_not be_nil
63
- @bob.messages.are_from(@alice).count.should == 1
64
- @alice.sent_messages.are_to(@bob).count.should == 1
65
- end
66
-
67
- it "alice should be able to reply to a message using the message object" do
68
- @reply_message = @message.reply("Re: Topic", "Body")
69
- @reply_message.should_not be_nil
70
- @bob.messages.are_from(@alice).count.should == 1
71
- @alice.sent_messages.are_to(@bob).count.should == 1
72
- end
73
- end
74
-
75
- describe "delete messages" do
76
-
77
- it "bob should have one deleted message from alice" do
78
- @bob.messages.process do |m|
79
- m.delete
80
- end
81
-
82
- @bob.messages.each do |m|
83
- m.recipient_delete.should == true
84
- m.sender_delete.should == false
85
- end
86
-
87
- @bob.deleted_messages.count.should == 1
88
- @bob.messages.count.should == 0
89
- end
90
-
91
- it "received_messages and sent_messages should work with .process method" do
92
- @bob.sent_messages.count.should == 1
93
- @alice.received_messages.count.should == 1
94
-
95
- @bob.sent_messages.process { |m| m.delete }
96
- @bob.sent_messages.count.should == 0
97
- @alice.received_messages.count.should == 1
98
-
99
- @alice.received_messages.process { |m| m.delete }
100
- @alice.received_messages.count.should == 0
101
- end
102
-
103
- it "message should permanent delete" do
104
- @alice.messages.process { |m| m.delete }
105
- @alice.messages.count.should == 0
106
-
107
- @alice.deleted_messages.count.should == 1
108
- @alice.deleted_messages.process { |m| m.delete }
109
- @alice.deleted_messages.count.should == 0
110
-
111
- @message.reload
112
- @message.recipient_permanent_delete.should == true
113
-
114
- @bob.sent_messages.count.should == 1
115
- end
116
-
117
- it "pat should not able to delete message" do
118
- lambda { @pat.delete_message(@message) }.should raise_error
119
- end
120
- end
121
-
122
- describe "restore message" do
123
- it "alice should restore message" do
124
- @alice.received_messages.process { |m| m.delete }
125
- @alice.restore_message(@message.reload)
126
- @alice.received_messages.count.should == 1
127
- end
128
-
129
- it "should works with relation" do
130
- @alice.received_messages.process { |m| m.delete }
131
- @alice.received_messages.count.should == 0
132
- @alice.deleted_messages.process { |m| m.restore }
133
- @alice.received_messages.count.should == 1
134
- end
135
-
136
- it "pat should not able to restore message" do
137
- lambda { @pat.restore_message(@message) }.should raise_error
138
- end
139
- end
140
-
141
- describe "read/unread feature" do
142
- it "alice should have one unread message from bob" do
143
- @alice.messages.are_from(@bob).unreaded.count.should == 1
144
- @alice.messages.are_from(@bob).readed.count.should == 0
145
- end
146
-
147
- it "alice should able to read message from bob" do
148
- @alice.messages.are_from(@bob).first.read
149
- @alice.messages.are_from(@bob).unreaded.count.should == 0
150
- end
151
-
152
- it "alice should able to unread message from bob" do
153
- @alice.messages.are_from(@bob).first.read
154
- @alice.messages.are_from(@bob).first.unread
155
- @alice.messages.are_from(@bob).unreaded.count.should == 1
156
- end
157
- end
158
-
159
- it "should be in database message with id ..." do
160
- message_id = send_message.id
161
- @bob.messages.with_id(message_id).count.should == 1
162
- end
163
-
164
- it "finds proper message" do
165
- @bob.messages.find(@message.id) == @message
166
- end
167
-
168
- it "message should have proper topic" do
169
- @bob.messages.count.should == 1
170
- @bob.messages.first.topic == "Topic"
171
- end
172
-
173
- describe "conversation" do
174
- it "bob send message to alice, and alice reply to bob message and show proper tree" do
175
- @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
176
-
177
- @reply_message.conversation.size.should == 2
178
- @reply_message.conversation.last.topic.should == "Topic"
179
- @reply_message.conversation.first.topic.should == "Re: Topic"
180
- end
181
-
182
- it "bob send message to alice, alice answer, and bob answer for alice answer" do
183
- @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
184
- @reply_reply_message = @bob.reply_to(@reply_message, "Re: Re: Topic", "Body")
185
-
186
- [@message, @reply_message, @reply_reply_message].each do |m|
187
- m.conversation.size.should == 3
188
- end
189
-
190
- @message.conversation.first.should == @reply_reply_message
191
- @reply_reply_message.conversation.first.should == @reply_reply_message
192
- end
193
- end
194
-
195
- describe "send messages with hash" do
196
- it "send message with hash" do
197
- @message = @bob.send_message(@alice, {:body => "Body", :topic => "Topic"})
198
- @message.topic.should == "Topic"
199
- @message.body.should == "Body"
200
- end
201
- end
202
-
203
- it "messages should return in right order :created_at" do
204
- @message = send_message(@bob, @alice, "Example", "Example Body")
205
- @alice.messages.last.body.should == "Body"
206
- end
207
-
208
- it "received_messages should return ActiveRecord::Relation" do
209
- @alice.received_messages.class.should == ActiveRecord::Relation
210
- end
211
-
212
- it "sent_messages should return ActiveRecord::Relation" do
213
- @bob.sent_messages.class.should == ActiveRecord::Relation
214
- end
215
-
216
- describe "send messages between two different models (the same id)" do
217
- it "should have the same id" do
218
- @alice.id.should be_equal(@admin.id)
219
- end
220
-
221
- it "bob send message to admin (different model) with the same id" do
222
- @bob.send_message(@alice, "hello", "alice")
223
- @alice.messages.are_to(@alice).size.should be_equal(2)
224
- @alice.messages.are_to(@admin).size.should be_equal(0)
225
- end
226
-
227
- it "admin send message to bob" do
228
- @admin.send_message(@bob, "hello", "bob")
229
- @bob.messages.are_from(@admin).size.should be_equal(1)
230
- @bob.messages.are_from(@alice).size.should be_equal(0)
231
- end
232
- end
233
-
234
- end
1
+ require 'spec_helper'
2
+
3
+ describe "ActsAsMessageable" do
4
+ before do
5
+ User.acts_as_messageable
6
+ @message = send_message
7
+ end
8
+
9
+ describe "send messages" do
10
+ it "alice should have one message" do
11
+ @alice.messages.count.should == 1
12
+ end
13
+
14
+ it "alice should have one message from bob" do
15
+ @alice.messages.are_from(@bob).count.should == 1
16
+ end
17
+
18
+ it "bob should have one message" do
19
+ @bob.messages.count.should == 1
20
+ end
21
+
22
+ it "bob should have one message to alice in outbox" do
23
+ @bob.sent_messages.are_to(@alice).count.should == 1
24
+ end
25
+
26
+ it "bob should have one open message from alice" do
27
+ @alice.messages.are_from(@bob).process { |m| m.open }
28
+ @alice.messages.readed.count.should == 1
29
+ end
30
+ end
31
+
32
+ describe "inheritance models" do
33
+ it "men send message to alice" do
34
+ send_message(@men, @alice)
35
+ @men.sent_messages.size.should be_equal(1)
36
+ @alice.received_messages.size.should be_equal(2)
37
+ end
38
+
39
+ it "messages method should receive all messages connected with user" do
40
+ send_message(@men, @alice)
41
+ @men.messages.size.should be_equal(1)
42
+ end
43
+
44
+ it "men send message and receive from alice" do
45
+ send_message(@men, @alice)
46
+ send_message(@alice, @men)
47
+
48
+ @men.messages.size.should be_equal(2)
49
+ @men.sent_messages.size.should be_equal(1)
50
+ @men.received_messages.size.should be_equal(1)
51
+ end
52
+ end
53
+
54
+ describe "reply to messages" do
55
+ it "pat should not be able to reply to a message from bob to alice" do
56
+ @reply_message = @pat.reply_to(@message, "Re: Topic", "Body")
57
+ @reply_message.should be_nil
58
+ end
59
+
60
+ it "alice should be able to reply to a message from bob to alice" do
61
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
62
+ @reply_message.should_not be_nil
63
+ @bob.messages.are_from(@alice).count.should == 1
64
+ @alice.sent_messages.are_to(@bob).count.should == 1
65
+ end
66
+
67
+ it "alice should be able to reply to a message using the message object" do
68
+ @reply_message = @message.reply("Re: Topic", "Body")
69
+ @reply_message.should_not be_nil
70
+ @bob.messages.are_from(@alice).count.should == 1
71
+ @alice.sent_messages.are_to(@bob).count.should == 1
72
+ end
73
+
74
+ it "bob try to add something to conversation" do
75
+ @reply_message = @bob.reply_to(@message, "Oh, I Forget", "1+1=2")
76
+ @reply_message.from.should == @message.from
77
+ @reply_message.to.should == @message.to
78
+ end
79
+
80
+ it "bob try to add something to conversation and should receive proper order" do
81
+ @reply_message = @bob.reply_to(@message, "Oh, I Forget", "1+1=2")
82
+ @sec_message = @alice.reply_to(@message, "Yeah, right", "1+1=3!")
83
+
84
+ @message.conversation.should == [@sec_message, @reply_message, @message]
85
+ end
86
+ end
87
+
88
+ describe "delete messages" do
89
+
90
+ it "bob should have one deleted message from alice" do
91
+ @bob.messages.process do |m|
92
+ m.delete
93
+ end
94
+
95
+ @bob.messages.each do |m|
96
+ m.recipient_delete.should == true
97
+ m.sender_delete.should == false
98
+ end
99
+
100
+ @bob.deleted_messages.count.should == 1
101
+ @bob.messages.count.should == 0
102
+ end
103
+
104
+ it "received_messages and sent_messages should work with .process method" do
105
+ @bob.sent_messages.count.should == 1
106
+ @alice.received_messages.count.should == 1
107
+
108
+ @bob.sent_messages.process { |m| m.delete }
109
+ @bob.sent_messages.count.should == 0
110
+ @alice.received_messages.count.should == 1
111
+
112
+ @alice.received_messages.process { |m| m.delete }
113
+ @alice.received_messages.count.should == 0
114
+ end
115
+
116
+ it "message should permanent delete" do
117
+ @alice.messages.process { |m| m.delete }
118
+ @alice.messages.count.should == 0
119
+
120
+ @alice.deleted_messages.count.should == 1
121
+ @alice.deleted_messages.process { |m| m.delete }
122
+ @alice.deleted_messages.count.should == 0
123
+
124
+ @message.reload
125
+ @message.recipient_permanent_delete.should == true
126
+
127
+ @bob.sent_messages.count.should == 1
128
+ end
129
+
130
+ it "pat should not able to delete message" do
131
+ lambda { @pat.delete_message(@message) }.should raise_error
132
+ end
133
+ end
134
+
135
+ describe "restore message" do
136
+ it "alice should restore message" do
137
+ @alice.received_messages.process { |m| m.delete }
138
+ @alice.restore_message(@message.reload)
139
+ @alice.received_messages.count.should == 1
140
+ end
141
+
142
+ it "should works with relation" do
143
+ @alice.received_messages.process { |m| m.delete }
144
+ @alice.received_messages.count.should == 0
145
+ @alice.deleted_messages.process { |m| m.restore }
146
+ @alice.received_messages.count.should == 1
147
+ end
148
+
149
+ it "pat should not able to restore message" do
150
+ lambda { @pat.restore_message(@message) }.should raise_error
151
+ end
152
+ end
153
+
154
+ describe "read/unread feature" do
155
+ it "alice should have one unread message from bob" do
156
+ @alice.messages.are_from(@bob).unreaded.count.should == 1
157
+ @alice.messages.are_from(@bob).readed.count.should == 0
158
+ end
159
+
160
+ it "alice should able to read message from bob" do
161
+ @alice.messages.are_from(@bob).first.read
162
+ @alice.messages.are_from(@bob).unreaded.count.should == 0
163
+ end
164
+
165
+ it "alice should able to unread message from bob" do
166
+ @alice.messages.are_from(@bob).first.read
167
+ @alice.messages.are_from(@bob).first.unread
168
+ @alice.messages.are_from(@bob).unreaded.count.should == 1
169
+ end
170
+ end
171
+
172
+ it "should be in database message with id ..." do
173
+ message_id = send_message.id
174
+ @bob.messages.with_id(message_id).count.should == 1
175
+ end
176
+
177
+ it "finds proper message" do
178
+ @bob.messages.find(@message.id) == @message
179
+ end
180
+
181
+ it "message should have proper topic" do
182
+ @bob.messages.count.should == 1
183
+ @bob.messages.first.topic == "Topic"
184
+ end
185
+
186
+ describe "conversation" do
187
+ it "bob send message to alice, and alice reply to bob message and show proper tree" do
188
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
189
+
190
+ @reply_message.conversation.size.should == 2
191
+ @reply_message.conversation.last.topic.should == "Topic"
192
+ @reply_message.conversation.first.topic.should == "Re: Topic"
193
+ end
194
+
195
+ it "bob send message to alice, alice answer, and bob answer for alice answer" do
196
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
197
+ @reply_reply_message = @bob.reply_to(@reply_message, "Re: Re: Topic", "Body")
198
+
199
+ [@message, @reply_message, @reply_reply_message].each do |m|
200
+ m.conversation.size.should == 3
201
+ end
202
+
203
+ @message.conversation.first.should == @reply_reply_message
204
+ @reply_reply_message.conversation.first.should == @reply_reply_message
205
+ end
206
+ end
207
+
208
+ describe "conversations" do
209
+ before do
210
+ @reply_message = @message.reply("Re: Topic", "Body")
211
+ @reply_reply_message = @reply_message.reply("Re: Re: Topic", "Body")
212
+ end
213
+
214
+ it "bob send message to alice and alice reply" do
215
+ @bob.messages.conversations.should == [@reply_reply_message]
216
+ @reply_message.conversation.should == [@reply_reply_message, @reply_message, @message]
217
+ end
218
+
219
+ it "show conversations in proper order" do
220
+ @sec_message = @bob.send_message(@alice, "Hi", "Alice!")
221
+ @sec_reply = @sec_message.reply("Re: Hi", "Fine!")
222
+ @bob.received_messages.conversations.should == [@sec_reply, @reply_reply_message]
223
+ @sec_reply.conversation.should == [@sec_reply, @sec_message]
224
+ end
225
+ end
226
+
227
+ describe "send messages with hash" do
228
+ it "send message with hash" do
229
+ @message = @bob.send_message(@alice, {:body => "Body", :topic => "Topic"})
230
+ @message.topic.should == "Topic"
231
+ @message.body.should == "Body"
232
+ end
233
+ end
234
+
235
+ it "messages should return in right order :created_at" do
236
+ @message = send_message(@bob, @alice, "Example", "Example Body")
237
+ @alice.messages.last.body.should == "Body"
238
+ end
239
+
240
+ it "received_messages should return ActiveRecord::Relation" do
241
+ @alice.received_messages.class.should == ActiveRecord::Relation
242
+ end
243
+
244
+ it "sent_messages should return ActiveRecord::Relation" do
245
+ @bob.sent_messages.class.should == ActiveRecord::Relation
246
+ end
247
+
248
+ describe "send messages between two different models (the same id)" do
249
+ it "should have the same id" do
250
+ @alice.id.should be_equal(@admin.id)
251
+ end
252
+
253
+ it "bob send message to admin (different model) with the same id" do
254
+ @bob.send_message(@alice, "hello", "alice")
255
+ @alice.messages.are_to(@alice).size.should be_equal(2)
256
+ @alice.messages.are_to(@admin).size.should be_equal(0)
257
+ end
258
+
259
+ it "admin send message to bob" do
260
+ @admin.send_message(@bob, "hello", "bob")
261
+ @bob.messages.are_from(@admin).size.should be_equal(1)
262
+ @bob.messages.are_from(@alice).size.should be_equal(0)
263
+ end
264
+ end
265
+
266
+ end