acts-as-messageable 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - rbx
5
+ - ree
6
+ - jruby
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "activerecord", "~> 3.0.0"
4
+ gem "ancestry", "~> 1.2.4"
4
5
 
5
6
  group :development do
6
7
  gem "rspec", "~> 2.3.0"
data/README.rdoc CHANGED
@@ -2,55 +2,88 @@
2
2
  = ActsAsMessageable
3
3
  The Acts As Messageable allows communication between the models.
4
4
 
5
+ http://travis-ci.org/LTe/acts-as-messageable.png
6
+
7
+
5
8
  == Usage
6
9
  To use it, add it to your Gemfile:
7
10
 
8
- gem 'acts-as-messageable'
11
+ gem 'acts-as-messageable'
9
12
 
10
13
  == Post instalation
11
14
 
12
- rails g acts-as-messageable:migration [table_name] # default 'messages'
13
- rake db:migrate
15
+ rails g acts-as-messageable:migration table_name # default 'messages'
16
+ rake db:migrate
14
17
 
15
18
  == Usage
16
19
 
17
- class User < ActiveRecord::Base
18
- acts_as_messageable :table_name => "table_with_messages", # default 'messages'
19
- :required => :body # default [:topic, :body]
20
+ class User < ActiveRecord::Base
21
+ acts_as_messageable :table_name => "table_with_messages", # default 'messages'
22
+ :required => :body # default [:topic, :body]
20
23
  end
21
24
 
22
25
  == Send message
23
26
 
24
- @alice = User.first
25
- @bob = User.last
27
+ @alice = User.first
28
+ @bob = User.last
26
29
 
27
- @alice.send_message(@bob, "Message topic", "Hi bob!")
28
- @bob.send_message(@alice, "Re: Message topic", "Hi alice!")
30
+ @alice.send_message(@bob, "Message topic", "Hi bob!")
31
+ @bob.send_message(@alice, "Re: Message topic", "Hi alice!")
29
32
 
30
- == New API
33
+ === With hash
34
+
35
+ @alice.send_message(@bob, { :body => "Hash body", :topic => "Hash topic" })
36
+
37
+ === Custom required
38
+
39
+ In User model
40
+ class User < ActiveRecord::Base
41
+ acts_as_messageable :required => :body
42
+ end
43
+
44
+ @alice.send_message(@bob, { :body => "Hash body" })
45
+ or
46
+ @alice.send_message(@bob, "body")
47
+
48
+ ==== Required sequence
31
49
 
32
- @alice.messages -- all messages connected with @alice (inbox/outbox)
33
- @alice.received_messages -- @alice inbox
34
- @alice.sent_messages -- @alice outbox
35
50
 
36
- @alice.deleted_messages -- all messages connected with @alice (trash)
51
+ class User < ActiveRecord::Base
52
+ acts_as_messageable :required => [:body, :topic]
53
+ end
37
54
 
38
- @alice.messages.are_from(@bob) -- all message form @bob
39
- @alice.messages.are_to(@bob) -- all message to @bob
40
- @alice.messages.with_id(@id_of_message) -- message with id id_of_message
41
- @alice.messages.readed -- all readed @alice messages
42
- @alice.messages.unaded -- all unreaded @alice messages
55
+ @alice.send_message(@bob, "body", "topic")
43
56
 
44
- You can use multiple filters at the same time
57
+ == Conversation
58
+ @message = @alice.send_message(@bob, "Hello bob!", "How are you?")
59
+ @reply_message = @bob.reply_to(@message, "Re: Hello bob!", "I'm fine!")
45
60
 
46
- @alice.messages.are_from(@bob).are_to(@alice).readed -- all message from @bob to @alice and readed
47
- @alice.deleted_message.are_from(@bob) -- all deleted messages from @bob
61
+ @message.conversation #=> [@message, @reply_message]
62
+ @reply_message.conversation #=> [@message, @reply_message]
63
+
64
+ == New API
65
+ @alice.messages # all messages connected with @alice (inbox/outbox)
66
+ @alice.received_messages # @alice inbox
67
+ @alice.sent_messages # @alice outbox
68
+
69
+ @alice.deleted_messages # all messages connected with @alice (trash)
70
+
71
+ @alice.messages.are_from(@bob) # all message form @bob
72
+ @alice.messages.are_to(@bob) # all message to @bob
73
+ @alice.messages.with_id(@id_of_message) # message with id id_of_message
74
+ @alice.messages.readed # all readed @alice messages
75
+ @alice.messages.unaded # all unreaded @alice messages
76
+
77
+ You can use multiple filters at the same time
78
+
79
+ @alice.messages.are_from(@bob).are_to(@alice).readed # all message from @bob to @alice and readed
80
+ @alice.deleted_messages.are_from(@bob) # all deleted messages from @bob
48
81
 
