acts-as-messageable 0.0.1 → 0.0.2
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/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/VERSION +1 -1
- data/lib/acts_as_messageable/acts-as-messageable.rb +39 -24
- data/lib/acts_as_messageable/message.rb +15 -5
- data/lib/generators/acts_as_messageable/migration/migration_generator.rb +0 -2
- data/lib/generators/acts_as_messageable/migration/templates/migration.rb +3 -4
- metadata +5 -4
- data/README +0 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Piotr Niełacny Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= ActsAsMessageable
|
2
|
+
The Acts As Messageable allows communication between the models.
|
3
|
+
|
4
|
+
== Usage
|
5
|
+
To use it, add it to your Gemfile:
|
6
|
+
|
7
|
+
gem 'acts-as-messageable'
|
8
|
+
|
9
|
+
== Post instalation
|
10
|
+
|
11
|
+
1. rails g acts_as_messageable:migration
|
12
|
+
2. rake db:migrate
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
acts_as_messageable
|
18
|
+
end
|
19
|
+
|
20
|
+
@user1 = User.first
|
21
|
+
@user2 = User.last
|
22
|
+
|
23
|
+
# user1 send message to user2
|
24
|
+
@user1.send_msg(@user2, "Message to user2", "Hi user 2!;-)")
|
25
|
+
@user2.send_msg(@user1, "Re: Message to user2", "Hi there!:)")
|
26
|
+
|
27
|
+
# Show inbox
|
28
|
+
@user1.recv
|
29
|
+
# Show outbox
|
30
|
+
@user1.sent
|
31
|
+
|
32
|
+
# Search message
|
33
|
+
@user1.msg # all messages (in and out)
|
34
|
+
@user1.msg(:from => @user2) # all messages from @user2
|
35
|
+
@user1.msg(:to => @user2) # all messages to @user2
|
36
|
+
@user1.msg(:id => 2) # message where message.id = 2
|
37
|
+
|
38
|
+
Copyright © 2010 Piotr Niełacny (http://ruby-blog.pl), released under the MIT license
|
39
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -3,45 +3,60 @@ module ActsAsMessageable
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
base.extend ClassMethods
|
6
|
-
base.send :include, InstanceMethods
|
7
6
|
end
|
8
7
|
|
9
8
|
module ClassMethods
|
10
9
|
def acts_as_messageable
|
11
10
|
class_eval do
|
12
|
-
has_many :
|
11
|
+
has_many :received_messages, :as => :received_messageable, :class_name => "ActsAsMessageable::Message"
|
12
|
+
has_many :sent_messages, :as => :sent_messageable, :class_name => "ActsAsMessageable::Message"
|
13
13
|
end
|
14
|
-
end
|
15
14
|
|
16
|
-
|
15
|
+
include InstanceMethods
|
16
|
+
end
|
17
|
+
|
17
18
|
end
|
18
19
|
|
19
20
|
module InstanceMethods
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
def msg(args = {})
|
22
|
+
|
23
|
+
all = self.recv + self.sent
|
24
|
+
|
25
|
+
if args[:from]
|
26
|
+
all.reject! do |m|
|
27
|
+
m.sent_messageable_id != args[:from].id
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if args[:to]
|
32
|
+
all.reject! do |m|
|
33
|
+
m.received_messageable_id != args[:to].id
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
if args[:id] != nil
|
38
|
+
all.reject! do |m|
|
39
|
+
m.id != args[:id]
|
40
|
+
end
|
31
41
|
end
|
42
|
+
|
43
|
+
all
|
32
44
|
end
|
33
45
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
@message.topic, @message.body = topic, body
|
46
|
+
def recv
|
47
|
+
self.received_messages
|
48
|
+
end
|
38
49
|
|
39
|
-
|
40
|
-
|
50
|
+
def sent
|
51
|
+
self.sent_messages
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_msg(to, topic, body)
|
55
|
+
@message = ActsAsMessageable::Message.create
|
56
|
+
@message.topic, @message.body = topic, body
|
41
57
|
|
42
|
-
self.
|
43
|
-
to.
|
44
|
-
@message.save
|
58
|
+
self.sent_messages << @message
|
59
|
+
to.received_messages << @message
|
45
60
|
end
|
46
61
|
|
47
62
|
end
|
@@ -1,12 +1,22 @@
|
|
1
1
|
module ActsAsMessageable
|
2
2
|
class Message < ::ActiveRecord::Base
|
3
|
-
belongs_to :
|
3
|
+
belongs_to :received_messageable, :polymorphic => true
|
4
|
+
belongs_to :sent_messageable, :polymorphic => true
|
4
5
|
|
5
6
|
attr_accessible :topic,
|
6
7
|
:body,
|
7
|
-
:
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
8
|
+
:received_messageable_type,
|
9
|
+
:received_messageable_id,
|
10
|
+
:sent_messageable_type,
|
11
|
+
:sent_messageable_id,
|
12
|
+
|
13
|
+
def from
|
14
|
+
"#{self.sent_messageable_type}".constantize.find(self.sent_messageable_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to
|
18
|
+
"#{self.received_messageable_type}".constantize.find(self.received_messageable_id)
|
19
|
+
end
|
20
|
+
|
11
21
|
end
|
12
22
|
end
|
@@ -4,8 +4,6 @@ module ActsAsMessageable
|
|
4
4
|
class MigrationGenerator < Rails::Generators::Base
|
5
5
|
include Rails::Generators::Migration
|
6
6
|
|
7
|
-
desc "Generates migration for Tag and Tagging models"
|
8
|
-
|
9
7
|
def self.source_root
|
10
8
|
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
11
9
|
end
|
@@ -3,12 +3,11 @@ class CreateMessagesTable < ActiveRecord::Migration
|
|
3
3
|
create_table :messages do |t|
|
4
4
|
t.string :topic
|
5
5
|
t.string :body
|
6
|
-
t.references :
|
7
|
-
t.
|
8
|
-
t.integer :to
|
6
|
+
t.references :received_messageable, :polymorphic => true
|
7
|
+
t.references :sent_messageable, :polymorphic => true
|
9
8
|
end
|
10
9
|
|
11
|
-
add_index :messages, [:
|
10
|
+
add_index :messages, [:sent_messageable_id, :received_messageable_id]
|
12
11
|
end
|
13
12
|
|
14
13
|
def self.down
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Piotr Nielacny
|
@@ -25,9 +25,10 @@ executables: []
|
|
25
25
|
extensions: []
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
|
-
- README
|
28
|
+
- README.rdoc
|
29
29
|
files:
|
30
|
-
-
|
30
|
+
- MIT-LICENSE
|
31
|
+
- README.rdoc
|
31
32
|
- Rakefile
|
32
33
|
- VERSION
|
33
34
|
- lib/acts-as-messageable.rb
|
data/README
DELETED
File without changes
|