acts-as-messageable 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source "http://rubygems.org"
3
3
  gem "activerecord", ">= 3.0.0"
4
4
  gem "activesupport", ">= 3.0.0"
5
5
  gem "ancestry", "~> 1.2.4"
6
+ gem "railties", ">= 3.0.0"
6
7
 
7
8
  group :development do
8
9
  gem "rspec", "~> 2.6.0"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -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.1"
8
+ s.version = "0.4.2"
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 = "2011-11-16"
12
+ s.date = "2011-11-19"
13
13
  s.email = "piotr.nielacny@gmail.com"
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "lib/acts-as-messageable.rb",
27
27
  "lib/acts-as-messageable/acts-as-messageable.rb",
28
28
  "lib/acts-as-messageable/message.rb",
29
+ "lib/acts-as-messageable/model.rb",
29
30
  "lib/acts-as-messageable/relation.rb",
30
31
  "lib/generators/acts-as-messageable/migration/migration_generator.rb",
31
32
  "lib/generators/acts-as-messageable/migration/templates/migration.rb",
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
49
50
  s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
50
51
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
51
52
  s.add_runtime_dependency(%q<ancestry>, ["~> 1.2.4"])
53
+ s.add_runtime_dependency(%q<railties>, [">= 3.0.0"])
52
54
  s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
53
55
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
54
56
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -56,6 +58,7 @@ Gem::Specification.new do |s|
56
58
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
57
59
  s.add_dependency(%q<activesupport>, [">= 3.0.0"])
58
60
  s.add_dependency(%q<ancestry>, ["~> 1.2.4"])
61
+ s.add_dependency(%q<railties>, [">= 3.0.0"])
59
62
  s.add_dependency(%q<rspec>, ["~> 2.6.0"])
60
63
  s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
61
64
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -64,6 +67,7 @@ Gem::Specification.new do |s|
64
67
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
65
68
  s.add_dependency(%q<activesupport>, [">= 3.0.0"])
66
69
  s.add_dependency(%q<ancestry>, ["~> 1.2.4"])
70
+ s.add_dependency(%q<railties>, [">= 3.0.0"])
67
71
  s.add_dependency(%q<rspec>, ["~> 2.6.0"])
68
72
  s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
69
73
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -1,16 +1,7 @@
1
- require "active_record"
2
- require "active_support/core_ext"
3
- require "acts-as-messageable/acts-as-messageable"
4
-
5
1
  module ActsAsMessageable
6
- autoload :Message, "acts-as-messageable/message"
7
- autoload :Relation, "acts-as-messageable/relation"
8
- end
9
-
10
- if defined?(ActiveRecord::Base)
11
- ActiveRecord::Base.send :include, ActsAsMessageable::Model
2
+ autoload :Model, 'acts-as-messageable/model'
3
+ autoload :Message, 'acts-as-messageable/message'
4
+ autoload :Relation, 'acts-as-messageable/relation'
12
5
  end
13
6
 
14
- if defined?(ActiveRecord::Relation)
15
- ActiveRecord::Relation.send :include, ActsAsMessageable::Relation
16
- end
7
+ require 'acts-as-messageable/acts-as-messageable'
@@ -1,122 +1,14 @@
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 :class_name
10
-
11
- # Method make ActiveRecord::Base object messageable
12
- # @param [Symbol] :table_name - table name for messages
13
- def acts_as_messageable(options = {})
14
- has_many :received_messages_relation,
15
- :as => :received_messageable,
16
- :class_name => options[:class_name] || "ActsAsMessageable::Message",
17
- :dependent => :nullify
18
- has_many :sent_messages_relation,
19
- :as => :sent_messageable,
20
- :class_name => options[:class_name] || "ActsAsMessageable::Message",
21
- :dependent => :nullify
22
-
23
- self.class_name = (options[:class_name] || "ActsAsMessageable::Message").constantize
24
- self.class_name.set_table_name(options[:table_name] || "messages")
25
-
26
- if options[:required].is_a? Symbol
27
- self.class_name.required = [options[:required]]
28
- elsif options[:required].is_a? Array
29
- self.class_name.required = options[:required]
30
- else
31
- self.class_name.required = [:topic, :body]
32
- end
33
-
34
- self.class_name.validates_presence_of self.class_name.required
35
- include ActsAsMessageable::Model::InstanceMethods
36
- end
1
+ require 'active_record/railtie'
2
+ require 'active_support/core_ext'
37
3
 