49
82
  === Delete message
50
83
 
51
- @alice.messages.process do |message|
52
- message.delete
53
- end
84
+ @alice.messages.process do |message|
85
+ message.delete
86
+ end
54
87
 
55
88
 
56
89
  Copyright © 2011 Piotr Niełacny (http://ruby-blog.pl), released under the MIT license
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
16
16
  gem.name = "acts-as-messageable"
17
17
  gem.email = "piotr.nielacny@gmail.com"
18
18
  gem.summary = "Make user messageable!;-)"
19
- gem.homepage = "http://github.com/LTe/acts_as_messageable"
19
+ gem.homepage = "http://github.com/LTe/acts-as-messageable"
20
20
  gem.authors = ["Piotr Nielacny"]
21
21
  # dependencies defined in Gemfile
22
22
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -5,16 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts-as-messageable}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
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 = %q{2011-05-05}
12
+ s.date = %q{2011-05-31}
13
13
  s.email = %q{piotr.nielacny@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
16
16
  ]
17
17
  s.files = [
18
+ ".travis.yml",
18
19
  "Gemfile",
19
20
  "MIT-LICENSE",
20
21
  "README.rdoc",
@@ -32,7 +33,7 @@ Gem::Specification.new do |s|
32
33
  "spec/spec_helper.rb",
33
34
  "spec/support/user.rb"
34
35
  ]
