mailboxer-without-notification 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +18 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +11 -0
  4. data/.yardopts +2 -0
  5. data/Appraisals +19 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +298 -0
  9. data/Rakefile +11 -0
  10. data/app/mailers/message_mailer.rb +37 -0
  11. data/app/models/conversation.rb +179 -0
  12. data/app/models/mailbox.rb +122 -0
  13. data/app/models/message.rb +218 -0
  14. data/app/models/receipt.rb +176 -0
  15. data/app/uploaders/attachment_uploader.rb +3 -0
  16. data/app/views/message_mailer/new_message_email.html.erb +20 -0
  17. data/app/views/message_mailer/new_message_email.text.erb +10 -0
  18. data/app/views/message_mailer/reply_message_email.html.erb +20 -0
  19. data/app/views/message_mailer/reply_message_email.text.erb +10 -0
  20. data/config/locales/en.yml +7 -0
  21. data/config/locales/fr.yml +7 -0
  22. data/db/migrate/20110511145103_create_mailboxer.rb +61 -0
  23. data/db/migrate/20110719110700_add_notified_object.rb +17 -0
  24. data/db/migrate/20110912163911_add_notification_code.rb +13 -0
  25. data/db/migrate/20111204163911_add_attachments.rb +9 -0
  26. data/db/migrate/20120813110712_rename_receipts_read.rb +9 -0
  27. data/db/migrate/20130305144212_add_global_notification_support.rb +9 -0
  28. data/db/migrate/20131003144212_change_table_notification.rb +10 -0
  29. data/db/migrate/20131003214212_change_relations.rb +6 -0
  30. data/gemfiles/rails3.0.gemfile +8 -0
  31. data/gemfiles/rails3.1.gemfile +8 -0
  32. data/gemfiles/rails3.2.gemfile +8 -0
  33. data/gemfiles/rails4.0.gemfile +8 -0
  34. data/lib/generators/mailboxer/install_generator.rb +35 -0
  35. data/lib/generators/mailboxer/templates/initializer.rb +17 -0
  36. data/lib/generators/mailboxer/views_generator.rb +9 -0
  37. data/lib/mailboxer.rb +37 -0
  38. data/lib/mailboxer/concerns/configurable_mailer.rb +13 -0
  39. data/lib/mailboxer/engine.rb +17 -0
  40. data/lib/mailboxer/models/messageable.rb +225 -0
  41. data/mailboxer.gemspec +49 -0
  42. data/spec/dummy/.gitignore +5 -0
  43. data/spec/dummy/Gemfile +33 -0
  44. data/spec/dummy/Rakefile +7 -0
  45. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  46. data/spec/dummy/app/controllers/home_controller.rb +4 -0
  47. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  48. data/spec/dummy/app/mailers/.gitkeep +0 -0
  49. data/spec/dummy/app/models/.gitkeep +0 -0
  50. data/spec/dummy/app/models/cylon.rb +7 -0
  51. data/spec/dummy/app/models/duck.rb +11 -0
  52. data/spec/dummy/app/models/user.rb +6 -0
  53. data/spec/dummy/app/views/home/index.html.haml +7 -0
  54. data/spec/dummy/app/views/layouts/application.html.haml +11 -0
  55. data/spec/dummy/config.ru +4 -0
  56. data/spec/dummy/config/application.rb +42 -0
  57. data/spec/dummy/config/boot.rb +10 -0
  58. data/spec/dummy/config/database.yml +24 -0
  59. data/spec/dummy/config/environment.rb +5 -0
  60. data/spec/dummy/config/environments/development.rb +26 -0
  61. data/spec/dummy/config/environments/production.rb +49 -0
  62. data/spec/dummy/config/environments/test.rb +33 -0
  63. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  64. data/spec/dummy/config/initializers/inflections.rb +10 -0
  65. data/spec/dummy/config/initializers/mailboxer.rb +17 -0
  66. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  67. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  68. data/spec/dummy/config/initializers/session_store.rb +8 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +58 -0
  71. data/spec/dummy/config/sunspot.yml +17 -0
  72. data/spec/dummy/db/migrate/20110228120600_create_users.rb +14 -0
  73. data/spec/dummy/db/migrate/20110306002940_create_ducks.rb +14 -0
  74. data/spec/dummy/db/migrate/20110306015107_create_cylons.rb +14 -0
  75. data/spec/dummy/db/migrate/20120305103200_create_mailboxer.rb +61 -0
  76. data/spec/dummy/db/migrate/20120305103201_add_notified_object.rb +17 -0
  77. data/spec/dummy/db/migrate/20120305103202_add_notification_code.rb +13 -0
  78. data/spec/dummy/db/migrate/20120305103203_add_attachments.rb +5 -0
  79. data/spec/dummy/db/migrate/20120813110712_rename_receipts_read.rb +9 -0
  80. data/spec/dummy/db/migrate/20130305144212_add_global_notification_support.rb +11 -0
  81. data/spec/dummy/db/migrate/20131003144212_change_table_notification.rb +10 -0
  82. data/spec/dummy/db/migrate/20131003214212_change_relations.rb +6 -0
  83. data/spec/dummy/db/schema.rb +74 -0
  84. data/spec/dummy/public/404.html +26 -0
  85. data/spec/dummy/public/422.html +26 -0
  86. data/spec/dummy/public/500.html +26 -0
  87. data/spec/dummy/public/favicon.ico +0 -0
  88. data/spec/dummy/public/index.html +239 -0
  89. data/spec/dummy/public/robots.txt +5 -0
  90. data/spec/dummy/public/uploads/testfile.txt +1 -0
  91. data/spec/dummy/script/rails +6 -0
  92. data/spec/factories/cylon.rb +10 -0
  93. data/spec/factories/duck.rb +10 -0
  94. data/spec/factories/user.rb +10 -0
  95. data/spec/integration/message_and_receipt_spec.rb +903 -0
  96. data/spec/integration/navigation_spec.rb +9 -0
  97. data/spec/mailboxer/concerns/configurable_mailer_spec.rb +27 -0
  98. data/spec/mailboxer_spec.rb +7 -0
  99. data/spec/mailers/message_mailer_spec.rb +109 -0
  100. data/spec/models/conversation_spec.rb +154 -0
  101. data/spec/models/mailbox_spec.rb +160 -0
  102. data/spec/models/mailboxer_models_messageable_spec.rb +324 -0
  103. data/spec/models/message_spec.rb +222 -0
  104. data/spec/models/receipt_spec.rb +56 -0
  105. data/spec/spec_helper.rb +36 -0
  106. data/spec/testfile.txt +1 -0
  107. metadata +301 -0