4
+ module ActsAsMessageable
5
+ class Railtie < Rails::Railtie
6
+ if defined?(ActiveRecord::Base)
7
+ ActiveRecord::Base.send :include, ActsAsMessageable::Model
38
8
  end
39
9
 
40
- module InstanceMethods
41
- # Get all messages connected with user
42
- # @return [ActiveRecord::Relation] all messages connected with user
43
- def messages(trash = false)
44
- result = self.class.class_name.connected_with(self, trash)
45
- result.relation_context = self
46
-
47
- result
48
- end
49
-
50
- def received_messages
51
- result = received_messages_relation.scoped.where(:recipient_delete => false)
52
- result.relation_context = self
53
-
54
- result
55
- end
56
-
57
- def sent_messages
58
- result = sent_messages_relation.scoped.where(:sender_delete => false)
59
- result.relation_context = self
60
-
61
- result
62
- end
63
-
64
- def deleted_messages
65
- messages true
66
- end
67
-
68
- # Method sens message to another user
69
- # @param [ActiveRecord::Base] to
70
- # @param [String] topic
71
- # @param [String] body
72
- #
73
- # @return [ActsAsMessageable::Message] the message object
74
- def send_message(to, *args)
75
- case args.first
76
- when String
77
- message_attributes = {}
78
- self.class.class_name.required.each_with_index do |attribute, index|
79
- message_attributes[attribute] = args[index]
80
- end
81
- when Hash
82
- message_attributes = args.first
83
- end
84
-
85
- message = self.class.class_name.create! message_attributes
86
-
87
- self.sent_messages_relation << message
88
- to.received_messages_relation << message
89
-
90
- message
91
- end
92
-
93
- def reply_to(message, *args)
94
- reply_message = send_message(message.from, *args)
95
- reply_message.parent = message
96
- reply_message.save
97
-
98
- reply_message
99
- end
100
-
101
- def delete_message(message)
102
- current_user = self
103
-
104
- if message.received_messageable == current_user
105
- unless message.recipient_delete
106
- message.update_attributes!(:recipient_delete => true)
107
- else
108
- message.update_attributes!(:recipient_permanent_delete => true)
109
- end
110
- elsif message.sent_messageable == current_user
111
- unless message.sender_delete
112
- message.update_attributes!(:sender_delete => true)
113
- else
114
- message.update_attributes!(:sender_permanent_delete => true)
115
- end
116
- end
117
- end
118
-
10
+ if defined?(ActiveRecord::Relation)
11
+ ActiveRecord::Relation.send :include, ActsAsMessageable::Relation
119
12
  end
120
-
121
13
  end
122
14
  end
