mailboxer 0.0.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.
Files changed (69) hide show
  1. data/.gitignore +12 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/app/models/mailboxer_conversation.rb +60 -0
  8. data/app/models/mailboxer_mail.rb +70 -0
  9. data/app/models/mailboxer_mailbox.rb +68 -0
  10. data/app/models/mailboxer_message.rb +42 -0
  11. data/lib/generators/mailboxer/install_generator.rb +15 -0
  12. data/lib/generators/mailboxer/templates/migration.rb +31 -0
  13. data/lib/mailboxer.rb +8 -0
  14. data/lib/mailboxer/engine.rb +10 -0
  15. data/lib/mailboxer/models/messageable.rb +204 -0
  16. data/mailboxer.gemspec +34 -0
  17. data/spec/dummy/Gemfile +6 -0
  18. data/spec/dummy/Rakefile +7 -0
  19. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  20. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  21. data/spec/dummy/app/models/cylon.rb +3 -0
  22. data/spec/dummy/app/models/duck.rb +3 -0
  23. data/spec/dummy/app/models/user.rb +3 -0
  24. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/spec/dummy/config.ru +4 -0
  26. data/spec/dummy/config/application.rb +45 -0
  27. data/spec/dummy/config/boot.rb +10 -0
  28. data/spec/dummy/config/database.yml +22 -0
  29. data/spec/dummy/config/environment.rb +5 -0
  30. data/spec/dummy/config/environments/development.rb +26 -0
  31. data/spec/dummy/config/environments/production.rb +49 -0
  32. data/spec/dummy/config/environments/test.rb +35 -0
  33. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/spec/dummy/config/initializers/inflections.rb +10 -0
  35. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  36. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  37. data/spec/dummy/config/initializers/session_store.rb +8 -0
  38. data/spec/dummy/config/locales/en.yml +5 -0
  39. data/spec/dummy/config/routes.rb +60 -0
  40. data/spec/dummy/db/migrate/20110228120600_create_users.rb +13 -0
  41. data/spec/dummy/db/migrate/20110303122122_create_mailboxer.rb +31 -0
  42. data/spec/dummy/db/migrate/20110306002940_create_ducks.rb +13 -0
  43. data/spec/dummy/db/migrate/20110306015107_create_cylons.rb +13 -0
  44. data/spec/dummy/db/schema.rb +53 -0
  45. data/spec/dummy/public/404.html +26 -0
  46. data/spec/dummy/public/422.html +26 -0
  47. data/spec/dummy/public/500.html +26 -0
  48. data/spec/dummy/public/favicon.ico +0 -0
  49. data/spec/dummy/public/javascripts/application.js +2 -0
  50. data/spec/dummy/public/javascripts/controls.js +965 -0
  51. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  52. data/spec/dummy/public/javascripts/effects.js +1123 -0
  53. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  54. data/spec/dummy/public/javascripts/rails.js +191 -0
  55. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  56. data/spec/dummy/public/stylesheets/scaffold.css +56 -0
  57. data/spec/dummy/script/rails +6 -0
  58. data/spec/factories/cylon.rb +3 -0
  59. data/spec/factories/duck.rb +3 -0
  60. data/spec/factories/user.rb +3 -0
  61. data/spec/integration/mailboxer_message_and_mail_spec.rb +717 -0
  62. data/spec/integration/navigation_spec.rb +9 -0
  63. data/spec/mailboxer_spec.rb +7 -0
  64. data/spec/models/mailboxer_conversation_spec.rb +39 -0
  65. data/spec/models/mailboxer_mail_spec.rb +33 -0
  66. data/spec/models/mailboxer_mailbox_spec.rb +99 -0
  67. data/spec/models/mailboxer_models_messageable_spec.rb +107 -0
  68. data/spec/spec_helper.rb +36 -0
  69. metadata +278 -0
