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.
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.8
1
+ 0.4.9
@@ -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.8"
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-02-05"
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.8.24"
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 = 3
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>, ["~> 1.3.0"])
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.11.0"])
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>, ["~> 1.3.0"])
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.11.0"])
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>, ["~> 1.3.0"])
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.11.0"])
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
@@ -4,3 +4,4 @@ gemspec :path => File.expand_path('../..', __FILE__)
4
4
  gem "activerecord", "~> 3.0.0"
5
5
  gem "activesupport", "~> 3.0.0"
6
6
  gem "railties", "~> 3.0.0"
7
+ gem "mime-types", "~> 1.25.1", :platforms => :ruby_18
data/gemfiles/Gemfile-3.1 CHANGED
@@ -4,3 +4,4 @@ gemspec :path => File.expand_path('../..', __FILE__)
4
4
  gem "activerecord", "~> 3.1.0"
5
5
  gem "activesupport", "~> 3.1.0"
6
6
  gem "railties", "~> 3.1.0"
7
+ gem "mime-types", "~> 1.25.1", :platforms => :ruby_18
data/gemfiles/Gemfile-3.2 CHANGED
@@ -4,3 +4,4 @@ gemspec :path => File.expand_path('../..', __FILE__)
4
4
  gem "activerecord", "~> 3.2.0"
5
5
  gem "activesupport", "~> 3.2.0"
6
6
  gem "railties", "~> 3.2.0"
7
+ gem "mime-types", "~> 1.25.1", :platforms => :ruby_18
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec :path => File.expand_path('../..', __FILE__)
4
+ gem "activerecord", "~> 4.0.0"
5
+ gem "activesupport", "~> 4.0.0"
6
+ gem "railties", "~> 4.0.0"
@@ -1,7 +1,18 @@
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 :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
- 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
+ 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
- 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
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