has_messages 0.3.0 → 0.3.1

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.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,10 @@
1
1
  == master
2
2
 
3
+ == 0.3.1 / 2008-01-11
4
+
5
+ * Use a state machine for the hidden_at field on Message/MessageRecipient
6
+ * Add dependency on state_machine 0.5.0
7
+
3
8
  == 0.3.0 / 2008-12-14
4
9
 
5
10
  * Remove the PluginAWeek namespace
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.3.0'
8
+ s.version = '0.3.1'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = 'Demonstrates a reference implementation for sending messages between users.'
11
11
 
@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s|
13
13
  s.require_path = 'lib'
14
14
  s.has_rdoc = true
15
15
  s.test_files = Dir['test/**/*_test.rb']
16
- s.add_dependency 'state_machine', '>= 0.3.0'
16
+ s.add_dependency 'state_machine', '>= 0.5.0'
17
17
 
18
18
  s.author = 'Aaron Pfeifer'
19
19
  s.email = 'aaron@pluginaweek.org'
@@ -14,12 +14,16 @@
14
14
  # * +queue+ - Queues the message so that you can send it in a separate process
15
15
  # * +deliver+ - Sends the message to all of the recipients
16
16
  #
17
- # == Hiding messages
17
+ # == Message visibility
18
18
  #
19
19
  # Although you can delete a message, it will also delete it from the inbox of all
20
- # the message's recipients. Instead, you can hide messages from users with the
21
- # following actions:
22
- # * +hide+ -Hides the message from the sender's inbox
20
+ # the message's recipients. Instead, you can change the *visibility* of the
21
+ # message. Messages have 1 of 2 states that define its visibility:
22
+ # * +visible+ - The message is visible to the sender
23
+ # * +hidden+ - The message is hidden from the sender
24
+ #
25
+ # The visibility of a message can be changed by running the associated action:
26
+ # * +hide+ -Hides the message from the sender
23
27
  # * +unhide+ - Makes the message visible again
24
28
  class Message < ActiveRecord::Base
25
29
  belongs_to :sender,
@@ -43,17 +47,33 @@ class Message < ActiveRecord::Base
43
47
  :conditions => {:hidden_at => nil}
44
48
 
45
49
  # Define actions for the message
46
- state_machine :state, :initial => 'unsent' do
50
+ state_machine :state, :initial => :unsent do
47
51
  # Queues the message so that it's sent in a separate process
48
52
  event :queue do
49
- transition :to => 'queued', :from => 'unsent', :if => :has_recipients?
53
+ transition :to => :queued, :from => :unsent, :if => :has_recipients?
50
54
  end
51
55
 
52
56
  # Sends the message to all of the recipients as long as at least one
53
57
  # recipient has been added
54
58
  event :deliver do
55
- transition :to => 'sent', :from => %w(unsent queued), :if => :has_recipients?
59
+ transition :to => :sent, :from => [:unsent, :queued], :if => :has_recipients?
60
+ end
61
+ end
62
+
63
+ # Defines actions for the visibility of the message
64
+ state_machine :hidden_at, :initial => :visible do
65
+ # Hides the message from the recipient's inbox
66
+ event :hide do
67
+ transition :to => :hidden
56
68
  end
69
+
70
+ # Makes the message visible in the recipient's inbox
71
+ event :unhide do
72
+ transition :to => :visible
73
+ end
74
+
75
+ state :visible, :value => nil
76
+ state :hidden, :value => lambda {Time.now}, :if => lambda {|value| value}
57
77
  end
58
78
 
59
79
  # Directly adds the receivers on the message (i.e. they are visible to all recipients)
@@ -101,21 +121,6 @@ class Message < ActiveRecord::Base
101
121
  message
102
122
  end
103
123
 
104
- # Hides the message from the sender's inbox
105
- def hide
106
- update_attribute(:hidden_at, Time.now)
107
- end
108
-
109
- # Makes the message visible in the sender's inbox
110
- def unhide
111
- update_attribute(:hidden_at, nil)
112
- end
113
-
114
- # Is this message still hidden from the sender's inbox?
115
- def hidden?
116
- hidden_at?
117
- end
118
-
119
124
  private
120
125
  # Create/destroy any receivers that were added/removed
121
126
  def update_recipients
@@ -17,8 +17,13 @@
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
- # message. Instead, you can hide messages from users with the following actions:
21
- # * +hide+ -Hides the message from the recipient's inbox
20
+ # message. Instead, you can change the *visibility* of the message. Messages
21
+ # have 1 of 2 states that define its visibility:
22
+ # * +visible+ - The message is visible to the recipient
23
+ # * +hidden+ - The message is hidden from the recipient
24
+ #
25
+ # The visibility of a message can be changed by running the associated action:
26
+ # * +hide+ -Hides the message from the recipient
22
27
  # * +unhide+ - Makes the message visible again
23
28
  class MessageRecipient < ActiveRecord::Base
24
29
  belongs_to :message
@@ -52,11 +57,28 @@ class MessageRecipient < ActiveRecord::Base
52
57
  named_scope :visible,
53
58
  :conditions => {:hidden_at => nil}
54
59
 
55
- state_machine :state, :initial => 'unread' do
60
+ # Defines actions for the recipient
61
+ state_machine :state, :initial => :unread do
56
62
  # Indicates that the message has been viewed by the receiver
57
63
  event :view do
58
- transition :to => 'read', :from => 'unread', :if => :message_sent?
64
+ transition :to => :read, :from => :unread, :if => :message_sent?
65
+ end
66
+ end
67
+
68
+ # Defines actions for the visibility of the message to the recipient
69
+ state_machine :hidden_at, :initial => :visible do
70
+ # Hides the message from the recipient's inbox
71
+ event :hide do
72
+ transition :to => :hidden
59
73
  end
74
+
75
+ # Makes the message visible in the recipient's inbox
76
+ event :unhide do
77
+ transition :to => :visible
78
+ end
79
+
80
+ state :visible, :value => nil
81
+ state :hidden, :value => lambda {Time.now}, :if => lambda {|value| value}
60
82
  end
61
83
 
62
84
  # Forwards this message, including the original subject and body in the new
@@ -87,25 +109,10 @@ class MessageRecipient < ActiveRecord::Base
87
109
  message
88
110
  end
89
111
 
90
- # Hides the message from the recipient's inbox
91
- def hide
92
- update_attribute(:hidden_at, Time.now)
93
- end
94
-
95
- # Makes the message visible in the recipient's inbox
96
- def unhide
97
- update_attribute(:hidden_at, nil)
98
- end
99
-
100
- # Is this message still hidden from the recipient's inbox?
101
- def hidden?
102
- hidden_at?
103
- end
104
-
105
112
  private
106
113
  # Has the message this recipient is on been sent?
107
114
  def message_sent?
108
- message.state == 'sent'
115
+ message.sent?
109
116
  end
110
117
 
111
118
  # Sets the position of the current recipient based on existing recipients
data/lib/has_messages.rb CHANGED
@@ -53,13 +53,13 @@ module HasMessages
53
53
  # Composed messages that have not yet been sent. These consists of all
54
54
  # messages that are currently in the "unsent" state.
55
55
  def unsent_messages
56
- messages.with_state('unsent')
56
+ messages.with_state(:unsent)
57
57
  end
58
58
 
59
59
  # Composed messages that have already been sent. These consists of all
60
60
  # messages that are currently in the "queued" or "sent" states.
61
61
  def sent_messages
62
- messages.with_states(%w(queued sent))
62
+ messages.with_states(:queued, :sent)
63
63
  end
64
64
  end
65
65
  end
@@ -234,6 +234,10 @@ class MessageRecipientHiddenTest < Test::Unit::TestCase
234
234
  def test_should_be_hidden
235
235
  assert @recipient.hidden?
236
236
  end
237
+
238
+ def test_should_not_be_visible
239
+ assert !@recipient.visible?
240
+ end
237
241
  end
238
242
 
239
243
  class MessageRecipientUnhiddenTest < Test::Unit::TestCase
@@ -250,6 +254,10 @@ class MessageRecipientUnhiddenTest < Test::Unit::TestCase
250
254
  def test_should_not_be_hidden
251
255
  assert !@recipient.hidden?
252
256
  end
257
+
258
+ def test_should_be_visible
259
+ assert @recipient.visible?
260
+ end
253
261
  end
254
262
 
255
263
  class MessageRecipientForwardedTest < Test::Unit::TestCase
@@ -278,6 +278,10 @@ class MessageHiddenTest < Test::Unit::TestCase
278
278
  def test_should_be_hidden
279
279
  assert @message.hidden?
280
280
  end
281
+
282
+ def test_should_not_be_visible
283
+ assert !@message.visible?
284
+ end
281
285
  end
282
286
 
283
287
  class MessageUnhiddenTest < Test::Unit::TestCase
@@ -294,6 +298,10 @@ class MessageUnhiddenTest < Test::Unit::TestCase
294
298
  def test_should_not_be_hidden
295
299
  assert !@message.hidden?
296
300
  end
301
+
302
+ def test_should_be_visible
303
+ assert @message.visible?
304
+ end
297
305
  end
298
306
 
299
307
  class MessageForwardedTest < Test::Unit::TestCase
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.3.0
4
+ version: 0.3.1
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-14 00:00:00 -05:00
12
+ date: 2009-01-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.0
23
+ version: 0.5.0
24
24
  version:
25
25
  description:
26
26
  email: aaron@pluginaweek.org