has_messages 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ class CreateMessages < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :messages do |t|
4
+ t.references :sender, :polymorphic => true, :null => false
5
+ t.text :subject
6
+ t.text :body
7
+ t.string :state, :null => false
8
+ t.datetime :hidden_at
9
+ t.string :type
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :messages
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class CreateMessageRecipients < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :message_recipients do |t|
4
+ t.references :message, :null => false
5
+ t.references :receiver, :polymorphic => true, :null => false
6
+ t.string :kind, :null => false
7
+ t.integer :position
8
+ t.string :state, :null => false
9
+ t.datetime :hidden_at
10
+ end
11
+ add_index :message_recipients, [:message_id, :kind, :position], :unique => true
12
+ end
13
+
14
+ def self.down
15
+ drop_table :message_recipients
16
+ end
17
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_messages'
@@ -0,0 +1,75 @@
1
+ require 'state_machine'
2
+
3
+ module PluginAWeek #:nodoc:
4
+ # Adds a generic implementation for sending messages between users
5
+ module HasMessages
6
+ def self.included(base) #:nodoc:
7
+ base.class_eval do
8
+ extend PluginAWeek::HasMessages::MacroMethods
9
+ end
10
+ end
11
+
12
+ module MacroMethods
13
+ # Creates the following message associations:
14
+ # * +messages+ - Messages that were composed and are visible to the owner. Mesages may have been sent or unsent.
15
+ # * +received_messages - Messages that have been received from others and are visible. Messages may have been read or unread.
16
+ #
17
+ # == Creating new messages
18
+ #
19
+ # To create a new message, the +messages+ association should be used, for example:
20
+ #
21
+ # user = User.find(123)
22
+ # message = user.messages.build
23
+ # message.subject = 'Hello'
24
+ # message.body = 'How are you?'
25
+ # message.to User.find(456)
26
+ # message.save!
27
+ # message.deliver!
28
+ #
29
+ # == Drafts
30
+ #
31
+ # You can get the drafts for a particular user by using the +unsent_messages+
32
+ # helper method. This will find all messages in the "unsent" state. For example,
33
+ #
34
+ # user = User.find(123)
35
+ # user.unsent_messages
36
+ #
37
+ # You can also get at the messages that *have* been sent, using the +sent_messages+
38
+ # helper method. For example,
39
+ #
40
+ # user = User.find(123)
41
+ # user.sent_messages
42
+ def has_messages
43
+ has_many :messages,
44
+ :as => :sender,
45
+ :class_name => 'Message',
46
+ :conditions => {:hidden_at => nil},
47
+ :order => 'messages.created_at ASC'
48
+ has_many :received_messages,
49
+ :as => :receiver,
50
+ :class_name => 'MessageRecipient',
51
+ :include => :message,
52
+ :conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'],
53
+ :order => 'messages.created_at ASC'
54
+
55
+ include PluginAWeek::HasMessages::InstanceMethods
56
+ end
57
+ end
58
+
59
+ module InstanceMethods
60
+ # Composed messages that have not yet been sent
61
+ def unsent_messages
62
+ messages.with_state('unsent')
63
+ end
64
+
65
+ # Composed messages that have already been sent
66
+ def sent_messages
67
+ messages.with_states(%w(queued sent))
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ ActiveRecord::Base.class_eval do
74
+ include PluginAWeek::HasMessages
75
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_messages
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'config/boot'
2
+ require "#{File.dirname(__FILE__)}/../../../../plugins_plus/boot"
3
+
4
+ Rails::Initializer.run do |config|
5
+ config.plugin_paths << '..'
6
+ config.plugins = %w(plugins_plus state_machine has_messages)
7
+ config.cache_classes = false
8
+ config.whiny_nils = true
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :login, :null => false
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class MigrateHasMessagesToVersion2 < ActiveRecord::Migration
2
+ def self.up
3
+ Rails::Plugin.find(:has_messages).migrate(2)
4
+ end
5
+
6
+ def self.down
7
+ Rails::Plugin.find(:has_messages).migrate(0)
8
+ end
9
+ end
data/test/factory.rb ADDED
@@ -0,0 +1,47 @@
1
+ module Factory
2
+ # Build actions for the class
3
+ def self.build(klass, &block)
4
+ name = klass.to_s.underscore
5
+ define_method("#{name}_attributes", block)
6
+
7
+ module_eval <<-end_eval
8
+ def valid_#{name}_attributes(attributes = {})
9
+ #{name}_attributes(attributes)
10
+ attributes
11
+ end
12
+
13
+ def new_#{name}(attributes = {})
14
+ #{klass}.new(valid_#{name}_attributes(attributes))
15
+ end
16
+
17
+ def create_#{name}(*args)
18
+ record = new_#{name}(*args)
19
+ record.save!
20
+ record.reload
21
+ record
22
+ end
23
+ end_eval
24
+ end
25
+
26
+ build Message do |attributes|
27
+ attributes[:sender] = create_user unless attributes.include?(:sender)
28
+ attributes.reverse_merge!(
29
+ :subject => 'New features',
30
+ :body => 'Lots of new things to talk about... come to the meeting tonight to find out!'
31
+ )
32
+ end
33
+
34
+ build MessageRecipient do |attributes|
35
+ attributes[:message] = create_message unless attributes.include?(:message)
36
+ attributes[:receiver] = create_user(:login => 'me') unless attributes.include?(:receiver)
37
+ attributes.reverse_merge!(
38
+ :kind => 'to'
39
+ )
40
+ end
41
+
42
+ build User do |attributes|
43
+ attributes.reverse_merge!(
44
+ :login => 'admin'
45
+ )
46
+ end
47
+ end
@@ -0,0 +1,139 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class UserByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @user = create_user
6
+ end
7
+
8
+ def test_should_not_have_any_messages
9
+ assert @user.messages.empty?
10
+ end
11
+
12
+ def test_should_not_have_any_unsent_messages
13
+ assert @user.unsent_messages.empty?
14
+ end
15
+
16
+ def test_should_not_have_any_sent_messages
17
+ assert @user.sent_messages.empty?
18
+ end
19
+
20
+ def test_should_not_have_any_received_messages
21
+ assert @user.received_messages.empty?
22
+ end
23
+ end
24
+
25
+ class UserTest < Test::Unit::TestCase
26
+ def setup
27
+ @user = create_user
28
+ end
29
+
30
+ def test_should_be_able_to_create_new_messages
31
+ message = @user.messages.build
32
+ assert_instance_of Message, message
33
+ assert_equal @user, message.sender
34
+ end
35
+
36
+ def test_should_be_able_to_send_new_messages
37
+ message = @user.messages.build
38
+ message.to create_user(:login => 'John')
39
+ assert message.deliver!
40
+ end
41
+ end
42
+
43
+ class UserWithUnsentMessages < Test::Unit::TestCase
44
+ def setup
45
+ @user = create_user
46
+ @sent_message = create_message(:sender => @user, :to => create_user(:login => 'you'))
47
+ @sent_message.deliver!
48
+ @first_draft = create_message(:sender => @user)
49
+ @second_draft = create_message(:sender => @user)
50
+ end
51
+
52
+ def test_should_have_unsent_messages
53
+ assert_equal [@first_draft, @second_draft], @user.unsent_messages
54
+ end
55
+
56
+ def test_should_include_unsent_messages_in_messages
57
+ assert_equal [@sent_message, @first_draft, @second_draft], @user.messages
58
+ end
59
+ end
60
+
61
+ class UserWithSentMessages < Test::Unit::TestCase
62
+ def setup
63
+ @user = create_user
64
+ @to = create_user(:login => 'you')
65
+ @draft = create_message(:sender => @user)
66
+
67
+ @first_sent_message = create_message(:sender => @user, :to => @to)
68
+ @first_sent_message.deliver!
69
+
70
+ @second_sent_message = create_message(:sender => @user, :to => @to)
71
+ @second_sent_message.deliver!
72
+ end
73
+
74
+ def test_should_have_sent_messages
75
+ assert_equal [@first_sent_message, @second_sent_message], @user.sent_messages
76
+ end
77
+
78
+ def test_should_include_sent_messages_in_messages
79
+ assert_equal [@draft, @first_sent_message, @second_sent_message], @user.messages
80
+ end
81
+ end
82
+
83
+ class UserWithReceivedMessages < Test::Unit::TestCase
84
+ def setup
85
+ @sender = create_user
86
+ @user = create_user(:login => 'me')
87
+
88
+ @unsent_message = create_message(:sender => @sender, :to => @user)
89
+
90
+ @first_sent_message = create_message(:sender => @sender, :to => @user)
91
+ @first_sent_message.deliver!
92
+
93
+ @second_sent_message = create_message(:sender => @sender, :to => @user)
94
+ @second_sent_message.deliver!
95
+ end
96
+
97
+ def test_should_have_received_messages
98
+ assert_equal [@first_sent_message, @second_sent_message], @user.received_messages.map(&:message)
99
+ end
100
+ end
101
+
102
+ class UserWithHiddenMessagesTest < Test::Unit::TestCase
103
+ def setup
104
+ @user = create_user
105
+ @friend = create_user(:login => 'you')
106
+
107
+ hidden_unsent_message = create_message(:sender => @user)
108
+ hidden_unsent_message.hide!
109
+ @unsent_message = create_message(:sender => @user)
110
+
111
+ hidden_sent_message = create_message(:sender => @user, :to => @friend)
112
+ hidden_sent_message.deliver!
113
+ hidden_sent_message.hide!
114
+ @sent_message = create_message(:sender => @user, :to => @friend)
115
+ @sent_message.deliver!
116
+
117
+ hidden_received_message = create_message(:sender => @friend, :to => @user)
118
+ hidden_received_message.deliver!
119
+ hidden_received_message.recipients.first.hide!
120
+ @received_message = create_message(:sender => @friend, :to => @user)
121
+ @received_message.deliver!
122
+ end
123
+
124
+ def test_should_not_include_hidden_messages_in_messages
125
+ assert_equal [@unsent_message, @sent_message], @user.messages
126
+ end
127
+
128
+ def test_should_not_include_hidden_messages_in_unsent_messages
129
+ assert_equal [@unsent_message], @user.unsent_messages
130
+ end
131
+
132
+ def test_should_not_include_hidden_messages_in_sent_messages
133
+ assert_equal [@sent_message], @user.sent_messages
134
+ end
135
+
136
+ def test_should_not_include_hidden_messages_in_received_messages
137
+ assert_equal [@received_message], @user.received_messages.map(&:message)
138
+ end
139
+ end
@@ -0,0 +1,13 @@
1
+ # Load the plugin testing framework
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
3
+ require 'rubygems'
4
+ require 'plugin_test_helper'
5
+
6
+ # Run the migrations
7
+ ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
8
+
9
+ # Mixin the factory helper
10
+ require File.expand_path("#{File.dirname(__FILE__)}/factory")
11
+ class Test::Unit::TestCase #:nodoc:
12
+ include Factory
13
+ end
@@ -0,0 +1,419 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class MessageRecipientByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @recipient = MessageRecipient.new
6
+ end
7
+
8
+ def test_should_not_have_a_message
9
+ assert_nil @recipient.message_id
10
+ end
11
+
12
+ def test_should_not_have_a_receiver_id
13
+ assert_nil @recipient.receiver_id
14
+ end
15
+
16
+ def test_should_not_have_a_receiver_type
17
+ assert @recipient.receiver_type.blank?
18
+ end
19
+
20
+ def test_should_not_have_a_kind
21
+ assert @recipient.kind.blank?
22
+ end
23
+
24
+ def test_should_not_have_a_position
25
+ assert_nil @recipient.position
26
+ end
27
+
28
+ def test_should_be_in_the_unread_state
29
+ assert_equal 'unread', @recipient.state
30
+ end
31
+
32
+ def test_should_not_be_hidden
33
+ assert_nil @recipient.hidden_at
34
+ assert !@recipient.hidden?
35
+ end
36
+ end
37
+
38
+ class MesageRecipientTest < Test::Unit::TestCase
39
+ def test_should_be_valid_with_a_set_of_valid_attributes
40
+ recipient = new_message_recipient
41
+ assert recipient.valid?
42
+ end
43
+
44
+ def test_should_require_a_message
45
+ recipient = new_message_recipient(:message => nil)
46
+ assert !recipient.valid?
47
+ assert_equal 1, Array(recipient.errors.on(:message_id)).size
48
+ end
49
+
50
+ def test_should_require_a_kind
51
+ recipient = new_message_recipient(:kind => nil)
52
+ assert !recipient.valid?
53
+ assert_equal 1, Array(recipient.errors.on(:kind)).size
54
+ end
55
+
56
+ def test_should_require_a_state
57
+ recipient = new_message_recipient(:state => nil)
58
+ assert !recipient.valid?
59
+ assert_equal 1, Array(recipient.errors.on(:state)).size
60
+ end
61
+
62
+ def test_should_require_a_receiver_id
63
+ recipient = new_message_recipient(:receiver => nil)
64
+ assert !recipient.valid?
65
+ assert_equal 1, Array(recipient.errors.on(:receiver_id)).size
66
+ end
67
+
68
+ def test_should_require_a_receiver_type
69
+ recipient = new_message_recipient(:receiver => nil)
70
+ assert !recipient.valid?
71
+ assert_equal 1, Array(recipient.errors.on(:receiver_type)).size
72
+ end
73
+
74
+ def test_should_not_require_a_position
75
+ recipient = new_message_recipient(:position => nil)
76
+ assert recipient.valid?
77
+ end
78
+ end
79
+
80
+ class MessageRecipientAfterBeingCreatedTest < Test::Unit::TestCase
81
+ def setup
82
+ @admin = create_user(:login => 'admin')
83
+ @erich = create_user(:login => 'Erich')
84
+ @richard = create_user(:login => 'Richard')
85
+ @ralph = create_user(:login => 'Ralph')
86
+
87
+ message = create_message(
88
+ :subject => 'Hello',
89
+ :body => 'How are you?',
90
+ :sender => @admin,
91
+ :cc => @richard,
92
+ :bcc => @ralph
93
+ )
94
+ @recipient = create_message_recipient(
95
+ :message => message,
96
+ :receiver => @erich,
97
+ :kind => 'to'
98
+ )
99
+ end
100
+
101
+ def test_should_not_be_hidden
102
+ assert !@recipient.hidden?
103
+ end
104
+
105
+ def test_should_delegate_sender_to_message
106
+ assert_equal @admin, @recipient.sender
107
+ end
108
+
109
+ def test_should_delegate_subject_to_message
110
+ assert_equal 'Hello', @recipient.subject
111
+ end
112
+
113
+ def test_should_delegate_body_to_message
114
+ assert_equal 'How are you?', @recipient.body
115
+ end
116
+
117
+ def test_should_delegate_recipients_to_message
118
+ assert_equal [@erich, @richard, @ralph], @recipient.recipients.map(&:receiver)
119
+ end
120
+
121
+ def test_should_delegate_to_receivers_to_message
122
+ assert_equal [@erich], @recipient.to
123
+ end
124
+
125
+ def test_should_delegate_cc_receivers_to_message
126
+ assert_equal [@richard], @recipient.cc
127
+ end
128
+
129
+ def test_should_delegate_bcc_receivers_to_message
130
+ assert_equal [@ralph], @recipient.bcc
131
+ end
132
+
133
+ def test_should_delegate_created_at_to_message
134
+ assert_not_nil @recipient.created_at
135
+ end
136
+ end
137
+
138
+ class MessageRecipientOfMultipleTest < Test::Unit::TestCase
139
+ def setup
140
+ @erich = create_user(:login => 'Erich')
141
+ @richard = create_user(:login => 'Richard')
142
+ @ralph = create_user(:login => 'Ralph')
143
+
144
+ message = create_message(:to => [@erich, @richard])
145
+ @recipient = create_message_recipient(
146
+ :message => message,
147
+ :receiver => @ralph,
148
+ :kind => 'to'
149
+ )
150
+ end
151
+
152
+ def test_should_use_position_based_on_position_of_last_recipient
153
+ assert_equal 3, @recipient.position
154
+ end
155
+ end
156
+
157
+ class MessageRecipientUnreadWithUnsentMessageTest < Test::Unit::TestCase
158
+ def setup
159
+ @recipient = create_message_recipient
160
+ end
161
+
162
+ def test_should_be_in_the_unread_state
163
+ assert_equal 'unread', @recipient.state
164
+ end
165
+
166
+ def test_should_not_be_able_to_view
167
+ assert !@recipient.view!
168
+ end
169
+ end
170
+
171
+ class MessageRecipientUnreadWithSentMessageTest < Test::Unit::TestCase
172
+ def setup
173
+ @recipient = create_message_recipient
174
+ @recipient.message.deliver!
175
+ end
176
+
177
+ def test_should_be_in_the_unread_state
178
+ assert_equal 'unread', @recipient.state
179
+ end
180
+
181
+ def test_should_be_able_to_view
182
+ assert @recipient.view!
183
+ end
184
+ end
185
+
186
+ class MessageRecipientReadTest < Test::Unit::TestCase
187
+ def setup
188
+ @recipient = create_message_recipient
189
+ @recipient.message.deliver!
190
+ @recipient.view!
191
+ end
192
+
193
+ def test_should_be_in_the_read_state
194
+ assert_equal 'read', @recipient.state
195
+ end
196
+
197
+ def test_should_not_be_able_to_view
198
+ assert !@recipient.view!
199
+ end
200
+ end
201
+
202
+ class MessageRecipientHiddenTest < Test::Unit::TestCase
203
+ def setup
204
+ @recipient = create_message_recipient
205
+ @recipient.hide!
206
+ end
207
+
208
+ def test_should_record_when_it_was_hidden
209
+ assert_not_nil @recipient.hidden_at
210
+ end
211
+
212
+ def test_should_be_hidden
213
+ assert @recipient.hidden?
214
+ end
215
+ end
216
+
217
+ class MessageRecipientUnhiddenTest < Test::Unit::TestCase
218
+ def setup
219
+ @recipient = create_message_recipient
220
+ @recipient.hide!
221
+ @recipient.unhide!
222
+ end
223
+
224
+ def test_should_not_have_the_recorded_value_when_it_was_hidden
225
+ assert_nil @recipient.hidden_at
226
+ end
227
+
228
+ def test_should_not_be_hidden
229
+ assert !@recipient.hidden?
230
+ end
231
+ end
232
+
233
+ class MessageRecipientForwardedTest < Test::Unit::TestCase
234
+ def setup
235
+ @erich = create_user(:login => 'Erich')
236
+ original_message = create_message(
237
+ :subject => 'Hello',
238
+ :body => 'How are you?',
239
+ :cc => create_user(:login => 'Richard'),
240
+ :bcc => create_user(:login => 'Ralph')
241
+ )
242
+ @recipient = create_message_recipient(
243
+ :message => original_message,
244
+ :receiver => @erich
245
+ )
246
+ @message = @recipient.forward
247
+ end
248
+
249
+ def test_should_be_in_unsent_state
250
+ assert_equal 'unsent', @message.state
251
+ end
252
+
253
+ def test_should_not_be_hidden
254
+ assert !@message.hidden?
255
+ end
256
+
257
+ def test_should_have_original_subject
258
+ assert_equal 'Hello', @message.subject
259
+ end
260
+
261
+ def test_should_have_original_body
262
+ assert_equal 'How are you?', @message.body
263
+ end
264
+
265
+ def test_should_use_receiver_as_the_sender
266
+ assert_equal @erich, @message.sender
267
+ end
268
+
269
+ def test_should_not_include_to_recipients
270
+ assert @message.to.empty?
271
+ end
272
+
273
+ def test_should_not_include_cc_recipients
274
+ assert @message.cc.empty?
275
+ end
276
+
277
+ def test_should_not_include_bcc_recipients
278
+ assert @message.bcc.empty?
279
+ end
280
+ end
281
+
282
+ class MessageRecipientRepliedTest < Test::Unit::TestCase
283
+ def setup
284
+ @erich = create_user(:login => 'Erich')
285
+ @john = create_user(:login => 'John')
286
+ original_message = create_message(
287
+ :subject => 'Hello',
288
+ :body => 'How are you?',
289
+ :to => @john,
290
+ :cc => create_user(:login => 'Richard'),
291
+ :bcc => create_user(:login => 'Ralph')
292
+ )
293
+ @recipient = create_message_recipient(
294
+ :message => original_message,
295
+ :receiver => @erich
296
+ )
297
+ @message = @recipient.reply
298
+ end
299
+
300
+ def test_should_be_in_unsent_state
301
+ assert_equal 'unsent', @message.state
302
+ end
303
+
304
+ def test_should_not_be_hidden
305
+ assert !@message.hidden?
306
+ end
307
+
308
+ def test_should_have_original_subject
309
+ assert_equal 'Hello', @message.subject
310
+ end
311
+
312
+ def test_should_have_original_body
313
+ assert_equal 'How are you?', @message.body
314
+ end
315
+
316
+ def test_should_use_receiver_as_the_sender
317
+ assert_equal @erich, @message.sender
318
+ end
319
+
320
+ def test_should_include_to_recipients
321
+ assert [@erich, @john], @message.to
322
+ end
323
+
324
+ def test_should_not_include_cc_recipients
325
+ assert @message.cc.empty?
326
+ end
327
+
328
+ def test_should_not_include_bcc_recipients
329
+ assert @message.bcc.empty?
330
+ end
331
+ end
332
+
333
+ class MessageRecipientRepliedToAllTest < Test::Unit::TestCase
334
+ def setup
335
+ @erich = create_user(:login => 'Erich')
336
+ @john = create_user(:login => 'John')
337
+ @richard = create_user(:login => 'Richard')
338
+ @ralph = create_user(:login => 'Ralph')
339
+ original_message = create_message(
340
+ :subject => 'Hello',
341
+ :body => 'How are you?',
342
+ :to => @john,
343
+ :cc => @richard,
344
+ :bcc => @ralph
345
+ )
346
+ @recipient = create_message_recipient(
347
+ :message => original_message,
348
+ :receiver => @erich
349
+ )
350
+ @message = @recipient.reply_to_all
351
+ end
352
+
353
+ def test_should_be_in_unsent_state
354
+ assert_equal 'unsent', @message.state
355
+ end
356
+
357
+ def test_should_not_be_hidden
358
+ assert !@message.hidden?
359
+ end
360
+
361
+ def test_should_have_original_subject
362
+ assert_equal 'Hello', @message.subject
363
+ end
364
+
365
+ def test_should_have_original_body
366
+ assert_equal 'How are you?', @message.body
367
+ end
368
+
369
+ def test_should_use_receiver_as_the_sender
370
+ assert_equal @erich, @message.sender
371
+ end
372
+
373
+ def test_should_include_to_recipients
374
+ assert [@erich, @john], @message.to
375
+ end
376
+
377
+ def test_should_include_cc_recipients
378
+ assert_equal [@richard], @message.cc
379
+ end
380
+
381
+ def test_should_include_bcc_recipients
382
+ assert_equal [@ralph], @message.bcc
383
+ end
384
+ end
385
+
386
+ class MessageRecipientAfterBeingDestroyedTest < Test::Unit::TestCase
387
+ def setup
388
+ message = create_message
389
+ @first_recipient = create_message_recipient(
390
+ :message => message,
391
+ :receiver => create_user(:login => 'Erich'),
392
+ :kind => 'to'
393
+ )
394
+ @last_recipient = create_message_recipient(
395
+ :message => message,
396
+ :receiver => create_user(:login => 'Richard'),
397
+ :kind => 'to'
398
+ )
399
+
400
+ assert @first_recipient.destroy
401
+ @last_recipient.reload
402
+ end
403
+
404
+ def test_should_reorder_positions_of_all_other_recipients
405
+ assert_equal 1, @last_recipient.position
406
+ end
407
+ end
408
+
409
+ class MessageRecipientAsAClassTest < Test::Unit::TestCase
410
+ def setup
411
+ message = create_message
412
+ @hidden_recipient = create_message_recipient(:message => message, :receiver => create_user(:login => 'erich'), :hidden_at => Time.now)
413
+ @visible_recipient = create_message_recipient(:message => message, :receiver => create_user(:login => 'richard'))
414
+ end
415
+
416
+ def test_should_include_only_visible_messages_in_visible_scope
417
+ assert_equal [@visible_recipient], MessageRecipient.visible
418
+ end
419
+ end