has_messages 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -0
- data/README.rdoc +1 -1
- data/Rakefile +1 -1
- data/app/models/message.rb +6 -6
- data/app/models/message_recipient.rb +5 -5
- data/lib/has_messages.rb +59 -61
- data/test/functional/has_messages_test.rb +3 -3
- data/test/unit/message_recipient_test.rb +8 -8
- data/test/unit/message_test.rb +6 -6
- metadata +16 -16
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
== master
|
2
2
|
|
3
|
+
== 0.3.0 / 2008-12-14
|
4
|
+
|
5
|
+
* Remove the PluginAWeek namespace
|
6
|
+
* Rename Message#hide!/unhide! to Message#hide/unhide and MessageRecipient#hide!/unhide! to MessageRecipient#hide/unhide
|
7
|
+
|
3
8
|
== 0.2.0 / 2008-10-26
|
4
9
|
|
5
10
|
* Add mass-assignment protection in the Message/MessageRecipient models
|
data/README.rdoc
CHANGED
@@ -46,7 +46,7 @@ This will build the following associations:
|
|
46
46
|
* +received_messages+
|
47
47
|
|
48
48
|
If you have more specified needs, you can create the same associations manually
|
49
|
-
that +has_messages+ builds. See
|
49
|
+
that +has_messages+ builds. See HasMessages::MacroMethods#has_messages
|
50
50
|
for more information about the asssociations that are generated from this macro.
|
51
51
|
|
52
52
|
=== Creating new messages
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'has_messages'
|
8
|
-
s.version = '0.
|
8
|
+
s.version = '0.3.0'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Demonstrates a reference implementation for sending messages between users.'
|
11
11
|
|
data/app/models/message.rb
CHANGED
@@ -11,16 +11,16 @@
|
|
11
11
|
#
|
12
12
|
# In order to perform actions on the message, such as queueing or delivering,
|
13
13
|
# you should always use the associated event action:
|
14
|
-
# * +queue
|
15
|
-
# * +deliver
|
14
|
+
# * +queue+ - Queues the message so that you can send it in a separate process
|
15
|
+
# * +deliver+ - Sends the message to all of the recipients
|
16
16
|
#
|
17
17
|
# == Hiding messages
|
18
18
|
#
|
19
19
|
# Although you can delete a message, it will also delete it from the inbox of all
|
20
20
|
# the message's recipients. Instead, you can hide messages from users with the
|
21
21
|
# following actions:
|
22
|
-
# * +hide
|
23
|
-
# * +unhide
|
22
|
+
# * +hide+ -Hides the message from the sender's inbox
|
23
|
+
# * +unhide+ - Makes the message visible again
|
24
24
|
class Message < ActiveRecord::Base
|
25
25
|
belongs_to :sender,
|
26
26
|
:polymorphic => true
|
@@ -102,12 +102,12 @@ class Message < ActiveRecord::Base
|
|
102
102
|
end
|
103
103
|
|
104
104
|
# Hides the message from the sender's inbox
|
105
|
-
def hide
|
105
|
+
def hide
|
106
106
|
update_attribute(:hidden_at, Time.now)
|
107
107
|
end
|
108
108
|
|
109
109
|
# Makes the message visible in the sender's inbox
|
110
|
-
def unhide
|
110
|
+
def unhide
|
111
111
|
update_attribute(:hidden_at, nil)
|
112
112
|
end
|
113
113
|
|
@@ -11,15 +11,15 @@
|
|
11
11
|
#
|
12
12
|
# In order to perform actions on the message, such as viewing, you should always
|
13
13
|
# use the associated event action:
|
14
|
-
# * +view
|
14
|
+
# * +view+ - Marks the message as read by the recipient
|
15
15
|
#
|
16
16
|
# == Hiding messages
|
17
17
|
#
|
18
18
|
# Although you can delete a recipient, it will also delete it from everyone else's
|
19
19
|
# message, meaning that no one will know that person was ever a recipient of the
|
20
20
|
# message. Instead, you can hide messages from users with the following actions:
|
21
|
-
# * +hide
|
22
|
-
# * +unhide
|
21
|
+
# * +hide+ -Hides the message from the recipient's inbox
|
22
|
+
# * +unhide+ - Makes the message visible again
|
23
23
|
class MessageRecipient < ActiveRecord::Base
|
24
24
|
belongs_to :message
|
25
25
|
belongs_to :receiver,
|
@@ -88,12 +88,12 @@ class MessageRecipient < ActiveRecord::Base
|
|
88
88
|
end
|
89
89
|
|
90
90
|
# Hides the message from the recipient's inbox
|
91
|
-
def hide
|
91
|
+
def hide
|
92
92
|
update_attribute(:hidden_at, Time.now)
|
93
93
|
end
|
94
94
|
|
95
95
|
# Makes the message visible in the recipient's inbox
|
96
|
-
def unhide
|
96
|
+
def unhide
|
97
97
|
update_attribute(:hidden_at, nil)
|
98
98
|
end
|
99
99
|
|
data/lib/has_messages.rb
CHANGED
@@ -1,71 +1,69 @@
|
|
1
1
|
require 'state_machine'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
3
|
+
# Adds a generic implementation for sending messages between users
|
4
|
+
module HasMessages
|
5
|
+
module MacroMethods
|
6
|
+
# Creates the following message associations:
|
7
|
+
# * +messages+ - Messages that were composed and are visible to the owner. Mesages may have been sent or unsent.
|
8
|
+
# * +received_messages - Messages that have been received from others and are visible. Messages may have been read or unread.
|
9
|
+
#
|
10
|
+
# == Creating new messages
|
11
|
+
#
|
12
|
+
# To create a new message, the +messages+ association should be used, for example:
|
13
|
+
#
|
14
|
+
# user = User.find(123)
|
15
|
+
# message = user.messages.build
|
16
|
+
# message.subject = 'Hello'
|
17
|
+
# message.body = 'How are you?'
|
18
|
+
# message.to User.find(456)
|
19
|
+
# message.save
|
20
|
+
# message.deliver
|
21
|
+
#
|
22
|
+
# == Drafts
|
23
|
+
#
|
24
|
+
# You can get the drafts for a particular user by using the +unsent_messages+
|
25
|
+
# helper method. This will find all messages in the "unsent" state. For example,
|
26
|
+
#
|
27
|
+
# user = User.find(123)
|
28
|
+
# user.unsent_messages
|
29
|
+
#
|
30
|
+
# You can also get at the messages that *have* been sent, using the +sent_messages+
|
31
|
+
# helper method. For example,
|
32
|
+
#
|
33
|
+
# user = User.find(123)
|
34
|
+
# user.sent_messages
|
35
|
+
def has_messages
|
36
|
+
has_many :messages,
|
37
|
+
:as => :sender,
|
38
|
+
:class_name => 'Message',
|
39
|
+
:conditions => {:hidden_at => nil},
|
40
|
+
:order => 'messages.created_at ASC'
|
41
|
+
has_many :received_messages,
|
42
|
+
:as => :receiver,
|
43
|
+
:class_name => 'MessageRecipient',
|
44
|
+
:include => :message,
|
45
|
+
:conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'],
|
46
|
+
:order => 'messages.created_at ASC'
|
47
|
+
|
48
|
+
include HasMessages::InstanceMethods
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module InstanceMethods
|
53
|
+
# Composed messages that have not yet been sent. These consists of all
|
54
|
+
# messages that are currently in the "unsent" state.
|
55
|
+
def unsent_messages
|
56
|
+
messages.with_state('unsent')
|
51
57
|
end
|
52
58
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
messages.with_state('unsent')
|
58
|
-
end
|
59
|
-
|
60
|
-
# Composed messages that have already been sent. These consists of all
|
61
|
-
# messages that are currently in the "queued" or "sent" states.
|
62
|
-
def sent_messages
|
63
|
-
messages.with_states(%w(queued sent))
|
64
|
-
end
|
59
|
+
# Composed messages that have already been sent. These consists of all
|
60
|
+
# messages that are currently in the "queued" or "sent" states.
|
61
|
+
def sent_messages
|
62
|
+
messages.with_states(%w(queued sent))
|
65
63
|
end
|
66
64
|
end
|
67
65
|
end
|
68
66
|
|
69
67
|
ActiveRecord::Base.class_eval do
|
70
|
-
extend
|
68
|
+
extend HasMessages::MacroMethods
|
71
69
|
end
|
@@ -105,18 +105,18 @@ class UserWithHiddenMessagesTest < Test::Unit::TestCase
|
|
105
105
|
@friend = create_user(:login => 'you')
|
106
106
|
|
107
107
|
hidden_unsent_message = create_message(:sender => @user)
|
108
|
-
hidden_unsent_message.hide
|
108
|
+
hidden_unsent_message.hide
|
109
109
|
@unsent_message = create_message(:sender => @user)
|
110
110
|
|
111
111
|
hidden_sent_message = create_message(:sender => @user, :to => @friend)
|
112
112
|
hidden_sent_message.deliver
|
113
|
-
hidden_sent_message.hide
|
113
|
+
hidden_sent_message.hide
|
114
114
|
@sent_message = create_message(:sender => @user, :to => @friend)
|
115
115
|
@sent_message.deliver
|
116
116
|
|
117
117
|
hidden_received_message = create_message(:sender => @friend, :to => @user)
|
118
118
|
hidden_received_message.deliver
|
119
|
-
hidden_received_message.recipients.first.hide
|
119
|
+
hidden_received_message.recipients.first.hide
|
120
120
|
@received_message = create_message(:sender => @friend, :to => @user)
|
121
121
|
@received_message.deliver
|
122
122
|
end
|
@@ -44,31 +44,31 @@ class MesageRecipientTest < Test::Unit::TestCase
|
|
44
44
|
def test_should_require_a_message
|
45
45
|
recipient = new_message_recipient(:message => nil)
|
46
46
|
assert !recipient.valid?
|
47
|
-
|
47
|
+
assert recipient.errors.invalid?(:message_id)
|
48
48
|
end
|
49
49
|
|
50
50
|
def test_should_require_a_kind
|
51
51
|
recipient = new_message_recipient(:kind => nil)
|
52
52
|
assert !recipient.valid?
|
53
|
-
|
53
|
+
assert recipient.errors.invalid?(:kind)
|
54
54
|
end
|
55
55
|
|
56
56
|
def test_should_require_a_state
|
57
57
|
recipient = new_message_recipient(:state => nil)
|
58
58
|
assert !recipient.valid?
|
59
|
-
|
59
|
+
assert recipient.errors.invalid?(:state)
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_should_require_a_receiver_id
|
63
63
|
recipient = new_message_recipient(:receiver => nil)
|
64
64
|
assert !recipient.valid?
|
65
|
-
|
65
|
+
assert recipient.errors.invalid?(:receiver_id)
|
66
66
|
end
|
67
67
|
|
68
68
|
def test_should_require_a_receiver_type
|
69
69
|
recipient = new_message_recipient(:receiver => nil)
|
70
70
|
assert !recipient.valid?
|
71
|
-
|
71
|
+
assert recipient.errors.invalid?(:receiver_type)
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_should_not_require_a_position
|
@@ -224,7 +224,7 @@ end
|
|
224
224
|
class MessageRecipientHiddenTest < Test::Unit::TestCase
|
225
225
|
def setup
|
226
226
|
@recipient = create_message_recipient
|
227
|
-
@recipient.hide
|
227
|
+
@recipient.hide
|
228
228
|
end
|
229
229
|
|
230
230
|
def test_should_record_when_it_was_hidden
|
@@ -239,8 +239,8 @@ end
|
|
239
239
|
class MessageRecipientUnhiddenTest < Test::Unit::TestCase
|
240
240
|
def setup
|
241
241
|
@recipient = create_message_recipient
|
242
|
-
@recipient.hide
|
243
|
-
@recipient.unhide
|
242
|
+
@recipient.hide
|
243
|
+
@recipient.unhide
|
244
244
|
end
|
245
245
|
|
246
246
|
def test_should_not_have_the_recorded_value_when_it_was_hidden
|
data/test/unit/message_test.rb
CHANGED
@@ -36,19 +36,19 @@ class MessageTest < Test::Unit::TestCase
|
|
36
36
|
def test_should_require_a_sender_id
|
37
37
|
message = new_message(:sender => nil)
|
38
38
|
assert !message.valid?
|
39
|
-
|
39
|
+
assert message.errors.invalid?(:sender_id)
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_should_require_a_sender_type
|
43
43
|
message = new_message(:sender => nil)
|
44
44
|
assert !message.valid?
|
45
|
-
|
45
|
+
assert message.errors.invalid?(:sender_type)
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_should_require_a_state
|
49
49
|
message = new_message(:state => nil)
|
50
50
|
assert !message.valid?
|
51
|
-
|
51
|
+
assert message.errors.invalid?(:state)
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_should_not_require_a_subject
|
@@ -268,7 +268,7 @@ end
|
|
268
268
|
class MessageHiddenTest < Test::Unit::TestCase
|
269
269
|
def setup
|
270
270
|
@message = create_message
|
271
|
-
@message.hide
|
271
|
+
@message.hide
|
272
272
|
end
|
273
273
|
|
274
274
|
def test_should_record_when_it_was_hidden
|
@@ -283,8 +283,8 @@ end
|
|
283
283
|
class MessageUnhiddenTest < Test::Unit::TestCase
|
284
284
|
def setup
|
285
285
|
@message = create_message
|
286
|
-
@message.hide
|
287
|
-
@message.unhide
|
286
|
+
@message.hide
|
287
|
+
@message.unhide
|
288
288
|
end
|
289
289
|
|
290
290
|
def test_should_not_have_the_recorded_value_when_it_was_hidden
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_messages
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,29 +32,29 @@ extra_rdoc_files: []
|
|
32
32
|
|
33
33
|
files:
|
34
34
|
- app/models
|
35
|
-
- app/models/message.rb
|
36
35
|
- app/models/message_recipient.rb
|
36
|
+
- app/models/message.rb
|
37
37
|
- db/migrate
|
38
|
-
- db/migrate/002_create_message_recipients.rb
|
39
38
|
- db/migrate/001_create_messages.rb
|
39
|
+
- db/migrate/002_create_message_recipients.rb
|
40
40
|
- lib/has_messages.rb
|
41
|
-
- test/
|
42
|
-
- test/
|
43
|
-
- test/app_root/app/models
|
44
|
-
- test/app_root/app/models/user.rb
|
45
|
-
- test/app_root/config
|
46
|
-
- test/app_root/config/environment.rb
|
47
|
-
- test/app_root/db
|
48
|
-
- test/app_root/db/migrate
|
49
|
-
- test/app_root/db/migrate/001_create_users.rb
|
50
|
-
- test/app_root/db/migrate/002_migrate_has_messages_to_version_2.rb
|
41
|
+
- test/factory.rb
|
42
|
+
- test/test_helper.rb
|
51
43
|
- test/functional
|
52
44
|
- test/functional/has_messages_test.rb
|
53
|
-
- test/test_helper.rb
|
54
|
-
- test/factory.rb
|
55
45
|
- test/unit
|
56
46
|
- test/unit/message_test.rb
|
57
47
|
- test/unit/message_recipient_test.rb
|
48
|
+
- test/app_root
|
49
|
+
- test/app_root/db
|
50
|
+
- test/app_root/db/migrate
|
51
|
+
- test/app_root/db/migrate/002_migrate_has_messages_to_version_2.rb
|
52
|
+
- test/app_root/db/migrate/001_create_users.rb
|
53
|
+
- test/app_root/config
|
54
|
+
- test/app_root/config/environment.rb
|
55
|
+
- test/app_root/app
|
56
|
+
- test/app_root/app/models
|
57
|
+
- test/app_root/app/models/user.rb
|
58
58
|
- CHANGELOG.rdoc
|
59
59
|
- init.rb
|
60
60
|
- LICENSE
|