@@ -0,0 +1,122 @@
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
+ def acts_as_messageable(options = {})
14
+ has_many :received_messages_relation,
15
+ :as => :received_messageable,
16
+ :class_name => options[:class_name] || "ActsAsMessageable::Message",
17
+ :dependent => :nullify
18
+ has_many :sent_messages_relation,
19
+ :as => :sent_messageable,
20
+ :class_name => options[:class_name] || "ActsAsMessageable::Message",
21
+ :dependent => :nullify
22
+
23
+ self.messages_class_name = (options[:class_name] || "ActsAsMessageable::Message").constantize
24
+ self.messages_class_name.set_table_name(options[:table_name] || "messages")
25
+
26
+ if options[:required].is_a? Symbol
27
+ self.messages_class_name.required = [options[:required]]
28
+ elsif options[:required].is_a? Array
29
+ self.messages_class_name.required = options[:required]
30
+ else
31
+ self.messages_class_name.required = [:topic, :body]
32
+ end
33
+
34
+ self.messages_class_name.validates_presence_of self.messages_class_name.required
35
+ include ActsAsMessageable::Model::InstanceMethods
36
+ end
37
+
38
+ end
39
+
40
+ module InstanceMethods
41
+ # Get all messages connected with user
42
+ # @return [ActiveRecord::Relation] all messages connected with user
43
+ def messages(trash = false)
44
+ result = self.class.messages_class_name.connected_with(self, trash)
45
+ result.relation_context = self
46
+
47
+ result
48
+ end
49
+
50
+ def received_messages
51
+ result = received_messages_relation.scoped.where(:recipient_delete => false)
52
+ result.relation_context = self
53
+
54
+ result
55
+ end
56
+
57
+ def sent_messages
58
+ result = sent_messages_relation.scoped.where(:sender_delete => false)
59
+ result.relation_context = self
60
+
61
+ result
62
+ end
63
+
64
+ def deleted_messages
65
+ messages true
66
+ end
67
+
68
+ # Method sens message to another user
69
+ # @param [ActiveRecord::Base] to
70
+ # @param [String] topic
71
+ # @param [String] body
72
+ #
73
+ # @return [ActsAsMessageable::Message] the message object
74
+ def send_message(to, *args)
75
+ case args.first
76
+ when String
77
+ message_attributes = {}
78
+ self.class.messages_class_name.required.each_with_index do |attribute, index|
79
+ message_attributes[attribute] = args[index]
80
+ end
81
+ when Hash
82
+ message_attributes = args.first
83
+ end
84
+
85
+ message = self.class.messages_class_name.create! message_attributes
86
+
87
+ self.sent_messages_relation << message
88
+ to.received_messages_relation << message
89
+
90
+ message
91
+ end
92
+
93
+ def reply_to(message, *args)
94
+ reply_message = send_message(message.from, *args)
95
+ reply_message.parent = message
96
+ reply_message.save
97
+
98
+ reply_message
99
+ end
100
+
101
+ def delete_message(message)
102
+ current_user = self
103
+
104
+ if message.received_messageable == current_user
105
+ unless message.recipient_delete
106
+ message.update_attributes!(:recipient_delete => true)
107
+ else
108
+ message.update_attributes!(:recipient_permanent_delete => true)
109
+ end
110
+ elsif message.sent_messageable == current_user
111
+ unless message.sender_delete
112
+ message.update_attributes!(:sender_delete => true)
113
+ else
114
+ message.update_attributes!(:sender_permanent_delete => true)
115
+ end
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end
data/spec/spec_helper.rb CHANGED
@@ -21,7 +21,7 @@ RSpec.configure do |config|
21
21
  end
22
22
 
23
23
  config.after(:each) do
24
- User.class_name.destroy_all
24
+ User.messages_class_name.destroy_all
25
25
  end
26
26
  end
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts-as-messageable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-16 00:00:00.000000000Z
12
+ date: 2011-11-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &69822268751920 !ruby/object:Gem::Requirement
16
+ requirement: &70019859513520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *69822268751920
24
+ version_requirements: *70019859513520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &69822268749940 !ruby/object:Gem::Requirement
27
+ requirement: &70019859510340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *69822268749940
35
+ version_requirements: *70019859510340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ancestry
38
- requirement: &69822268747920 !ruby/object:Gem::Requirement
38
+ requirement: &70019859508340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: 1.2.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *69822268747920
46
+ version_requirements: *70019859508340
47
+ - !ruby/object:Gem::Dependency
48
+ name: railties
49
+ requirement: &70019859506340 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70019859506340
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rspec
49
- requirement: &69822268746840 !ruby/object:Gem::Requirement
60
+ requirement: &70019859505060 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 2.6.0
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *69822268746840
68
+ version_requirements: *70019859505060
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: jeweler
60
- requirement: &69822268745520 !ruby/object:Gem::Requirement
71
+ requirement: &70019859503360 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: 1.6.0
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *69822268745520
79
+ version_requirements: *70019859503360
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: sqlite3-ruby
71
- requirement: &69822268743820 !ruby/object:Gem::Requirement
82
+ requirement: &70019859501840 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *69822268743820
90
+ version_requirements: *70019859501840
80
91
  description:
81
92
  email: piotr.nielacny@gmail.com
82
93
  executables: []
@@ -95,6 +106,7 @@ files:
95
106
  - lib/acts-as-messageable.rb
96
107
  - lib/acts-as-messageable/acts-as-messageable.rb
97
108
  - lib/acts-as-messageable/message.rb
109
+ - lib/acts-as-messageable/model.rb
98
110
  - lib/acts-as-messageable/relation.rb
99
111
  - lib/generators/acts-as-messageable/migration/migration_generator.rb
100
112
  - lib/generators/acts-as-messageable/migration/templates/migration.rb
@@ -119,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
131
  version: '0'
120
132
  segments:
121
133
  - 0
122
- hash: -3869198984656405401
134
+ hash: -2282999855817855460
123
135
  required_rubygems_version: !ruby/object:Gem::Requirement
124
136
  none: false
125
137
  requirements: