acts-as-messageable 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.7
1
+ 0.4.8
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "acts-as-messageable"
8
- s.version = "0.4.7"
8
+ s.version = "0.4.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Piotr Nielacny"]
12
- s.date = "2012-10-02"
12
+ s.date = "2013-02-05"
13
13
  s.email = "piotr.nielacny@gmail.com"
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -1,7 +1,7 @@
1
- module ActsAsMessageable
2
- autoload :Model, 'acts-as-messageable/model'
3
- autoload :Message, 'acts-as-messageable/message'
4
- autoload :Relation, 'acts-as-messageable/relation'
5
- end
6
-
7
- require 'acts-as-messageable/railtie'
1
+ module ActsAsMessageable
2
+ autoload :Model, 'acts-as-messageable/model'
3
+ autoload :Message, 'acts-as-messageable/message'
4
+ autoload :Relation, 'acts-as-messageable/relation'
5
+ end
6
+
7
+ require 'acts-as-messageable/railtie'
@@ -1,95 +1,97 @@
1
- require 'ancestry'
2
-
3
- module ActsAsMessageable
4
- class Message < ::ActiveRecord::Base
5
- has_ancestry
6
-
7
- belongs_to :received_messageable, :polymorphic => true
8
- belongs_to :sent_messageable, :polymorphic => true
9
-
10
- attr_accessible :topic,
11
- :body,
12
- :received_messageable_type,
13
- :received_messageable_id,
14
- :sent_messageable_type,
15
- :sent_messageable_id,
16
- :opened,
17
- :recipient_delete,
18
- :sender_delete,
19
- :recipient_permanent_delete,
20
- :sender_permanent_delete,
21
- :created_at,
22
- :updated_at
23
-
24
- attr_accessor :removed, :restored
25
- cattr_accessor :required
26
-
27
-
28
- # Sample documentation for scope
29
- default_scope order("created_at desc")
30
- scope :are_from, lambda { |*args| where(:sent_messageable_id => args.first, :sent_messageable_type => args.first.class.name) }
31
- scope :are_to, lambda { |*args| where(:received_messageable_id => args.first, :received_messageable_type => args.first.class.name) }
32
- scope :with_id, lambda { |*args|
33
- ActiveSupport::Deprecation.warn("Calling with_id is deprecated. Please use `find` instead.")
34
- where(:id => args.first)
35
- }
36
-
37
- scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and
38
- sent_messageable_id = :sent_id and
39
- sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or
40
- (received_messageable_type = :received_type and
41
- received_messageable_id = :received_id and
42
- recipient_delete = :r_delete and recipient_permanent_delete = :r_perm_delete)",
43
- :sent_type => args.first.class.resolve_active_record_ancestor.to_s,
44
- :sent_id => args.first.id,
45
- :received_type => args.first.class.resolve_active_record_ancestor.to_s,
46
- :received_id => args.first.id,
47
- :r_delete => args.last,
48
- :s_delete => args.last,
49
- :r_perm_delete => false,
50
- :s_perm_delete => false)
51
- }
52
- scope :readed, lambda { where(:opened => true) }
53
- scope :unreaded, lambda { where(:opened => false) }
54
- scope :deleted, lambda { where(:recipient_delete => true, :sender_delete => true) }
55
-
56
- def open?
57
- self.opened?
58
- end
59
-
60
- def open
61
- update_attributes!(:opened => true)
62
- end
63
- alias :mark_as_read :open
64
- alias :read :open
65
-
66
- def close
67
- update_attributes!(:opened => false)
68
- end
69
- alias :mark_as_unread :close
70
- alias :unread :close
71
-
72
- alias :from :sent_messageable
73
- alias :to :received_messageable
74
-
75
- def participant?(user)
76
- (to == user) || (from == user)
77
- end
78
-
79
- def conversation
80
- root.subtree
81
- end
82
-
83
- def delete
84
- self.removed = true
85
- end
86
-
87
- def restore
88
- self.restored = true
89
- end
90
-
91
- def reply(*args)
92
- to.reply_to(self, *args)
93
- end
94
- end
95
- end
1
+ require 'ancestry'
2
+
3
+ module ActsAsMessageable
4
+ class Message < ::ActiveRecord::Base
5
+ belongs_to :received_messageable, :polymorphic => true
6
+ belongs_to :sent_messageable, :polymorphic => true
7
+
8
+ attr_accessible :topic,
9
+ :body,
10
+ :received_messageable_type,
11
+ :received_messageable_id,
12
+ :sent_messageable_type,
13
+ :sent_messageable_id,
14
+ :opened,
15
+ :recipient_delete,
16
+ :sender_delete,
17
+ :recipient_permanent_delete,
18
+ :sender_permanent_delete,
19
+ :created_at,
20
+ :updated_at
21
+
22
+ attr_accessor :removed, :restored
23
+ cattr_accessor :required
24
+
25
+
26
+ # Sample documentation for scope
27
+ default_scope order("created_at desc")
28
+ scope :are_from, lambda { |*args| where(:sent_messageable_id => args.first, :sent_messageable_type => args.first.class.name) }
29
+ scope :are_to, lambda { |*args| where(:received_messageable_id => args.first, :received_messageable_type => args.first.class.name) }
30
+ scope :with_id, lambda { |*args|
31
+ ActiveSupport::Deprecation.warn("Calling with_id is deprecated. Please use `find` instead.")
32
+ where(:id => args.first)
33
+ }
34
+
35
+ scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and
36
+ sent_messageable_id = :sent_id and
37
+ sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or
38
+ (received_messageable_type = :received_type and
39
+ received_messageable_id = :received_id and
40
+ recipient_delete = :r_delete and recipient_permanent_delete = :r_perm_delete)",
41
+ :sent_type => args.first.class.resolve_active_record_ancestor.to_s,
42
+ :sent_id => args.first.id,
43
+ :received_type => args.first.class.resolve_active_record_ancestor.to_s,
44
+ :received_id => args.first.id,
45
+ :r_delete => args.last,
46
+ :s_delete => args.last,
47
+ :r_perm_delete => false,
48
+ :s_perm_delete => false)
49
+ }
50
+ scope :readed, lambda { where(:opened => true) }
51
+ scope :unreaded, lambda { where(:opened => false) }
52
+ scope :deleted, lambda { where(:recipient_delete => true, :sender_delete => true) }
53
+
54
+ def open?
55
+ self.opened?
56
+ end
57
+
58
+ def open
59
+ update_attributes!(:opened => true)
60
+ end
61
+ alias :mark_as_read :open
62
+ alias :read :open
63
+
64
+ def close
65
+ update_attributes!(:opened => false)
66
+ end
67
+ alias :mark_as_unread :close
68
+ alias :unread :close
69
+
70
+ alias :from :sent_messageable
71
+ alias :to :received_messageable
72
+
73
+ def real_receiver(user)
74
+ user == from ? to : from
75
+ end
76
+
77
+ def participant?(user)
78
+ (to == user) || (from == user)
79
+ end
80
+
81
+ def conversation
82
+ root.subtree
83
+ end
84
+
85
+ def delete
86
+ self.removed = true
87
+ end
88
+
89
+ def restore
90
+ self.restored = true
91
+ end
92
+
93
+ def reply(*args)
94
+ to.reply_to(self, *args)
95
+ end
96
+ end
97
+ end
@@ -1,157 +1,165 @@
1
- module ActsAsMessageable
2
- module Model
3
-
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- mattr_accessor :messages_class_name
10
-
11
- # Method make ActiveRecord::Base object messageable
12
- # @param [Symbol] :table_name - table name for messages
13
- # @param [String] :class_name - message class name
14
- # @param [Array, Symbol] :required - required fields in message
15
- # @param [Symbol] :dependent - dependent option from ActiveRecord has_many method
16
- def acts_as_messageable(options = {})
17
- has_many :received_messages_relation,
18
- :as => :received_messageable,
19
- :class_name => options[:class_name] || "ActsAsMessageable::Message",
20
- :dependent => options[:dependent] || :nullify
21
- has_many :sent_messages_relation,
22
- :as => :sent_messageable,
23
- :class_name => options[:class_name] || "ActsAsMessageable::Message",
24
- :dependent => options[:dependent] || :nullify
25
-
26
- self.messages_class_name = (options[:class_name] || "ActsAsMessageable::Message").constantize
27
-
28
- if self.messages_class_name.respond_to?(:table_name=)
29
- self.messages_class_name.table_name = (options[:table_name] || "messages")
30
- else
31
- self.messages_class_name.set_table_name(options[:table_name] || "messages")
32
- ActiveSupport::Deprecation.warn("Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.")
33
- end
34
-
35
- self.messages_class_name.required = Array.wrap(options[:required] || [:topic, :body])
36
- self.messages_class_name.validates_presence_of self.messages_class_name.required
37
-
38
- include ActsAsMessageable::Model::InstanceMethods
39
- end
40
-
41
- # Method recognize real object class
42
- # @return [ActiveRecord::Base] class or relation object
43
- def resolve_active_record_ancestor
44
- self.reflect_on_association(:received_messages_relation).active_record
45
- end
46
-
47
- end
48
-
49
- module InstanceMethods
50
- # @return [ActiveRecord::Relation] all messages connected with user
51
- def messages(trash = false)
52
- result = self.class.messages_class_name.connected_with(self, trash)
53
- result.relation_context = self
54
-
55
- result
56
- end
57
-
58
- # @return [ActiveRecord::Relation] returns all messages from inbox
59
- def received_messages
60
- result = received_messages_relation.scoped.where(:recipient_delete => false)
61
- result.relation_context = self
62
-
63
- result
64
- end
65
-
66
- # @return [ActiveRecord::Relation] returns all messages from outbox
67
- def sent_messages
68
- result = sent_messages_relation.scoped.where(:sender_delete => false)
69
- result.relation_context = self
70
-
71
- result
72
- end
73
-
74
- # @return [ActiveRecord::Relation] returns all messages from trash
75
- def deleted_messages
76
- messages true
77
- end
78
-
79
- # Method sens message to another user
80
- # @param [ActiveRecord::Base] to
81
- # @param [String] topic
82
- # @param [String] body
83
- #
84
- # @return [ActsAsMessageable::Message] the message object
85
- def send_message(to, *args)
86
- message_attributes = {}
87
-
88
- case args.first
89
- when String
90
- self.class.messages_class_name.required.each_with_index do |attribute, index|
91
- message_attributes[attribute] = args[index]
92
- end
93
- when Hash
94
- message_attributes = args.first
95
- end
96
-
97
- message = self.class.messages_class_name.create message_attributes
98
- message.received_messageable = to
99
- message.sent_messageable = self
100
-
101
- message.save
102
-
103
- message
104
- end
105
-
106
- # Reply to given message
107
- # @param [ActsAsMessageable::Message] message
108
- # @param [String] topic
109
- # @param [String] body
110
- #
111
- # @return [ActsAsMessageable::Message] a message that is a response to a given message
112
- def reply_to(message, *args)
113
- current_user = self
114
-
115
- if message.participant?(current_user)
116
- reply_message = send_message(message.from, *args)
117
- reply_message.parent = message
118
- reply_message.save
119
-
120
- reply_message
121
- end
122
- end
123
-
124
- # Mark message as deleted
125
- def delete_message(message)
126
- current_user = self
127
-
128
- case current_user
129
- when message.to
130
- attribute = message.recipient_delete ? :recipient_permanent_delete : :recipient_delete
131
- when message.from
132
- attribute = message.sender_delete ? :sender_permanent_delete : :sender_delete
133
- else
134
- raise "#{current_user} can't delete this message"
135
- end
136
-
137
- message.update_attributes!(attribute => true)
138
- end
139
-
140
- # Mark message as restored
141
- def restore_message(message)
142
- current_user = self
143
-
144
- case current_user
145
- when message.to
146
- attribute = :recipient_delete
147
- when message.from
148
- attribute = :sender_delete
149
- else
150
- raise "#{current_user} can't delete this message"
151
- end
152
-
153
- message.update_attributes!(attribute => false)
154
- end
155
- end
156
- end
157
- end
1
+ module ActsAsMessageable
2
+ module Model
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ mattr_accessor :messages_class_name
10
+
11
+ # Method make ActiveRecord::Base object messageable
12
+ # @param [Symbol] :table_name - table name for messages
13
+ # @param [String] :class_name - message class name
14
+ # @param [Array, Symbol] :required - required fields in message
15
+ # @param [Symbol] :dependent - dependent option from ActiveRecord has_many method
16
+ def acts_as_messageable(options = {})
17
+ default_options = {
18
+ :table_name => "messages",
19
+ :class_name => "ActsAsMessageable::Message",
20
+ :required => [:topic, :body],
21
+ :dependent => :nullify,
22
+ }
23
+ options = default_options.merge(options)
24
+
25
+ has_many :received_messages_relation,
26
+ :as => :received_messageable,
27
+ :class_name => options[:class_name],
28
+ :dependent => options[:dependent]
29
+ has_many :sent_messages_relation,
30
+ :as => :sent_messageable,
31
+ :class_name => options[:class_name],
32
+ :dependent => options[:dependent]
33
+
34
+ self.messages_class_name = options[:class_name].constantize
35
+ self.messages_class_name.has_ancestry
36
+
37
+ if self.messages_class_name.respond_to?(:table_name=)
38
+ self.messages_class_name.table_name = options[:table_name]
39
+ else
40
+ self.messages_class_name.set_table_name(options[:table_name])
41
+ ActiveSupport::Deprecation.warn("Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.")
42
+ end
43
+
44
+ self.messages_class_name.required = Array.wrap(options[:required])
45
+ self.messages_class_name.validates_presence_of self.messages_class_name.required
46
+
47
+ include ActsAsMessageable::Model::InstanceMethods
48
+ end
49
+
50
+ # Method recognize real object class
51
+ # @return [ActiveRecord::Base] class or relation object
52
+ def resolve_active_record_ancestor
53
+ self.reflect_on_association(:received_messages_relation).active_record
54
+ end
55
+
56
+ end
57
+
58
+ module InstanceMethods
59
+ # @return [ActiveRecord::Relation] all messages connected with user
60
+ def messages(trash = false)
61
+ result = self.class.messages_class_name.connected_with(self, trash)
62
+ result.relation_context = self
63
+
64
+ result
65
+ end
66
+
67
+ # @return [ActiveRecord::Relation] returns all messages from inbox
68
+ def received_messages
69
+ result = received_messages_relation.scoped.where(:recipient_delete => false)
70
+ result.relation_context = self
71
+
72
+ result
73
+ end
74
+
75
+ # @return [ActiveRecord::Relation] returns all messages from outbox
76
+ def sent_messages
77
+ result = sent_messages_relation.scoped.where(:sender_delete => false)
78
+ result.relation_context = self
79
+
80
+ result
81
+ end
82
+
83
+ # @return [ActiveRecord::Relation] returns all messages from trash
84
+ def deleted_messages
85
+ messages true
86
+ end
87
+
88
+ # Method sens message to another user
89
+ # @param [ActiveRecord::Base] to
90
+ # @param [String] topic
91
+ # @param [String] body
92
+ #
93
+ # @return [ActsAsMessageable::Message] the message object
94
+ def send_message(to, *args)
95
+ message_attributes = {}
96
+
97
+ case args.first
98
+ when String
99
+ self.class.messages_class_name.required.each_with_index do |attribute, index|
100
+ message_attributes[attribute] = args[index]
101
+ end
102
+ when Hash
103
+ message_attributes = args.first
104
+ end
105
+
106
+ message = self.class.messages_class_name.create message_attributes
107
+ message.received_messageable = to
108
+ message.sent_messageable = self
109
+ message.save
110
+
111
+ message
112
+ end
113
+
114
+ # Reply to given message
115
+ # @param [ActsAsMessageable::Message] message
116
+ # @param [String] topic
117
+ # @param [String] body
118
+ #
119
+ # @return [ActsAsMessageable::Message] a message that is a response to a given message
120
+ def reply_to(message, *args)
121
+ current_user = self
122
+
123
+ if message.participant?(current_user)
124
+ reply_message = send_message(message.real_receiver(current_user), *args)
125
+ reply_message.parent = message
126
+ reply_message.save
127
+
128
+ reply_message
129
+ end
130
+ end
131
+
132
+ # Mark message as deleted
133
+ def delete_message(message)
134
+ current_user = self
135
+
136
+ case current_user
137
+ when message.to
138
+ attribute = message.recipient_delete ? :recipient_permanent_delete : :recipient_delete
139
+ when message.from
140
+ attribute = message.sender_delete ? :sender_permanent_delete : :sender_delete
141
+ else
142
+ raise "#{current_user} can't delete this message"
143
+ end
144
+
145
+ message.update_attributes!(attribute => true)
146
+ end
147
+
148
+ # Mark message as restored
149
+ def restore_message(message)
150
+ current_user = self
151
+
152
+ case current_user
153
+ when message.to
154
+ attribute = :recipient_delete
155
+ when message.from
156
+ attribute = :sender_delete
157
+ else
158
+ raise "#{current_user} can't delete this message"
159
+ end
160
+
161
+ message.update_attributes!(attribute => false)
162
+ end
163
+ end
164
+ end
165
+ end