@@ -0,0 +1,191 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+
176
+ Ajax.Responders.register({
177
+ onCreate: function(request) {
178
+ var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
179
+
180
+ if (csrf_meta_tag) {
181
+ var header = 'X-CSRF-Token',
182
+ token = csrf_meta_tag.readAttribute('content');
183
+
184
+ if (!request.options.requestHeaders) {
185
+ request.options.requestHeaders = {};
186
+ }
187
+ request.options.requestHeaders[header] = token;
188
+ }
189
+ }
190
+ });
191
+ })();
File without changes
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby1.8
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,3 @@
1
+ Factory.define :cylon do |c|
2
+ c.sequence(:name) { |n| "Cylon #{ n }" }
3
+ end
@@ -0,0 +1,3 @@
1
+ Factory.define :duck do |d|
2
+ d.sequence(:name) { |n| "Duck #{ n }" }
3
+ end
@@ -0,0 +1,3 @@
1
+ Factory.define :user do |u|
2
+ u.sequence(:name) { |n| "User #{ n }" }
3
+ end
@@ -0,0 +1,717 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Mailboxer Messages And Mails" do
4
+
5
+ describe "two equal entities" do
6
+ before do
7
+ @entity1 = Factory(:user)
8
+ @entity2 = Factory(:user)
9
+ end
10
+
11
+ describe "message sending" do
12
+
13
+ before do
14
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
15
+ @message1 = @mail1.mailboxer_message
16
+ end
17
+
18
+ it "should create proper message" do
19
+ @message1.sender.id.should == @entity1.id
20
+ @message1.sender.class.should == @entity1.class
21
+ assert @message1.body.eql?"Body"
22
+ assert @message1.subject.eql?"Subject"
23
+ end
24
+
25
+ it "should create proper mails" do
26
+ #Sender Mail
27
+ mail = MailboxerMail.receiver(@entity1).message(@message1).first
28
+ assert mail
29
+ if mail
30
+ mail.read.should==true
31
+ mail.trashed.should==false
32
+ mail.mailbox_type.should=="sentbox"
33
+ end
34
+ #Receiver Mail
35
+ mail = MailboxerMail.receiver(@entity2).message(@message1).first
36
+ assert mail
37
+ if mail
38
+ mail.read.should==false
39
+ mail.trashed.should==false
40
+ mail.mailbox_type.should=="inbox"
41
+ end
42
+ end
43
+
44
+ it "should have the correct recipients" do
45
+ recipients = @message1.get_recipients
46
+ recipients.count.should==2
47
+ recipients.count(@entity1).should==1
48
+ recipients.count(@entity2).should==1
49
+ end
50
+
51
+ end
52
+
53
+ describe "message replying to sender" do
54
+ before do
55
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
56
+ @mail2 = @entity2.reply_to_sender(@mail1,"Reply body")
57
+ @message1 = @mail1.mailboxer_message
58
+ @message2 = @mail2.mailboxer_message
59
+ end
60
+
61
+ it "should create proper message" do
62
+ @message2.sender.id.should == @entity2.id
63
+ @message2.sender.class.should == @entity2.class
64
+ assert @message2.body.eql?"Reply body"
65
+ assert @message2.subject.eql?"RE: Subject"
66
+ end
67
+
68
+ it "should create proper mails" do
69
+ #Sender Mail
70
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
71
+ assert mail
72
+ if mail
73
+ mail.read.should==true
74
+ mail.trashed.should==false
75
+ mail.mailbox_type.should=="sentbox"
76
+ end
77
+ #Receiver Mail
78
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
79
+ assert mail
80
+ if mail
81
+ mail.read.should==false
82
+ mail.trashed.should==false
83
+ mail.mailbox_type.should=="inbox"
84
+ end
85
+ end
86
+
87
+ it "should have the correct recipients" do
88
+ recipients = @message2.get_recipients
89
+ recipients.count.should==2
90
+ recipients.count(@entity1).should==1
91
+ recipients.count(@entity2).should==1
92
+ end
93
+
94
+ it "should be associated to the same conversation" do
95
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
96
+ end
97
+ end
98
+
99
+ describe "message replying to all" do
100
+ before do
101
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
102
+ @mail2 = @entity2.reply_to_all(@mail1,"Reply body")
103
+ @message1 = @mail1.mailboxer_message
104
+ @message2 = @mail2.mailboxer_message
105
+ end
106
+
107
+ it "should create proper message" do
108
+ @message2.sender.id.should == @entity2.id
109
+ @message2.sender.class.should == @entity2.class
110
+ assert @message2.body.eql?"Reply body"
111
+ assert @message2.subject.eql?"RE: Subject"
112
+ end
113
+
114
+ it "should create proper mails" do
115
+ #Sender Mail
116
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
117
+ assert mail
118
+ if mail
119
+ mail.read.should==true
120
+ mail.trashed.should==false
121
+ mail.mailbox_type.should=="sentbox"
122
+ end
123
+ #Receiver Mail
124
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
125
+ assert mail
126
+ if mail
127
+ mail.read.should==false
128
+ mail.trashed.should==false
129
+ mail.mailbox_type.should=="inbox"
130
+ end
131
+ end
132
+
133
+ it "should have the correct recipients" do
134
+ recipients = @message2.get_recipients
135
+ recipients.count.should==2
136
+ recipients.count(@entity1).should==1
137
+ recipients.count(@entity2).should==1
138
+ end
139
+
140
+ it "should be associated to the same conversation" do
141
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
142
+ end
143
+ end
144
+ describe "message replying to conversation" do
145
+ before do
146
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
147
+ @mail2 = @entity2.reply_to_conversation(@mail1.mailboxer_conversation,"Reply body")
148
+ @message1 = @mail1.mailboxer_message
149
+ @message2 = @mail2.mailboxer_message
150
+ end
151
+
152
+ it "should create proper message" do
153
+ @message2.sender.id.should == @entity2.id
154
+ @message2.sender.class.should == @entity2.class
155
+ assert @message2.body.eql?"Reply body"
156
+ assert @message2.subject.eql?"RE: Subject"
157
+ end
158
+
159
+ it "should create proper mails" do
160
+ #Sender Mail
161
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
162
+ assert mail
163
+ if mail
164
+ mail.read.should==true
165
+ mail.trashed.should==false
166
+ mail.mailbox_type.should=="sentbox"
167
+ end
168
+ #Receiver Mail
169
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
170
+ assert mail
171
+ if mail
172
+ mail.read.should==false
173
+ mail.trashed.should==false
174
+ mail.mailbox_type.should=="inbox"
175
+ end
176
+ end
177
+
178
+ it "should have the correct recipients" do
179
+ recipients = @message2.get_recipients
180
+ recipients.count.should==2
181
+ recipients.count(@entity1).should==1
182
+ recipients.count(@entity2).should==1
183
+ end
184
+
185
+ it "should be associated to the same conversation" do
186
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "two different entities" do
192
+ before do
193
+ @entity1 = Factory(:user)
194
+ @entity2 = Factory(:duck)
195
+ end
196
+
197
+ describe "message sending" do
198
+
199
+ before do
200
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
201
+ @message1 = @mail1.mailboxer_message
202
+ end
203
+
204
+ it "should create proper message" do
205
+ @message1.sender.id.should == @entity1.id
206
+ @message1.sender.class.should == @entity1.class
207
+ assert @message1.body.eql?"Body"
208
+ assert @message1.subject.eql?"Subject"
209
+ end
210
+
211
+ it "should create proper mails" do
212
+ #Sender Mail
213
+ mail = MailboxerMail.receiver(@entity1).message(@message1).first
214
+ assert mail
215
+ if mail
216
+ mail.read.should==true
217
+ mail.trashed.should==false
218
+ mail.mailbox_type.should=="sentbox"
219
+ end
220
+ #Receiver Mail
221
+ mail = MailboxerMail.receiver(@entity2).message(@message1).first
222
+ assert mail
223
+ if mail
224
+ mail.read.should==false
225
+ mail.trashed.should==false
226
+ mail.mailbox_type.should=="inbox"
227
+ end
228
+ end
229
+
230
+ it "should have the correct recipients" do
231
+ recipients = @message1.get_recipients
232
+ recipients.count.should==2
233
+ recipients.count(@entity1).should==1
234
+ recipients.count(@entity2).should==1
235
+ end
236
+
237
+ end
238
+
239
+ describe "message replying to sender" do
240
+ before do
241
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
242
+ @mail2 = @entity2.reply_to_sender(@mail1,"Reply body")
243
+ @message1 = @mail1.mailboxer_message
244
+ @message2 = @mail2.mailboxer_message
245
+ end
246
+
247
+ it "should create proper message" do
248
+ @message2.sender.id.should == @entity2.id
249
+ @message2.sender.class.should == @entity2.class
250
+ assert @message2.body.eql?"Reply body"
251
+ assert @message2.subject.eql?"RE: Subject"
252
+ end
253
+
254
+ it "should create proper mails" do
255
+ #Sender Mail
256
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
257
+ assert mail
258
+ if mail
259
+ mail.read.should==true
260
+ mail.trashed.should==false
261
+ mail.mailbox_type.should=="sentbox"
262
+ end
263
+ #Receiver Mail
264
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
265
+ assert mail
266
+ if mail
267
+ mail.read.should==false
268
+ mail.trashed.should==false
269
+ mail.mailbox_type.should=="inbox"
270
+ end
271
+ end
272
+
273
+ it "should have the correct recipients" do
274
+ recipients = @message2.get_recipients
275
+ recipients.count.should==2
276
+ recipients.count(@entity1).should==1
277
+ recipients.count(@entity2).should==1
278
+ end
279
+
280
+ it "should be associated to the same conversation" do
281
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
282
+ end
283
+ end
284
+
285
+ describe "message replying to all" do
286
+ before do
287
+ @mail1 = @entity1.send_message(@entity2,"Body","Subject")
288
+ @mail2 = @entity2.reply_to_all(@mail1,"Reply body")
289
+ @message1 = @mail1.mailboxer_message
290
+ @message2 = @mail2.mailboxer_message
291
+ end
292
+
293
+ it "should create proper message" do
294
+ @message2.sender.id.should == @entity2.id
295
+ @message2.sender.class.should == @entity2.class
296
+ assert @message2.body.eql?"Reply body"
297
+ assert @message2.subject.eql?"RE: Subject"
298
+ end
299
+
300
+ it "should create proper mails" do
301
+ #Sender Mail
302
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
303
+ assert mail
304
+ if mail
305
+ mail.read.should==true
306
+ mail.trashed.should==false
307
+ mail.mailbox_type.should=="sentbox"
308
+ end
309
+ #Receiver Mail
310
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
311
+ assert mail
312
+ if mail
313
+ mail.read.should==false
314
+ mail.trashed.should==false
315
+ mail.mailbox_type.should=="inbox"
316
+ end
317
+ end
318
+
319
+ it "should have the correct recipients" do
320
+ recipients = @message2.get_recipients
321
+ recipients.count.should==2
322
+ recipients.count(@entity1).should==1
323
+ recipients.count(@entity2).should==1
324
+ end
325
+
326
+ it "should be associated to the same conversation" do
327
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
328
+ end
329
+ end
330
+ describe "message replying to conversation (TODO)" do
331
+ before do
332
+ #TODO
333
+ end
334
+
335
+ it "should create proper message" do
336
+ #TODO
337
+ end
338
+
339
+ it "should create proper mails" do
340
+ #TODO
341
+ end
342
+
343
+ it "should have the correct recipients" do
344
+ #TODO
345
+ end
346
+
347
+ it "should be associated to the same conversation" do
348
+ #TODO
349
+ end
350
+ end
351
+ end
352
+
353
+ describe "three equal entities" do
354
+ before do
355
+ @entity1 = Factory(:user)
356
+ @entity2 = Factory(:user)
357
+ @entity3 = Factory(:user)
358
+ @recipients = Array.new
359
+ @recipients << @entity2
360
+ @recipients << @entity3
361
+ end
362
+
363
+ describe "message sending" do
364
+
365
+ before do
366
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
367
+ @message1 = @mail1.mailboxer_message
368
+ end
369
+
370
+ it "should create proper message" do
371
+ @message1.sender.id.should == @entity1.id
372
+ @message1.sender.class.should == @entity1.class
373
+ assert @message1.body.eql?"Body"
374
+ assert @message1.subject.eql?"Subject"
375
+ end
376
+
377
+ it "should create proper mails" do
378
+ #Sender Mail
379
+ mail = MailboxerMail.receiver(@entity1).message(@message1).first
380
+ assert mail
381
+ if mail
382
+ mail.read.should==true
383
+ mail.trashed.should==false
384
+ mail.mailbox_type.should=="sentbox"
385
+ end
386
+ #Receiver Mails
387
+ @recipients.each do |receiver|
388
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message1.id,receiver.id,receiver.class])
389
+ assert mail
390
+ if mail
391
+ mail.read.should==false
392
+ mail.trashed.should==false
393
+ mail.mailbox_type.should=="inbox"
394
+ end
395
+ end
396
+ end
397
+
398
+ it "should have the correct recipients" do
399
+ recipients = @message1.get_recipients
400
+ recipients.count.should==3
401
+ recipients.count(@entity1).should==1
402
+ recipients.count(@entity2).should==1
403
+ recipients.count(@entity3).should==1
404
+ end
405
+
406
+ end
407
+
408
+ describe "message replying to sender" do
409
+ before do
410
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
411
+ @mail2 = @entity2.reply_to_sender(@mail1,"Reply body")
412
+ @message1 = @mail1.mailboxer_message
413
+ @message2 = @mail2.mailboxer_message
414
+ end
415
+
416
+ it "should create proper message" do
417
+ @message2.sender.id.should == @entity2.id
418
+ @message2.sender.class.should == @entity2.class
419
+ assert @message2.body.eql?"Reply body"
420
+ assert @message2.subject.eql?"RE: Subject"
421
+ end
422
+
423
+ it "should create proper mails" do
424
+ #Sender Mail
425
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
426
+ assert mail
427
+ if mail
428
+ mail.read.should==true
429
+ mail.trashed.should==false
430
+ mail.mailbox_type.should=="sentbox"
431
+ end
432
+ #Receiver Mail
433
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
434
+ assert mail
435
+ if mail
436
+ mail.read.should==false
437
+ mail.trashed.should==false
438
+ mail.mailbox_type.should=="inbox"
439
+ end
440
+
441
+ #No Receiver, No Mail
442
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity3.id,@entity3.class])
443
+ assert mail.nil?
444
+
445
+ end
446
+
447
+ it "should have the correct recipients" do
448
+ recipients = @message2.get_recipients
449
+ recipients.count.should==2
450
+ recipients.count(@entity1).should==1
451
+ recipients.count(@entity2).should==1
452
+ recipients.count(@entity3).should==0
453
+ end
454
+
455
+ it "should be associated to the same conversation" do
456
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
457
+ end
458
+ end
459
+
460
+ describe "message replying to all" do
461
+ before do
462
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
463
+ @mail2 = @entity2.reply_to_all(@mail1,"Reply body")
464
+ @message1 = @mail1.mailboxer_message
465
+ @message2 = @mail2.mailboxer_message
466
+ @recipients2 = Array.new
467
+ @recipients2 << @entity1
468
+ @recipients2 << @entity3
469
+
470
+ end
471
+
472
+ it "should create proper message" do
473
+ @message2.sender.id.should == @entity2.id
474
+ @message2.sender.class.should == @entity2.class
475
+ assert @message2.body.eql?"Reply body"
476
+ assert @message2.subject.eql?"RE: Subject"
477
+ end
478
+
479
+ it "should create proper mails" do
480
+ #Sender Mail
481
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
482
+ assert mail
483
+ if mail
484
+ mail.read.should==true
485
+ mail.trashed.should==false
486
+ mail.mailbox_type.should=="sentbox"
487
+ end
488
+ #Receiver Mails
489
+ @recipients2.each do |receiver|
490
+ mail = MailboxerMail.receiver(receiver).message(@message2).first
491
+ assert mail
492
+ if mail
493
+ mail.read.should==false
494
+ mail.trashed.should==false
495
+ mail.mailbox_type.should=="inbox"
496
+ end
497
+ end
498
+ end
499
+
500
+ it "should have the correct recipients" do
501
+ recipients = @message2.get_recipients
502
+ recipients.count.should==3
503
+ recipients.count(@entity1).should==1
504
+ recipients.count(@entity2).should==1
505
+ recipients.count(@entity3).should==1
506
+ end
507
+
508
+ it "should be associated to the same conversation" do
509
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
510
+ end
511
+ end
512
+ describe "message replying to conversation (TODO)" do
513
+ before do
514
+ #TODO
515
+ end
516
+
517
+ it "should create proper message" do
518
+ #TODO
519
+ end
520
+
521
+ it "should create proper mails" do
522
+ #TODO
523
+ end
524
+
525
+ it "should have the correct recipients" do
526
+ #TODO
527
+ end
528
+
529
+ it "should be associated to the same conversation" do
530
+ #TODO
531
+ end
532
+ end
533
+ end
534
+
535
+ describe "three different entities" do
536
+ before do
537
+ @entity1 = Factory(:user)
538
+ @entity2 = Factory(:duck)
539
+ @entity3 = Factory(:cylon)
540
+ @recipients = Array.new
541
+ @recipients << @entity2
542
+ @recipients << @entity3
543
+ end
544
+
545
+ describe "message sending" do
546
+
547
+ before do
548
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
549
+ @message1 = @mail1.mailboxer_message
550
+ end
551
+
552
+ it "should create proper message" do
553
+ @message1.sender.id.should == @entity1.id
554
+ @message1.sender.class.should == @entity1.class
555
+ assert @message1.body.eql?"Body"
556
+ assert @message1.subject.eql?"Subject"
557
+ end
558
+
559
+ it "should create proper mails" do
560
+ #Sender Mail
561
+ mail = MailboxerMail.receiver(@entity1).message(@message1).first
562
+ assert mail
563
+ if mail
564
+ mail.read.should==true
565
+ mail.trashed.should==false
566
+ mail.mailbox_type.should=="sentbox"
567
+ end
568
+ #Receiver Mails
569
+ @recipients.each do |receiver|
570
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message1.id,receiver.id,receiver.class])
571
+ assert mail
572
+ if mail
573
+ mail.read.should==false
574
+ mail.trashed.should==false
575
+ mail.mailbox_type.should=="inbox"
576
+ end
577
+ end
578
+ end
579
+
580
+ it "should have the correct recipients" do
581
+ recipients = @message1.get_recipients
582
+ recipients.count.should==3
583
+ recipients.count(@entity1).should==1
584
+ recipients.count(@entity2).should==1
585
+ recipients.count(@entity3).should==1
586
+ end
587
+
588
+ end
589
+
590
+ describe "message replying to sender" do
591
+ before do
592
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
593
+ @mail2 = @entity2.reply_to_sender(@mail1,"Reply body")
594
+ @message1 = @mail1.mailboxer_message
595
+ @message2 = @mail2.mailboxer_message
596
+ end
597
+
598
+ it "should create proper message" do
599
+ @message2.sender.id.should == @entity2.id
600
+ @message2.sender.class.should == @entity2.class
601
+ assert @message2.body.eql?"Reply body"
602
+ assert @message2.subject.eql?"RE: Subject"
603
+ end
604
+
605
+ it "should create proper mails" do
606
+ #Sender Mail
607
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
608
+ assert mail
609
+ if mail
610
+ mail.read.should==true
611
+ mail.trashed.should==false
612
+ mail.mailbox_type.should=="sentbox"
613
+ end
614
+ #Receiver Mail
615
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity1.id,@entity1.class])
616
+ assert mail
617
+ if mail
618
+ mail.read.should==false
619
+ mail.trashed.should==false
620
+ mail.mailbox_type.should=="inbox"
621
+ end
622
+
623
+ #No Receiver, No Mail
624
+ mail = MailboxerMail.find(:first,:conditions=>["mailboxer_message_id=? AND receiver_id=? AND receiver_type=?",@message2.id,@entity3.id,@entity3.class])
625
+ assert mail.nil?
626
+
627
+ end
628
+
629
+ it "should have the correct recipients" do
630
+ recipients = @message2.get_recipients
631
+ recipients.count.should==2
632
+ recipients.count(@entity1).should==1
633
+ recipients.count(@entity2).should==1
634
+ recipients.count(@entity3).should==0
635
+ end
636
+
637
+ it "should be associated to the same conversation" do
638
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
639
+ end
640
+ end
641
+
642
+ describe "message replying to all" do
643
+ before do
644
+ @mail1 = @entity1.send_message(@recipients,"Body","Subject")
645
+ @mail2 = @entity2.reply_to_all(@mail1,"Reply body")
646
+ @message1 = @mail1.mailboxer_message
647
+ @message2 = @mail2.mailboxer_message
648
+ @recipients2 = Array.new
649
+ @recipients2 << @entity1
650
+ @recipients2 << @entity3
651
+
652
+ end
653
+
654
+ it "should create proper message" do
655
+ @message2.sender.id.should == @entity2.id
656
+ @message2.sender.class.should == @entity2.class
657
+ assert @message2.body.eql?"Reply body"
658
+ assert @message2.subject.eql?"RE: Subject"
659
+ end
660
+
661
+ it "should create proper mails" do
662
+ #Sender Mail
663
+ mail = MailboxerMail.receiver(@entity2).message(@message2).first
664
+ assert mail
665
+ if mail
666
+ mail.read.should==true
667
+ mail.trashed.should==false
668
+ mail.mailbox_type.should=="sentbox"
669
+ end
670
+ #Receiver Mails
671
+ @recipients2.each do |receiver|
672
+ mail = MailboxerMail.receiver(receiver).message(@message2).first
673
+ assert mail
674
+ if mail
675
+ mail.read.should==false
676
+ mail.trashed.should==false
677
+ mail.mailbox_type.should=="inbox"
678
+ end
679
+ end
680
+ end
681
+
682
+ it "should have the correct recipients" do
683
+ recipients = @message2.get_recipients
684
+ recipients.count.should==3
685
+ recipients.count(@entity1).should==1
686
+ recipients.count(@entity2).should==1
687
+ recipients.count(@entity3).should==1
688
+ end
689
+
690
+ it "should be associated to the same conversation" do
691
+ @message1.mailboxer_conversation.id.should==@message2.mailboxer_conversation.id
692
+ end
693
+ end
694
+
695
+ describe "message replying to conversation (TODO)" do
696
+ before do
697
+ #TODO
698
+ end
699
+
700
+ it "should create proper message" do
701
+ #TODO
702
+ end
703
+
704
+ it "should create proper mails" do
705
+ #TODO
706
+ end
707
+
708
+ it "should have the correct recipients" do
709
+ #TODO
710
+ end
711
+
712
+ it "should be associated to the same conversation" do
713
+ #TODO
714
+ end
715
+ end
716
+ end
717
+ end