@@ -0,0 +1,3 @@
1
+ class AttachmentUploader < CarrierWave::Uploader::Base
2
+ storage :file
3
+ end
@@ -0,0 +1,20 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
5
+ </head>
6
+ <body>
7
+ <h1>You have a new message: <%= @message.subject.html_safe? ? @message.subject : strip_tags(@message.subject) %></h1>
8
+ <p>
9
+ You have received a new message:
10
+ </p>
11
+ <blockquote>
12
+ <p>
13
+ <%= raw @message.body %>
14
+ </p>
15
+ </blockquote>
16
+ <p>
17
+ Visit <%= link_to root_url,root_url %> and go to your inbox for more info.
18
+ </p>
19
+ </body>
20
+ </html>
@@ -0,0 +1,10 @@
1
+ You have a new message: <%= @message.subject.html_safe? ? @message.subject : strip_tags(@message.subject) %>
2
+ ===============================================
3
+
4
+ You have received a new message:
5
+
6
+ -----------------------------------------------
7
+ <%= @message.body.html_safe? ? @message.body : strip_tags(@message.body) %>
8
+ -----------------------------------------------
9
+
10
+ Visit <%= root_url %> and go to your inbox for more info.
@@ -0,0 +1,20 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
5
+ </head>
6
+ <body>
7
+ <h1>You have a new reply: <%= @message.subject.html_safe? ? @message.subject : strip_tags(@message.subject) %></h1>
8
+ <p>
9
+ You have received a new reply:
10
+ </p>
11
+ <blockquote>
12
+ <p>
13
+ <%= raw @message.body %>
14
+ </p>
15
+ </blockquote>
16
+ <p>
17
+ Visit <%= link_to root_url,root_url %> and go to your inbox for more info.
18
+ </p>
19
+ </body>
20
+ </html>
@@ -0,0 +1,10 @@
1
+ You have a new reply: <%= @message.subject.html_safe? ? @message.subject : strip_tags(@message.subject) %>
2
+ ===============================================
3
+
4
+ You have received a new reply:
5
+
6
+ -----------------------------------------------
7
+ <%= @message.body.html_safe? ? @message.body : strip_tags(@message.body) %>
8
+ -----------------------------------------------
9
+
10
+ Visit <%= root_url %> and go to your inbox for more info.
@@ -0,0 +1,7 @@
1
+ en:
2
+ mailboxer:
3
+ message_mailer:
4
+ subject_new: "Mailboxer new message: %{subject}"
5
+ subject_reply: "Mailboxer new reply: %{subject}"
6
+ notification_mailer:
7
+ subject: "Mailboxer new notification: %{subject}"
@@ -0,0 +1,7 @@
1
+ en:
2
+ mailboxer:
3
+ message_mailer:
4
+ subject_new: "Mailboxer nouveau message: %{subject}"
5
+ subject_reply: "Mailboxer nouvelle réponse: %{subject}"
6
+ notification_mailer:
7
+ subject: "Mailboxer nouvelle notification: %{subject}"
@@ -0,0 +1,61 @@
1
+ class CreateMailboxer < ActiveRecord::Migration
2
+ def self.up
3
+ #Tables
4
+ #Conversations
5
+ create_table :conversations do |t|
6
+ t.column :subject, :string, :default => ""
7
+ t.column :created_at, :datetime, :null => false
8
+ t.column :updated_at, :datetime, :null => false
9
+ end
10
+ #Receipts
11
+ create_table :receipts do |t|
12
+ t.references :receiver, :polymorphic => true
13
+ t.column :notification_id, :integer, :null => false
14
+ t.column :read, :boolean, :default => false
15
+ t.column :trashed, :boolean, :default => false
16
+ t.column :deleted, :boolean, :default => false
17
+ t.column :mailbox_type, :string, :limit => 25
18
+ t.column :created_at, :datetime, :null => false
19
+ t.column :updated_at, :datetime, :null => false
20
+ end
21
+ #Notifications and Messages
22
+ create_table :notifications do |t|
23
+ t.column :type, :string
24
+ t.column :body, :text
25
+ t.column :subject, :string, :default => ""
26
+ t.references :sender, :polymorphic => true
27
+ t.references :object, :polymorphic => true
28
+ t.column :conversation_id, :integer
29
+ t.column :draft, :boolean, :default => false
30
+ t.column :updated_at, :datetime, :null => false
31
+ t.column :created_at, :datetime, :null => false
32
+ end
33
+
34
+
35
+ #Indexes
36
+ #Conversations
37
+ #Receipts
38
+ add_index "receipts","notification_id"
39
+
40
+ #Messages
41
+ add_index "notifications","conversation_id"
42
+
43
+ #Foreign keys
44
+ #Conversations
45
+ #Receipts
46
+ add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
47
+ #Messages
48
+ add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
49
+ end
50
+
51
+ def self.down
52
+ #Tables
53
+ remove_foreign_key "receipts", :name => "receipts_on_notification_id"
54
+ remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
55
+
56
+ #Indexes
57
+ drop_table :receipts
58
+ drop_table :conversations
59
+ drop_table :notifications
60
+ end
61
+ end
@@ -0,0 +1,17 @@
1
+ class AddNotifiedObject < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :notifications do |t|
4
+ t.references :notified_object, :polymorphic => true
5
+ t.remove :object_id
6
+ t.remove :object_type
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ change_table :notifications do |t|
12
+ t.remove :notified_object_id
13
+ t.remove :notified_object_type
14
+ t.references :object, :polymorphic => true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ class AddNotificationCode < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :notifications do |t|
4
+ t.string :notification_code, :default => nil
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ change_table :notifications do |t|
10
+ t.remove :notification_code
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ class AddAttachments < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :notifications, :attachment, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :notifications, :attachment
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class RenameReceiptsRead < ActiveRecord::Migration
2
+ def up
3
+ rename_column :receipts, :read, :is_read
4
+ end
5
+
6
+ def down
7
+ rename_column :receipts, :is_read, :read
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddGlobalNotificationSupport < ActiveRecord::Migration
2
+
3
+ def change
4
+ change_table :notifications do |t|
5
+ t.boolean :global, default: false
6
+ t.datetime :expires
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class ChangeTableNotification < ActiveRecord::Migration
2
+
3
+ def change
4
+ rename_table :notifications, :messages
5
+ remove_column :messages, :type
6
+ remove_column :messages, :notified_object_id
7
+ remove_column :messages, :notified_object_type
8
+ remove_column :messages, :notification_code
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ class ChangeRelations < ActiveRecord::Migration
2
+
3
+ def change
4
+ rename_column :receipts, :notification_id, :message_id
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.0.15"
6
+ gem "mailboxer", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.1.6"
6
+ gem "mailboxer", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.6"
6
+ gem "mailboxer", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", ">=4.0.0"
6
+ gem "mailboxer", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,35 @@
1
+ class Mailboxer::InstallGenerator < Rails::Generators::Base #:nodoc:
2
+ include Rails::Generators::Migration
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ require 'rails/generators/migration'
5
+
6
+ def self.next_migration_number path
7
+ unless @prev_migration_nr
8
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
9
+ else
10
+ @prev_migration_nr += 1
11
+ end
12
+ @prev_migration_nr.to_s
13
+ end
14
+
15
+ def create_initializer_file
16
+ template 'initializer.rb', 'config/initializers/mailboxer.rb'
17
+ end
18
+
19
+ def copy_migrations
20
+ if Rails.version < "3.1"
21
+ migrations = [["20110511145103_create_mailboxer.rb","create_mailboxer.rb"],
22
+ ["20110719110700_add_notified_object.rb","add_notified_object.rb"],
23
+ ["20110912163911_add_notification_code.rb","add_notification_code.rb"],
24
+ ["20111204163911_add_attachments.rb","add_attachments.rb"]]
25
+ migrations.each do |migration|
26
+ migration_template "../../../../db/migrate/" + migration[0], "db/migrate/" + migration[1]
27
+ end
28
+ else
29
+ require 'rake'
30
+ Rails.application.load_tasks
31
+ Rake::Task['railties:install:migrations'].reenable
32
+ Rake::Task['mailboxer_engine:install:migrations'].invoke
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ Mailboxer.setup do |config|
2
+
3
+ #Configures if you applications uses or no the email sending for Notifications and Messages
4
+ config.uses_emails = true
5
+
6
+ #Configures the default from for the email sent for Messages and Notifications of Mailboxer
7
+ config.default_from = "no-reply@mailboxer.com"
8
+
9
+ #Configures the methods needed by mailboxer
10
+ config.email_method = :mailboxer_email
11
+ config.name_method = :name
12
+
13
+ #Configures if you use or not a search engine and wich one are you using
14
+ #Supported enignes: [:solr,:sphinx]
15
+ config.search_enabled = false
16
+ config.search_engine = :solr
17
+ end
@@ -0,0 +1,9 @@
1
+ class Mailboxer::ViewsGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../../../../app/views", __FILE__)
3
+
4
+ desc "Copy Mailboxer views into your app"
5
+ def copy_views
6
+ directory('message_mailer', 'app/views/message_mailer')
7
+ #directory('notification_mailer', 'app/views/notification_mailer')
8
+ end
9
+ end
data/lib/mailboxer.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Mailboxer
2
+ module Models
3
+ autoload :Messageable, 'mailboxer/models/messageable'
4
+ end
5
+
6
+ mattr_accessor :default_from
7
+ @@default_from = "no-reply@mailboxer.com"
8
+ mattr_accessor :uses_emails
9
+ @@uses_emails = true
10
+ mattr_accessor :mailer_wants_array
11
+ @@mailer_wants_array = false
12
+ mattr_accessor :search_enabled
13
+ @@search_enabled = false
14
+ mattr_accessor :search_engine
15
+ @@search_engine = :solr
16
+ mattr_accessor :email_method
17
+ @@email_method = :mailboxer_email
18
+ mattr_accessor :name_method
19
+ @@name_method = :name
20
+ #mattr_accessor :notification_mailer
21
+ mattr_accessor :message_mailer
22
+
23
+ class << self
24
+ def setup
25
+ yield self
26
+ end
27
+
28
+ def protected_attributes?
29
+ Rails.version < '4' || defined?(ProtectedAttributes)
30
+ end
31
+ end
32
+
33
+ end
34
+ # reopen ActiveRecord and include all the above to make
35
+ # them available to all our models if they want it
36
+ require 'mailboxer/engine'
37
+ require 'mailboxer/concerns/configurable_mailer'
@@ -0,0 +1,13 @@
1
+ require 'active_support/inflections'
2
+
3
+ module Concerns
4
+ module ConfigurableMailer
5
+
6
+ def get_mailer
7
+ return @mailer if @mailer
8
+ method = "#{self.class.to_s.downcase}_mailer".to_sym
9
+ @mailer = Mailboxer.send(method) || "#{self.class}Mailer".constantize
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # Database foreign keys
2
+ require 'foreigner'
3
+ require 'carrierwave'
4
+ begin
5
+ require 'sunspot_rails'
6
+ rescue LoadError
7
+ end
8
+
9
+ module Mailboxer
10
+ class Engine < Rails::Engine
11
+ initializer "mailboxer.models.messageable" do
12
+ ActiveSupport.on_load(:active_record) do
13
+ extend Mailboxer::Models::Messageable::ActiveRecord
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,225 @@
1
+ module Mailboxer
2
+ module Models
3
+ module Messageable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ActiveRecord
7
+ #Converts the model into messageable allowing it to interchange messages and
8
+ #receive notifications
9
+ def acts_as_messageable
10
+ include Messageable
11
+ end
12
+ end
13
+
14
+ included do
15
+ has_many :messages, :as => :sender
16
+ if Rails::VERSION::MAJOR == 4
17
+ has_many :receipts, -> { order 'created_at DESC' }, dependent: :destroy, as: :receiver
18
+ else
19
+ # Rails 3 does it this way
20
+ has_many :receipts, :order => 'created_at DESC', :dependent => :destroy, :as => :receiver
21
+ end
22
+ end
23
+
24
+ unless defined?(Mailboxer.name_method)
25
+ # Returning any kind of identification you want for the model
26
+ define_method Mailboxer.name_method do
27
+ begin
28
+ super
29
+ rescue NameError
30
+ return "You should add method :#{Mailboxer.name_method} in your Messageable model"
31
+ end
32
+ end
33
+ end
34
+
35
+ unless defined?(Mailboxer.email_method)
36
+ #Returning the email address of the model if an email should be sent for this object (Message or Notification).
37
+ #If no mail has to be sent, return nil.
38
+ define_method Mailboxer.email_method do |object|
39
+ begin
40
+ super
41
+ rescue NameError
42
+ return "You should add method :#{Mailboxer.email_method} in your Messageable model"
43
+ end
44
+ end
45
+ end
46
+
47
+ #Gets the mailbox of the messageable
48
+ def mailbox
49
+ @mailbox = Mailbox.new(self) if @mailbox.nil?
50
+ @mailbox.type = :all
51
+ @mailbox
52
+ end
53
+
54
+ #Sends a notification to the messageable
55
+ #def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil,send_mail=true)
56
+ # Message.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail)
57
+ #end
58
+
59
+ #Sends a messages, starting a new conversation, with the messageable
60
+ #as originator
61
+ def send_message(recipients, msg_body, subject=nil, sanitize_text=true, attachment=nil, message_timestamp = Time.now)
62
+ convo = Conversation.new({:subject => subject})
63
+ convo.created_at = message_timestamp
64
+ convo.updated_at = message_timestamp
65
+ message = messages.new({:body => msg_body, :subject => subject, :attachment => attachment})
66
+ message.created_at = message_timestamp
67
+ message.updated_at = message_timestamp
68
+ message.conversation = convo
69
+ message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
70
+ message.recipients = message.recipients.uniq
71
+ message.deliver false, sanitize_text
72
+ end
73
+
74
+ #Basic reply method. USE NOT RECOMENDED.
75
+ #Use reply_to_sender, reply_to_all and reply_to_conversation instead.
76
+ def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true, attachment=nil)
77
+ subject = subject || "RE: #{conversation.subject}"
78
+ response = messages.new({:body => reply_body, :subject => subject, :attachment => attachment})
79
+ response.conversation = conversation
80
+ response.recipients = recipients.is_a?(Array) ? recipients : [recipients]
81
+ response.recipients = response.recipients.uniq
82
+ response.recipients.delete(self)
83
+ response.deliver true, sanitize_text
84
+ end
85
+
86
+ #Replies to the sender of the message in the conversation
87
+ def reply_to_sender(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
88
+ reply(receipt.conversation, receipt.message.sender, reply_body, subject, sanitize_text, attachment)
89
+ end
90
+
91
+ #Replies to all the recipients of the message in the conversation
92
+ def reply_to_all(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
93
+ reply(receipt.conversation, receipt.message.recipients, reply_body, subject, sanitize_text, attachment)
94
+ end
95
+
96
+ #Replies to all the recipients of the last message in the conversation and untrash any trashed message by messageable
97
+ #if should_untrash is set to true (this is so by default)
98
+ def reply_to_conversation(conversation, reply_body, subject=nil, should_untrash=true, sanitize_text=true, attachment=nil)
99
+ #move conversation to inbox if it is currently in the trash and should_untrash parameter is true.
100
+ if should_untrash && mailbox.is_trashed?(conversation)
101
+ mailbox.receipts_for(conversation).untrash
102
+ mailbox.receipts_for(conversation).mark_as_not_deleted
103
+ end
104
+
105
+ reply(conversation, conversation.last_message.recipients, reply_body, subject, sanitize_text, attachment)
106
+ end
107
+
108
+ #Mark the object as read for messageable.
109
+ #
110
+ #Object can be:
111
+ #* A Receipt
112
+ #* A Message
113
+ #* A Notification
114
+ #* A Conversation
115
+ #* An array with any of them
116
+ def mark_as_read(obj)
117
+ case obj
118
+ when Receipt
119
+ obj.mark_as_read if obj.receiver == self
120
+ when Message#, Notification
121
+ obj.mark_as_read(self)
122
+ when Conversation
123
+ obj.mark_as_read(self)
124
+ when Array
125
+ obj.map{ |sub_obj| mark_as_read(sub_obj) }
126
+ end
127
+ end
128
+
129
+ #Mark the object as unread for messageable.
130
+ #
131
+ #Object can be:
132
+ #* A Receipt
133
+ #* A Message
134
+ #* A Notification
135
+ #* A Conversation
136
+ #* An array with any of them
137
+ def mark_as_unread(obj)
138
+ case obj
139
+ when Receipt
140
+ obj.mark_as_unread if obj.receiver == self
141
+ when Message#, Notification
142
+ obj.mark_as_unread(self)
143
+ when Conversation
144
+ obj.mark_as_unread(self)
145
+ when Array
146
+ obj.map{ |sub_obj| mark_as_unread(sub_obj) }
147
+ end
148
+ end
149
+
150
+ #Mark the object as deleted for messageable.
151
+ #
152
+ #Object can be:
153
+ #* A Receipt
154
+ #* A Notification
155
+ #* A Message
156
+ #* A Conversation
157
+ #* An array with any of them
158
+ def mark_as_deleted(obj)
159
+ case obj
160
+ when Receipt
161
+ return obj.mark_as_deleted if obj.receiver == self
162
+ when Message#, Notification
163
+ obj.mark_as_deleted(self)
164
+ when Conversation
165
+ obj.mark_as_deleted(self)
166
+ when Array
167
+ obj.map{ |sub_obj| mark_as_deleted(sub_obj) }
168
+ else
169
+ return nil
170
+ end
171
+ end
172
+
173
+ #Mark the object as trashed for messageable.
174
+ #
175
+ #Object can be:
176
+ #* A Receipt
177
+ #* A Message
178
+ #* A Notification
179
+ #* A Conversation
180
+ #* An array with any of them
181
+ def trash(obj)
182
+ case obj
183
+ when Receipt
184
+ obj.move_to_trash if obj.receiver == self
185
+ when Message#, Notification
186
+ obj.move_to_trash(self)
187
+ when Conversation
188
+ obj.move_to_trash(self)
189
+ when Array
190
+ obj.map{ |sub_obj| trash(sub_obj) }
191
+ end
192
+ end
193
+
194
+ #Mark the object as not trashed for messageable.
195
+ #
196
+ #Object can be:
197
+ #* A Receipt
198
+ #* A Message
199
+ #* A Notification
200
+ #* A Conversation
201
+ #* An array with any of them
202
+ def untrash(obj)
203
+ case obj
204
+ when Receipt
205
+ obj.untrash if obj.receiver == self
206
+ when Message#, Notification
207
+ obj.untrash(self)
208
+ when Conversation
209
+ obj.untrash(self)
210
+ when Array
211
+ obj.map{ |sub_obj| untrash(sub_obj) }
212
+ end
213
+ end
214
+
215
+ def search_messages(query)
216
+ @search = Receipt.search do
217
+ fulltext query
218
+ with :receiver_id, self.id
219
+ end
220
+
221
+ @search.results.map { |r| r.conversation }.uniq
222
+ end
223
+ end
224
+ end
225
+ end