35
- s.homepage = %q{http://github.com/LTe/acts_as_messageable}
36
+ s.homepage = %q{http://github.com/LTe/acts-as-messageable}
36
37
  s.require_paths = ["lib"]
37
38
  s.rubygems_version = %q{1.6.2}
38
39
  s.summary = %q{Make user messageable!;-)}
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
 
43
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
45
  s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
46
+ s.add_runtime_dependency(%q<ancestry>, ["~> 1.2.4"])
45
47
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
46
48
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
@@ -49,6 +51,7 @@ Gem::Specification.new do |s|
49
51
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
50
52
  else
51
53
  s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
54
+ s.add_dependency(%q<ancestry>, ["~> 1.2.4"])
52
55
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
53
56
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
57
  s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
@@ -57,6 +60,7 @@ Gem::Specification.new do |s|
57
60
  end
58
61
  else
59
62
  s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
63
+ s.add_dependency(%q<ancestry>, ["~> 1.2.4"])
60
64
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
61
65
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
66
  s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
@@ -1,6 +1,6 @@
1
1
  require "active_record"
2
- require 'acts-as-messageable/acts-as-messageable'
3
2
  require 'acts-as-messageable/message'
3
+ require 'acts-as-messageable/acts-as-messageable'
4
4
  require 'acts-as-messageable/relation'
5
5
 
6
6
  if defined?(ActiveRecord::Base)
@@ -9,7 +9,6 @@ module ActsAsMessageable
9
9
  # Method make ActiveRecord::Base object messageable
10
10
  # @param [Symbol] :table_name - table name for messages
11
11
  def acts_as_messageable(options = {})
12
- class_eval do
13
12
  has_many :received_messages,
14
13
  :as => :received_messageable,
15
14
  :class_name => "ActsAsMessageable::Message",
@@ -18,15 +17,13 @@ module ActsAsMessageable
18
17
  :as => :sent_messageable,
19
18
  :class_name => "ActsAsMessageable::Message",
20
19
  :dependent => :nullify
21
- end
22
-
23
- table_name = %q{set_table_name "#{options[:table_name] || "messages"}"}
24
- ActsAsMessageable::Message.class_eval(table_name)
25
20
 
26
- validation = %q{validates_presence_of options[:required] || [:topic ,:body]}
27
- ActsAsMessageable::Message.class_eval(validation)
28
21
 
29
- include ActsAsMessageable::Model::InstanceMethods
22
+ ActsAsMessageable::Message.set_table_name(options[:table_name] || "messages")
23
+ ActsAsMessageable::Message.validates_presence_of(options[:required] || [:topic ,:body])
24
+ ActsAsMessageable::Message.required = options[:required] || [:topic, :body]
25
+
26
+ include ActsAsMessageable::Model::InstanceMethods
30
27
  end
31
28
 
32
29
  end
@@ -51,11 +48,31 @@ module ActsAsMessageable
51
48
  # @param [String] body
52
49
  #
53
50
  # @return [ActsAsMessageable::Message] the message object
54
- def send_message(to, topic, body)
55
- @message = ActsAsMessageable::Message.create!(:topic => topic, :body => body)
51
+ def send_message(to, *args)
52
+ case args.first
53
+ when String
54
+ message_attributes = {}
55
+ ActsAsMessageable::Message.required.each_with_index do |attribute, index|
56
+ message_attributes[attribute] = args[index]
57
+ end
58
+ when Hash
59
+ message_attributes = args.first
60
+ end
61
+
62
+ message = ActsAsMessageable::Message.create! message_attributes
63
+
64
+ self.sent_messages << message
65
+ to.received_messages << message
66
+
67
+ message
68
+ end
69
+
70
+ def reply_to(message, topic, body)
71
+ reply_message = send_message(self, topic, body)
72
+ reply_message.parent = message
73
+ reply_message.save
56
74
 
57
- self.sent_messages << @message
58
- to.received_messages << @message
75
+ reply_message
59
76
  end
60
77
 
61
78
  def delete_message(message)
@@ -1,5 +1,9 @@
1
+ require 'ancestry'
2
+
1
3
  module ActsAsMessageable
2
4
  class Message < ::ActiveRecord::Base
5
+ has_ancestry
6
+
3
7
  belongs_to :received_messageable, :polymorphic => true
4
8
  belongs_to :sent_messageable, :polymorphic => true
5
9
 
@@ -13,7 +17,9 @@ module ActsAsMessageable
13
17
  :recipient_delete,
14
18
  :sender_delete
15
19
 
16
- attr_accessor :context, :removed
20
+ attr_accessor :removed
21
+ cattr_accessor :required
22
+
17
23
 
18
24
  # Sample documentation for scope
19
25
  scope :are_from, lambda { |*args| where("sent_messageable_id = :sender", :sender => args.first) }
@@ -66,6 +72,10 @@ module ActsAsMessageable
66
72
  received_messageable
67
73
  end
68
74
 
75
+ def conversation
76
+ root.subtree
77
+ end
78
+
69
79
  def delete
70
80
  self.removed = true
71
81
  end
@@ -6,12 +6,15 @@ class CreateMessagesTable < ActiveRecord::Migration
6
6
  t.references :received_messageable, :polymorphic => true
7
7
  t.references :sent_messageable, :polymorphic => true
8
8
  t.boolean :opened, :default => false
9
- t.boolean :opened, :default => false
10
9
  t.boolean :recipient_delete, :default => false
11
10
  t.boolean :sender_delete, :default => false
11
+
12
+ # ancestry
13
+ t.string :ancestry
12
14
  end
13
15
 
14
16
  add_index :<%= table_name %>, [:sent_messageable_id, :received_messageable_id], :name => "acts_as_messageable_ids"
17
+ add_index :<%= table_name %>, :ancestry
15
18
  end
16
19
 
17
20
  def self.down
@@ -18,7 +18,7 @@ describe "ActsAsMessageable" do
18
18
  end
19
19
 
20
20
  it "bob should have one open message from alice" do
21
- @alice.send_message(@bob, "Topic", "Message body")
21
+ @message = @alice.send_message(@bob, "Topic", "Message body")
22
22
  @bob.messages.are_from(@alice).process do |m|
23
23
  m.open
24
24
  end
@@ -48,8 +48,8 @@ describe "ActsAsMessageable" do
48
48
  end
49
49
 
50
50
  it "should be in database message with id ..." do
51
- message_id = @bob.send_message(@alice, "Topic", "Message body").first.id
52
- @bob.messages.id(message_id).count.should == 1
51
+ message_id = @bob.send_message(@alice, "Topic", "Message body").id
52
+ @bob.messages.with_id(message_id).count.should == 1
53
53
  end
54
54
 
55
55
  it "message should have proper topic" do
@@ -58,4 +58,32 @@ describe "ActsAsMessageable" do
58
58
  @bob.messages.first.topic == "Topic"
59
59
  @bob.messages.first.body == "Message body"
60
60
  end
61
+
62
+ it "bob send message to alice, and alice reply to bob message and show proper tree" do
63
+ @message = @bob.send_message(@alice, "Topic", "Body")
64
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
65
+
66
+ @reply_message.conversation.size.should == 2
67
+ @reply_message.conversation.first.topic.should == "Topic"
68
+ @reply_message.conversation.last.topic.should == "Re: Topic"
69
+ end
70
+
71
+ it "bob send message to alice, alice answer, and bob answer for alice answer" do
72
+ @message = @bob.send_message(@alice, "Topic", "Body")
73
+ @reply_message = @alice.reply_to(@message, "Re: Topic", "Body")
74
+ @reply_reply_message = @bob.reply_to(@reply_message, "Re: Re: Topic", "Body")
75
+
76
+ [@message, @reply_message, @reply_reply_message].each do |m|
77
+ m.conversation.size.should == 3
78
+ end
79
+
80
+ @message.conversation.last.should == @reply_reply_message
81
+ @reply_reply_message.conversation.last.should == @reply_reply_message
82
+ end
83
+
84
+ it "send message with hash" do
85
+ @message = @bob.send_message(@alice, {:body => "Body", :topic => "Topic"})
86
+ @message.topic.should == "Topic"
87
+ @message.body.should == "Body"
88
+ end
61
89
  end
data/spec/spec_helper.rb CHANGED
@@ -33,13 +33,13 @@ def create_database
33
33
  t.references :received_messageable, :polymorphic => true
34
34
  t.references :sent_messageable, :polymorphic => true
35
35
  t.boolean :opened, :default => false
36
- t.boolean :opened, :default => false
37
36
  t.boolean :recipient_delete, :default => false
38
37
  t.boolean :sender_delete, :default => false
38
+ t.string :ancestry
39
39
  end
40
40
 
41
41
  create_table :users do |t|
42
- t.string :email
42
+ t.string :email
43
43
  end
44
44
  end
45
45
  end
data/spec/support/user.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'ancestry'
2
+
1
3
  class User < ActiveRecord::Base
2
4
  acts_as_messageable
3
- end
5
+ end
6
+
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.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-05 00:00:00.000000000 +02:00
12
+ date: 2011-05-31 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &14917040 !ruby/object:Gem::Requirement
17
+ requirement: &5338400 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,21 @@ dependencies:
22
22
  version: 3.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *14917040
25
+ version_requirements: *5338400
26
+ - !ruby/object:Gem::Dependency
27
+ name: ancestry
28
+ requirement: &5337340 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *5337340
26
37
  - !ruby/object:Gem::Dependency
27
38
  name: rspec
28
- requirement: &14916280 !ruby/object:Gem::Requirement
39
+ requirement: &5335900 !ruby/object:Gem::Requirement
29
40
  none: false
30
41
  requirements:
31
42
  - - ~>
@@ -33,10 +44,10 @@ dependencies:
33
44
  version: 2.3.0
34
45
  type: :development
35
46
  prerelease: false
36
- version_requirements: *14916280
47
+ version_requirements: *5335900
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: bundler
39
- requirement: &14914600 !ruby/object:Gem::Requirement
50
+ requirement: &5334660 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ~>
@@ -44,10 +55,10 @@ dependencies:
44
55
  version: 1.0.0
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *14914600
58
+ version_requirements: *5334660
48
59
  - !ruby/object:Gem::Dependency
49
60
  name: jeweler
50
- requirement: &14912820 !ruby/object:Gem::Requirement
61
+ requirement: &5333300 !ruby/object:Gem::Requirement
51
62
  none: false
52
63
  requirements:
53
64
  - - ~>
@@ -55,10 +66,10 @@ dependencies:
55
66
  version: 1.6.0
56
67
  type: :development
57
68
  prerelease: false
58
- version_requirements: *14912820
69
+ version_requirements: *5333300
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: rcov
61
- requirement: &14911500 !ruby/object:Gem::Requirement
72
+ requirement: &5331960 !ruby/object:Gem::Requirement
62
73
  none: false
63
74
  requirements:
64
75
  - - ! '>='
@@ -66,10 +77,10 @@ dependencies:
66
77
  version: '0'
67
78
  type: :development
68
79
  prerelease: false
69
- version_requirements: *14911500
80
+ version_requirements: *5331960
70
81
  - !ruby/object:Gem::Dependency
71
82
  name: sqlite3-ruby
72
- requirement: &14909060 !ruby/object:Gem::Requirement
83
+ requirement: &5251020 !ruby/object:Gem::Requirement
73
84
  none: false
74
85
  requirements:
75
86
  - - ! '>='
@@ -77,7 +88,7 @@ dependencies:
77
88
  version: '0'
78
89
  type: :development
79
90
  prerelease: false
80
- version_requirements: *14909060
91
+ version_requirements: *5251020
81
92
  description:
82
93
  email: piotr.nielacny@gmail.com
83
94
  executables: []
@@ -85,6 +96,7 @@ extensions: []
85
96
  extra_rdoc_files:
86
97
  - README.rdoc
87
98
  files:
99
+ - .travis.yml
88
100
  - Gemfile
89
101
  - MIT-LICENSE
90
102
  - README.rdoc
@@ -102,7 +114,7 @@ files:
102
114
  - spec/spec_helper.rb
103
115
  - spec/support/user.rb
104
116
  has_rdoc: true
105
- homepage: http://github.com/LTe/acts_as_messageable
117
+ homepage: http://github.com/LTe/acts-as-messageable
106
118
  licenses: []
107
119
  post_install_message:
108
120
  rdoc_options: []
@@ -116,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
128
  version: '0'
117
129
  segments:
118
130
  - 0
119
- hash: 4493248086086979425
131
+ hash: 4035680522358941194
120
132
  required_rubygems_version: !ruby/object:Gem::Requirement
121
133
  none: false
122
134
  requirements: