acts-as-messageable 0.4.8 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +0 -0
- data/.rspec +1 -1
- data/.travis.yml +14 -2
- data/Gemfile +13 -12
- data/Gemfile.lock +121 -0
- data/README.md +360 -278
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/acts-as-messageable.gemspec +21 -10
- data/gemfiles/Gemfile-3.0 +1 -0
- data/gemfiles/Gemfile-3.1 +1 -0
- data/gemfiles/Gemfile-3.2 +1 -0
- data/gemfiles/Gemfile-4.0 +6 -0
- data/lib/acts-as-messageable.rb +18 -7
- data/lib/acts-as-messageable/message.rb +62 -97
- data/lib/acts-as-messageable/model.rb +181 -165
- data/lib/acts-as-messageable/rails3.rb +15 -0
- data/lib/acts-as-messageable/rails4.rb +19 -0
- data/lib/acts-as-messageable/scopes.rb +37 -0
- data/spec/acts-as-messageable_spec.rb +293 -266
- data/spec/custom-class_spec.rb +11 -1
- data/spec/group-messages_spec.rb +28 -0
- data/spec/spec_helper.rb +81 -65
- data/spec/support/admin.rb +3 -3
- data/spec/support/send_message.rb +1 -1
- data/spec/support/user.rb +6 -7
- metadata +40 -38
data/Rakefile
CHANGED
@@ -25,7 +25,7 @@ Jeweler::RubygemsDotOrgTasks.new
|
|
25
25
|
require 'rspec/core'
|
26
26
|
require 'rspec/core/rake_task'
|
27
27
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
28
|
-
spec.rspec_opts = ['-fd -c']
|
28
|
+
spec.rspec_opts = ['-fd -c --order random']
|
29
29
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
30
30
|
end
|
31
31
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.9
|
data/acts-as-messageable.gemspec
CHANGED
@@ -2,22 +2,25 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: acts-as-messageable 0.4.9 ruby lib
|
5
6
|
|
6
7
|
Gem::Specification.new do |s|
|
7
8
|
s.name = "acts-as-messageable"
|
8
|
-
s.version = "0.4.
|
9
|
+
s.version = "0.4.9"
|
9
10
|
|
10
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
12
|
s.authors = ["Piotr Nielacny"]
|
12
|
-
s.date = "2013-
|
13
|
+
s.date = "2013-11-25"
|
13
14
|
s.email = "piotr.nielacny@gmail.com"
|
14
15
|
s.extra_rdoc_files = [
|
15
16
|
"README.md"
|
16
17
|
]
|
17
18
|
s.files = [
|
19
|
+
".coveralls.yml",
|
18
20
|
".rspec",
|
19
21
|
".travis.yml",
|
20
22
|
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
21
24
|
"MIT-LICENSE",
|
22
25
|
"README.md",
|
23
26
|
"Rakefile",
|
@@ -26,17 +29,22 @@ Gem::Specification.new do |s|
|
|
26
29
|
"gemfiles/Gemfile-3.0",
|
27
30
|
"gemfiles/Gemfile-3.1",
|
28
31
|
"gemfiles/Gemfile-3.2",
|
32
|
+
"gemfiles/Gemfile-4.0",
|
29
33
|
"lib/acts-as-messageable.rb",
|
30
34
|
"lib/acts-as-messageable/message.rb",
|
31
35
|
"lib/acts-as-messageable/model.rb",
|
36
|
+
"lib/acts-as-messageable/rails3.rb",
|
37
|
+
"lib/acts-as-messageable/rails4.rb",
|
32
38
|
"lib/acts-as-messageable/railtie.rb",
|
33
39
|
"lib/acts-as-messageable/relation.rb",
|
40
|
+
"lib/acts-as-messageable/scopes.rb",
|
34
41
|
"lib/generators/acts-as-messageable/migration/migration_generator.rb",
|
35
42
|
"lib/generators/acts-as-messageable/migration/templates/migration.rb",
|
36
43
|
"lib/generators/acts-as-messageable/migration/templates/migration_permanent.rb",
|
37
44
|
"spec/acts-as-messageable_spec.rb",
|
38
45
|
"spec/custom-class_spec.rb",
|
39
46
|
"spec/custom-required_spec.rb",
|
47
|
+
"spec/group-messages_spec.rb",
|
40
48
|
"spec/spec_helper.rb",
|
41
49
|
"spec/support/admin.rb",
|
42
50
|
"spec/support/send_message.rb",
|
@@ -44,37 +52,40 @@ Gem::Specification.new do |s|
|
|
44
52
|
]
|
45
53
|
s.homepage = "http://github.com/LTe/acts-as-messageable"
|
46
54
|
s.require_paths = ["lib"]
|
47
|
-
s.rubygems_version = "1.
|
55
|
+
s.rubygems_version = "2.1.11"
|
48
56
|
s.summary = "Make user messageable!;-)"
|
49
57
|
|
50
58
|
if s.respond_to? :specification_version then
|
51
|
-
s.specification_version =
|
59
|
+
s.specification_version = 4
|
52
60
|
|
53
61
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
54
62
|
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
55
63
|
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
56
|
-
s.add_runtime_dependency(%q<ancestry>, ["
|
64
|
+
s.add_runtime_dependency(%q<ancestry>, [">= 1.3.0"])
|
57
65
|
s.add_runtime_dependency(%q<railties>, [">= 3.0.0"])
|
58
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.
|
66
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.0"])
|
59
67
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.0"])
|
60
68
|
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
69
|
+
s.add_development_dependency(%q<coveralls>, [">= 0"])
|
61
70
|
else
|
62
71
|
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
63
72
|
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
64
|
-
s.add_dependency(%q<ancestry>, ["
|
73
|
+
s.add_dependency(%q<ancestry>, [">= 1.3.0"])
|
65
74
|
s.add_dependency(%q<railties>, [">= 3.0.0"])
|
66
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
75
|
+
s.add_dependency(%q<rspec>, ["~> 2.0"])
|
67
76
|
s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
|
68
77
|
s.add_dependency(%q<sqlite3>, [">= 0"])
|
78
|
+
s.add_dependency(%q<coveralls>, [">= 0"])
|
69
79
|
end
|
70
80
|
else
|
71
81
|
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
72
82
|
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
73
|
-
s.add_dependency(%q<ancestry>, ["
|
83
|
+
s.add_dependency(%q<ancestry>, [">= 1.3.0"])
|
74
84
|
s.add_dependency(%q<railties>, [">= 3.0.0"])
|
75
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
85
|
+
s.add_dependency(%q<rspec>, ["~> 2.0"])
|
76
86
|
s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
|
77
87
|
s.add_dependency(%q<sqlite3>, [">= 0"])
|
88
|
+
s.add_dependency(%q<coveralls>, [">= 0"])
|
78
89
|
end
|
79
90
|
end
|
80
91
|
|
data/gemfiles/Gemfile-3.0
CHANGED
data/gemfiles/Gemfile-3.1
CHANGED
data/gemfiles/Gemfile-3.2
CHANGED
data/lib/acts-as-messageable.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
|
-
module ActsAsMessageable
|
2
|
-
autoload :Model, 'acts-as-messageable/model'
|
3
|
-
autoload :
|
4
|
-
autoload :
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module ActsAsMessageable
|
2
|
+
autoload :Model, 'acts-as-messageable/model'
|
3
|
+
autoload :Scopes, 'acts-as-messageable/scopes'
|
4
|
+
autoload :Message, 'acts-as-messageable/message'
|
5
|
+
autoload :Relation, 'acts-as-messageable/relation'
|
6
|
+
autoload :Rails3, 'acts-as-messageable/rails3'
|
7
|
+
autoload :Rails4, 'acts-as-messageable/rails4'
|
8
|
+
|
9
|
+
def self.rails_api
|
10
|
+
if Rails::VERSION::MAJOR >= 4
|
11
|
+
Rails4
|
12
|
+
else
|
13
|
+
Rails3
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'acts-as-messageable/railtie'
|
@@ -1,97 +1,62 @@
|
|
1
|
-
require 'ancestry'
|
2
|
-
|
3
|
-
module ActsAsMessageable
|
4
|
-
class Message < ::ActiveRecord::Base
|
5
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
def
|
55
|
-
self
|
56
|
-
end
|
57
|
-
|
58
|
-
def
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
|
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
|
+
require 'ancestry'
|
2
|
+
|
3
|
+
module ActsAsMessageable
|
4
|
+
class Message < ::ActiveRecord::Base
|
5
|
+
include ActsAsMessageable::Scopes
|
6
|
+
|
7
|
+
belongs_to :received_messageable, :polymorphic => true
|
8
|
+
belongs_to :sent_messageable, :polymorphic => true
|
9
|
+
|
10
|
+
attr_accessor :removed, :restored
|
11
|
+
cattr_accessor :required
|
12
|
+
|
13
|
+
ActsAsMessageable.rails_api.new(self).default_scope("created_at desc")
|
14
|
+
|
15
|
+
def open?
|
16
|
+
self.opened?
|
17
|
+
end
|
18
|
+
|
19
|
+
def open
|
20
|
+
update_attributes!(:opened => true)
|
21
|
+
end
|
22
|
+
alias :mark_as_read :open
|
23
|
+
alias :read :open
|
24
|
+
|
25
|
+
def close
|
26
|
+
update_attributes!(:opened => false)
|
27
|
+
end
|
28
|
+
alias :mark_as_unread :close
|
29
|
+
alias :unread :close
|
30
|
+
|
31
|
+
alias :from :sent_messageable
|
32
|
+
alias :to :received_messageable
|
33
|
+
|
34
|
+
def real_receiver(user)
|
35
|
+
user == from ? to : from
|
36
|
+
end
|
37
|
+
|
38
|
+
def participant?(user)
|
39
|
+
user.class.group_messages || (to == user) || (from == user)
|
40
|
+
end
|
41
|
+
|
42
|
+
def conversation
|
43
|
+
root.subtree
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete
|
47
|
+
self.removed = true
|
48
|
+
end
|
49
|
+
|
50
|
+
def restore
|
51
|
+
self.restored = true
|
52
|
+
end
|
53
|
+
|
54
|
+
def reply(*args)
|
55
|
+
to.reply_to(self, *args)
|
56
|
+
end
|
57
|
+
|
58
|
+
def people
|
59
|
+
conversation.map{ |x| x.from }.uniq!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -1,165 +1,181 @@
|
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
:
|
28
|
-
:
|
29
|
-
|
30
|
-
|
31
|
-
:
|
32
|
-
:
|
33
|
-
|
34
|
-
|
35
|
-
self.messages_class_name.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
self.messages_class_name.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
result
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
result
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
#
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
message
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
#
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
# Mark message as
|
149
|
-
def
|
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 =>
|
162
|
-
end
|
163
|
-
|
164
|
-
|
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, :group_messages
|
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
|
+
:group_messages => false,
|
23
|
+
}
|
24
|
+
options = default_options.merge(options)
|
25
|
+
|
26
|
+
has_many :received_messages_relation,
|
27
|
+
:as => :received_messageable,
|
28
|
+
:class_name => options[:class_name],
|
29
|
+
:dependent => options[:dependent]
|
30
|
+
has_many :sent_messages_relation,
|
31
|
+
:as => :sent_messageable,
|
32
|
+
:class_name => options[:class_name],
|
33
|
+
:dependent => options[:dependent]
|
34
|
+
|
35
|
+
self.messages_class_name = options[:class_name].constantize
|
36
|
+
self.messages_class_name.has_ancestry
|
37
|
+
|
38
|
+
if self.messages_class_name.respond_to?(:table_name=)
|
39
|
+
self.messages_class_name.table_name = options[:table_name]
|
40
|
+
self.messages_class_name.initialize_scopes
|
41
|
+
else
|
42
|
+
self.messages_class_name.set_table_name(options[:table_name])
|
43
|
+
ActiveSupport::Deprecation.warn("Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.")
|
44
|
+
end
|
45
|
+
|
46
|
+
self.messages_class_name.required = Array.wrap(options[:required])
|
47
|
+
self.messages_class_name.validates_presence_of self.messages_class_name.required
|
48
|
+
self.group_messages = options[:group_messages]
|
49
|
+
|
50
|
+
include ActsAsMessageable::Model::InstanceMethods
|
51
|
+
end
|
52
|
+
|
53
|
+
# Method recognize real object class
|
54
|
+
# @return [ActiveRecord::Base] class or relation object
|
55
|
+
def resolve_active_record_ancestor
|
56
|
+
self.reflect_on_association(:received_messages_relation).active_record
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
module InstanceMethods
|
62
|
+
# @return [ActiveRecord::Relation] all messages connected with user
|
63
|
+
def messages(trash = false)
|
64
|
+
result = self.class.messages_class_name.connected_with(self, trash)
|
65
|
+
result.relation_context = self
|
66
|
+
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [ActiveRecord::Relation] returns all messages from inbox
|
71
|
+
def received_messages
|
72
|
+
result = ActsAsMessageable.rails_api.new(received_messages_relation)
|
73
|
+
result = result.scoped.where(:recipient_delete => false)
|
74
|
+
result.relation_context = self
|
75
|
+
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [ActiveRecord::Relation] returns all messages from outbox
|
80
|
+
def sent_messages
|
81
|
+
result = ActsAsMessageable.rails_api.new(sent_messages_relation)
|
82
|
+
result = result.scoped.where(:sender_delete => false)
|
83
|
+
result.relation_context = self
|
84
|
+
|
85
|
+
result
|
86
|
+
end
|
87
|
+
|
88
|
+
# @return [ActiveRecord::Relation] returns all messages from trash
|
89
|
+
def deleted_messages
|
90
|
+
messages true
|
91
|
+
end
|
92
|
+
|
93
|
+
# Method sens message to another user
|
94
|
+
# @param [ActiveRecord::Base] to
|
95
|
+
# @param [String] topic
|
96
|
+
# @param [String] body
|
97
|
+
#
|
98
|
+
# @return [ActsAsMessageable::Message] the message object
|
99
|
+
def send_message(to, *args)
|
100
|
+
message_attributes = {}
|
101
|
+
|
102
|
+
case args.first
|
103
|
+
when String
|
104
|
+
self.class.messages_class_name.required.each_with_index do |attribute, index|
|
105
|
+
message_attributes[attribute] = args[index]
|
106
|
+
end
|
107
|
+
when Hash
|
108
|
+
message_attributes = args.first
|
109
|
+
end
|
110
|
+
|
111
|
+
message = self.class.messages_class_name.new message_attributes
|
112
|
+
message.received_messageable = to
|
113
|
+
message.sent_messageable = self
|
114
|
+
message.save
|
115
|
+
|
116
|
+
message
|
117
|
+
end
|
118
|
+
|
119
|
+
# Method send message to another user
|
120
|
+
# and raise exception in case of validation errors
|
121
|
+
# @param [ActiveRecord::Base] to
|
122
|
+
# @param [String] topic
|
123
|
+
# @param [String] body
|
124
|
+
#
|
125
|
+
# @return [ActsAsMessageable::Message] the message object
|
126
|
+
def send_message!(to, *args)
|
127
|
+
send_message(to, *args).save!
|
128
|
+
end
|
129
|
+
|
130
|
+
# Reply to given message
|
131
|
+
# @param [ActsAsMessageable::Message] message
|
132
|
+
# @param [String] topic
|
133
|
+
# @param [String] body
|
134
|
+
#
|
135
|
+
# @return [ActsAsMessageable::Message] a message that is a response to a given message
|
136
|
+
def reply_to(message, *args)
|
137
|
+
current_user = self
|
138
|
+
|
139
|
+
if message.participant?(current_user)
|
140
|
+
reply_message = send_message(message.real_receiver(current_user), *args)
|
141
|
+
reply_message.parent = message
|
142
|
+
reply_message.save
|
143
|
+
|
144
|
+
reply_message
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Mark message as deleted
|
149
|
+
def delete_message(message)
|
150
|
+
current_user = self
|
151
|
+
|
152
|
+
case current_user
|
153
|
+
when message.to
|
154
|
+
attribute = message.recipient_delete ? :recipient_permanent_delete : :recipient_delete
|
155
|
+
when message.from
|
156
|
+
attribute = message.sender_delete ? :sender_permanent_delete : :sender_delete
|
157
|
+
else
|
158
|
+
raise "#{current_user} can't delete this message"
|
159
|
+
end
|
160
|
+
|
161
|
+
message.update_attributes!(attribute => true)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Mark message as restored
|
165
|
+
def restore_message(message)
|
166
|
+
current_user = self
|
167
|
+
|
168
|
+
case current_user
|
169
|
+
when message.to
|
170
|
+
attribute = :recipient_delete
|
171
|
+
when message.from
|
172
|
+
attribute = :sender_delete
|
173
|
+
else
|
174
|
+
raise "#{current_user} can't restore this message"
|
175
|
+
end
|
176
|
+
|
177
|
+
message.update_attributes!(attribute => false)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|