acts-as-messageable 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ module ActsAsMessageable
2
+ class Rails3
3
+ def initialize(subject)
4
+ @subject = subject
5
+ end
6
+
7
+ def default_scope(order_by)
8
+ @subject.send(:default_scope, order(order_by))
9
+ end
10
+
11
+ def method_missing(name, *args)
12
+ @subject.send(name, *args)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module ActsAsMessageable
2
+ class Rails4
3
+ def initialize(subject)
4
+ @subject = subject
5
+ end
6
+
7
+ def default_scope(order_by)
8
+ @subject.default_scope { order(order_by) }
9
+ end
10
+
11
+ def scoped
12
+ @subject.scope
13
+ end
14
+
15
+ def method_missing(name, *args)
16
+ @subject.send(name, *args)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsMessageable
4
+ module Scopes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ initialize_scopes
9
+ end
10
+
11
+ module ClassMethods
12
+ def initialize_scopes
13
+ scope :are_from, lambda { |*args| where(:sent_messageable_id => args.first, :sent_messageable_type => args.first.class.name) }
14
+ scope :are_to, lambda { |*args| where(:received_messageable_id => args.first, :received_messageable_type => args.first.class.name) }
15
+ scope :search, lambda { |*args| where("body like :search_txt or topic like :search_txt",:search_txt => "%#{args.first}%")}
16
+ scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and
17
+ sent_messageable_id = :sent_id and
18
+ sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or
19
+ (received_messageable_type = :received_type and
20
+ received_messageable_id = :received_id and
21
+ recipient_delete = :r_delete and recipient_permanent_delete = :r_perm_delete)",
22
+ :sent_type => args.first.class.resolve_active_record_ancestor.to_s,
23
+ :sent_id => args.first.id,
24
+ :received_type => args.first.class.resolve_active_record_ancestor.to_s,
25
+ :received_id => args.first.id,
26
+ :r_delete => args.last,
27
+ :s_delete => args.last,
28
+ :r_perm_delete => false,
29
+ :s_perm_delete => false)
30
+ }
31
+ scope :readed, lambda { where(:opened => true) }
32
+ scope :unreaded, lambda { where(:opened => false) }
33
+ scope :deleted, lambda { where(:recipient_delete => true, :sender_delete => true) }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,266 +1,293 @@
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
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 "send messages with bang" do
33
+ it "should raise exception" do
34
+ expect {
35
+ @alice.send_message!(@bob, :body => "body")
36
+ }.to raise_error(ActiveRecord::RecordInvalid)
37
+ end
38
+
39
+ it "should return message object" do
40
+ @alice.send_message!(@bob, :body => "body", :topic => "topic").should
41
+ be_kind_of ActsAsMessageable::Message
42
+ end
43
+ end
44
+
45
+ describe "inheritance models" do
46
+ it "men send message to alice" do
47
+ send_message(@men, @alice)
48
+ @men.sent_messages.size.should be_equal(1)
49
+ @alice.received_messages.size.should be_equal(2)
50
+ end
51
+
52
+ it "messages method should receive all messages connected with user" do
53
+ send_message(@men, @alice)
54
+ @men.messages.size.should be_equal(1)
55
+ end
56
+
57
+ it "men send message and receive from alice" do
58
+ send_message(@men, @alice)
59
+ send_message(@alice, @men)
60
+
61
+ @men.messages.size.should be_equal(2)
62
+ @men.sent_messages.size.should be_equal(1)
63
+ @men.received_messages.size.should be_equal(1)
64
+ end
65
+ end
66
+
67
+ describe "reply to messages" do
68
+
69
+ it "pat should not be able to reply to a message from bob to alice" do
70
+ @reply_message = @pat.reply_to(@message, "Re: Topic", "Body")
71
+ @reply_message.should be_nil
72
+ end
73
+
74
+ it "alice should be able to reply to a message from bob to alice" do
75
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
76
+ @reply_message.should_not be_nil
77
+ @bob.messages.are_from(@alice).count.should == 1
78
+ @alice.sent_messages.are_to(@bob).count.should == 1
79
+ end
80
+
81
+ it "alice should be able to reply to a message using the message object" do
82
+ @reply_message = @message.reply("Re: Topic", "Body")
83
+ @reply_message.should_not be_nil
84
+ @bob.messages.are_from(@alice).count.should == 1
85
+ @alice.sent_messages.are_to(@bob).count.should == 1
86
+ end
87
+
88
+ it "bob try to add something to conversation" do
89
+ @reply_message = @bob.reply_to(@message, "Oh, I Forget", "1+1=2")
90
+ @reply_message.from.should == @message.from
91
+ @reply_message.to.should == @message.to
92
+ end
93
+
94
+ it "bob try to add something to conversation and should receive proper order" do
95
+ @reply_message = @bob.reply_to(@message, "Oh, I Forget", "1+1=2")
96
+ @sec_message = @alice.reply_to(@message, "Yeah, right", "1+1=3!")
97
+
98
+ @message.conversation.should == [@sec_message, @reply_message, @message]
99
+ end
100
+ end
101
+
102
+ describe "delete messages" do
103
+
104
+ it "bob should have one deleted message from alice" do
105
+ @bob.messages.process do |m|
106
+ m.delete
107
+ end
108
+
109
+ @bob.messages.each do |m|
110
+ m.recipient_delete.should == true
111
+ m.sender_delete.should == false
112
+ end
113
+
114
+ @bob.deleted_messages.count.should == 1
115
+ @bob.messages.count.should == 0
116
+ end
117
+
118
+ it "received_messages and sent_messages should work with .process method" do
119
+ @bob.sent_messages.count.should == 1
120
+ @alice.received_messages.count.should == 1
121
+
122
+ @bob.sent_messages.process { |m| m.delete }
123
+ @bob.sent_messages.count.should == 0
124
+ @alice.received_messages.count.should == 1
125
+
126
+ @alice.received_messages.process { |m| m.delete }
127
+ @alice.received_messages.count.should == 0
128
+ end
129
+
130
+ it "message should permanent delete" do
131
+ @alice.messages.process { |m| m.delete }
132
+ @alice.messages.count.should == 0
133
+
134
+ @alice.deleted_messages.count.should == 1
135
+ @alice.deleted_messages.process { |m| m.delete }
136
+ @alice.deleted_messages.count.should == 0
137
+
138
+ @message.reload
139
+ @message.recipient_permanent_delete.should == true
140
+
141
+ @bob.sent_messages.count.should == 1
142
+ end
143
+
144
+ it "pat should not able to delete message" do
145
+ lambda { @pat.delete_message(@message) }.should raise_error
146
+ end
147
+ end
148
+
149
+ describe "restore message" do
150
+ it "alice should restore message" do
151
+ @alice.received_messages.process { |m| m.delete }
152
+ @alice.restore_message(@message.reload)
153
+ @alice.received_messages.count.should == 1
154
+ end
155
+
156
+ it "should works with relation" do
157
+ @alice.received_messages.process { |m| m.delete }
158
+ @alice.received_messages.count.should == 0
159
+ @alice.deleted_messages.process { |m| m.restore }
160
+ @alice.received_messages.count.should == 1
161
+ end
162
+
163
+ it "pat should not able to restore message" do
164
+ lambda { @pat.restore_message(@message) }.should raise_error
165
+ end
166
+ end
167
+
168
+ describe "read/unread feature" do
169
+ it "alice should have one unread message from bob" do
170
+ @alice.messages.are_from(@bob).unreaded.count.should == 1
171
+ @alice.messages.are_from(@bob).readed.count.should == 0
172
+ end
173
+
174
+ it "alice should able to read message from bob" do
175
+ @alice.messages.are_from(@bob).first.read
176
+ @alice.messages.are_from(@bob).unreaded.count.should == 0
177
+ end
178
+
179
+ it "alice should able to unread message from bob" do
180
+ @alice.messages.are_from(@bob).first.read
181
+ @alice.messages.are_from(@bob).first.unread
182
+ @alice.messages.are_from(@bob).unreaded.count.should == 1
183
+ end
184
+
185
+ it "alice should able to get datetime when he read bob message" do
186
+ @alice.messages.are_from(@bob).first.read
187
+ read_datetime = @alice.messages.are_from(@bob).first.updated_at
188
+ @alice.messages.are_from(@bob).reorder("updated_at asc").first.updated_at.should == read_datetime
189
+ end
190
+ end
191
+
192
+ it "finds proper message" do
193
+ @bob.messages.find(@message.id) == @message
194
+ end
195
+
196
+ it "message should have proper topic" do
197
+ @bob.messages.count.should == 1
198
+ @bob.messages.first.topic == "Topic"
199
+ end
200
+
201
+ describe "conversation" do
202
+ it "bob send message to alice, and alice reply to bob message and show proper tree" do
203
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
204
+
205
+ @reply_message.conversation.size.should == 2
206
+ @reply_message.conversation.last.topic.should == "Topic"
207
+ @reply_message.conversation.first.topic.should == "Re: Topic"
208
+ end
209
+
210
+ it "bob send message to alice, alice answer, and bob answer for alice answer" do
211
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
212
+ @reply_reply_message = @bob.reply_to(@reply_message, "Re: Re: Topic", "Body")
213
+
214
+ [@message, @reply_message, @reply_reply_message].each do |m|
215
+ m.conversation.size.should == 3
216
+ end
217
+
218
+ @message.conversation.first.should == @reply_reply_message
219
+ @reply_reply_message.conversation.first.should == @reply_reply_message
220
+ end
221
+ end
222
+
223
+ describe "conversations" do
224
+ before do
225
+ @reply_message = @message.reply("Re: Topic", "Body")
226
+ @reply_reply_message = @reply_message.reply("Re: Re: Topic", "Body")
227
+ end
228
+
229
+ it "bob send message to alice and alice reply" do
230
+ @bob.messages.conversations.should == [@reply_reply_message]
231
+ @reply_message.conversation.should == [@reply_reply_message, @reply_message, @message]
232
+ end
233
+
234
+ it "show conversations in proper order" do
235
+ @sec_message = @bob.send_message(@alice, "Hi", "Alice!")
236
+ @sec_reply = @sec_message.reply("Re: Hi", "Fine!")
237
+ @bob.received_messages.conversations.map(&:id).should == [@sec_reply.id, @reply_reply_message.id]
238
+ @sec_reply.conversation.to_a.should == [@sec_reply, @sec_message]
239
+ end
240
+ end
241
+
242
+ describe "search text from messages" do
243
+ before do
244
+ @reply_message = @message.reply("Re: Topic", "Body : I am fine")
245
+ @reply_reply_message = @reply_message.reply("Re: Re: Topic", "Fine too")
246
+ end
247
+
248
+ it "bob should be able to search text from messages" do
249
+ recordset = @bob.messages.search("I am fine")
250
+ recordset.count.should == 1
251
+ recordset.should_not be_nil
252
+ end
253
+ end
254
+
255
+ describe "send messages with hash" do
256
+ it "send message with hash" do
257
+ @message = @bob.send_message(@alice, {:body => "Body", :topic => "Topic"})
258
+ @message.topic.should == "Topic"
259
+ @message.body.should == "Body"
260
+ end
261
+ end
262
+
263
+ it "messages should return in right order :created_at" do
264
+ @message = send_message(@bob, @alice, "Example", "Example Body")
265
+ @alice.messages.last.body.should == "Body"
266
+ end
267
+
268
+ it "received_messages should return unloaded messages" do
269
+ @alice.received_messages.loaded?.should be_false
270
+ end
271
+
272
+ it "sent_messages should return unloaded messages" do
273
+ @bob.sent_messages.loaded?.should be_false
274
+ end
275
+
276
+ describe "send messages between two different models (the same id)" do
277
+ it "should have the same id" do
278
+ @alice.id.should be_equal(@admin.id)
279
+ end
280
+
281
+ it "bob send message to admin (different model) with the same id" do
282
+ @bob.send_message(@alice, "hello", "alice")
283
+ @alice.messages.are_to(@alice).size.should be_equal(2)
284
+ @alice.messages.are_to(@admin).size.should be_equal(0)
285
+ end
286
+
287
+ it "admin send message to bob" do
288
+ @admin.send_message(@bob, "hello", "bob")
289
+ @bob.messages.are_from(@admin).size.should be_equal(1)
290
+ @bob.messages.are_from(@alice).size.should be_equal(0)
291
+ end
292
+ end
293
+ end