Skip to content

Commit

Permalink
1st tentative of removing notification from mailboxer
Browse files Browse the repository at this point in the history
  • Loading branch information
Texicitys committed Oct 4, 2013
1 parent 03dfd9b commit fe842ba
Show file tree
Hide file tree
Showing 22 changed files with 512 additions and 463 deletions.
21 changes: 0 additions & 21 deletions app/mailers/notification_mailer.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class Conversation < ActiveRecord::Base
has_many :messages, :dependent => :destroy
has_many :receipts, :through => :messages

validates_presence_of :subject
#validates_presence_of :subject

before_validation :clean

scope :participant, lambda {|participant|
select('DISTINCT conversations.*').
where('notifications.type'=> Message.name).
#where('notifications.type'=> Message.name).
order("conversations.updated_at DESC").
joins(:receipts).merge(Receipt.recipient(participant))
}
Expand Down Expand Up @@ -122,7 +122,7 @@ def add_participant(participant)
messages = self.messages
messages.each do |message|
receipt = Receipt.new
receipt.notification = message
#receipt.notification = message
receipt.is_read = false
receipt.receiver = participant
receipt.mailbox_type = 'inbox'
Expand Down
4 changes: 2 additions & 2 deletions app/models/mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(messageable)
#Returns the notifications for the messageable
def notifications(options = {})
#:type => nil is a hack not to give Messages as Notifications
notifs = Notification.recipient(@messageable).where(:type => nil).order("notifications.created_at DESC")
notifs = Message.recipient(@messageable).where(:type => nil).order("messages.created_at DESC")
if (options[:read].present? and options[:read]==false) or (options[:unread].present? and options[:unread]==true)
notifs = notifs.unread
end
Expand Down Expand Up @@ -113,7 +113,7 @@ def is_completely_trashed?(conversation)
#If object isn't one of the above, a nil will be returned
def receipts_for(object)
case object
when Message, Notification
when Message#, Notification
object.receipt_for(@messageable)
when Conversation
object.receipts_for(@messageable)
Expand Down
154 changes: 152 additions & 2 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class Message < Notification
class Message < ActiveRecord::Base
#AJOUT DEPUIS NOTIFICATION
attr_accessor :recipients
attr_accessible :body, :subject, :global, :expires if Mailboxer.protected_attributes?

belongs_to :sender, :polymorphic => :true
belongs_to :notified_object, :polymorphic => :true
has_many :receipts, :dependent => :destroy

validates_presence_of :body#, :subject
#FIN NOTIFICATION
attr_accessible :attachment if Mailboxer.protected_attributes?

belongs_to :conversation, :validate => true, :autosave => true
Expand All @@ -9,6 +19,25 @@ class Message < Notification
scope :conversation, lambda { |conversation|
where(:conversation_id => conversation.id)
}
#AJOUT DEPUIS NOTIFICATION + modification de notifications en messages
scope :recipient, lambda { |recipient|
joins(:receipts).where('receipts.receiver_id' => recipient.id,'receipts.receiver_type' => recipient.class.base_class.to_s)
}
scope :with_object, lambda { |obj|
where('notified_object_id' => obj.id,'notified_object_type' => obj.class.to_s)
}
scope :not_trashed, lambda {
joins(:receipts).where('receipts.trashed' => false)
}
scope :unread, lambda {
joins(:receipts).where('receipts.is_read' => false)
}
scope :global, lambda { where(:global => true) }
scope :expired, lambda { where("messages.expires < ?", Time.now) }
scope :unexpired, lambda {
where("messages.expires is NULL OR messages.expires > ?", Time.now)
}
#FIN NOTIFICATION

mount_uploader :attachment, AttachmentUploader

Expand All @@ -19,6 +48,31 @@ class << self
def on_deliver(callback_method)
self.on_deliver_callback = callback_method
end

##Sends a Notification to all the recipients
#def notify_all(recipients,subject,body,obj = nil,sanitize_text = true,notification_code=nil,send_mail=true)
# notification = Notification.new({:body => body, :subject => subject})
# notification.recipients = recipients.respond_to?(:each) ? recipients : [recipients]
# notification.recipients = notification.recipients.uniq if recipients.respond_to?(:uniq)
# notification.notified_object = obj if obj.present?
# notification.notification_code = notification_code if notification_code.present?
# notification.deliver sanitize_text, send_mail
#end
#
##Takes a +Receipt+ or an +Array+ of them and returns +true+ if the delivery was
##successful or +false+ if some error raised
#def successful_delivery? receipts
# case receipts
# when Receipt
# receipts.valid?
# receipts.errors.empty?
# when Array
# receipts.each(&:valid?)
# receipts.all? { |t| t.errors.empty? }
# else
# false
# end
#end
end

#Delivers a Message. USE NOT RECOMENDED.
Expand Down Expand Up @@ -56,10 +110,106 @@ def deliver(reply = false, should_clean = true)
sender_receipt
end

#AJOUT DEPUIS NOTIFICATION
#Returns the recipients of the message
def recipients
if @recipients.blank?
recipients_array = Array.new
self.receipts.each do |receipt|
recipients_array << receipt.receiver
end

recipients_array
else
@recipients
end
end

#Returns the receipt for the participant
def receipt_for(participant)
Receipt.message(self).recipient(participant)
end

#Returns the receipt for the participant. Alias for receipt_for(participant)
def receipts_for(participant)
receipt_for(participant)
end

#Returns if the participant have read the message
def is_unread?(participant)
return false if participant.nil?
!self.receipt_for(participant).first.is_read
end

def is_read?(participant)
!self.is_unread?(participant)
end

#Returns if the participant have trashed the message
def is_trashed?(participant)
return false if participant.nil?
self.receipt_for(participant).first.trashed
end

#Returns if the participant have deleted the message
def is_deleted?(participant)
return false if participant.nil?
return self.receipt_for(participant).first.deleted
end

#Mark the message as read
def mark_as_read(participant)
return if participant.nil?
self.receipt_for(participant).mark_as_read
end

#Mark the message as unread
def mark_as_unread(participant)
return if participant.nil?
self.receipt_for(participant).mark_as_unread
end

#Move the message to the trash
def move_to_trash(participant)
return if participant.nil?
self.receipt_for(participant).move_to_trash
end

#Takes the message out of the trash
def untrash(participant)
return if participant.nil?
self.receipt_for(participant).untrash
end

#Mark the message as deleted for one of the participant
def mark_as_deleted(participant)
return if participant.nil?
return self.receipt_for(participant).mark_as_deleted
end

include ActionView::Helpers::SanitizeHelper

#Sanitizes the body and subject
def clean
unless self.subject.nil?
self.subject = sanitize self.subject
end
self.body = sanitize self.body
end

#Returns notified_object. DEPRECATED
def object
warn "DEPRECATION WARNING: use 'notify_object' instead of 'object' to get the object associated with the Message"
notified_object
end

#FIN NOTIFICATION

private
def build_receipt(receiver, mailbox_type, is_read = false)
Receipt.new.tap do |receipt|
receipt.notification = self
#mis message à la place de notification
receipt.message = self
receipt.is_read = is_read
receipt.receiver = receiver
receipt.mailbox_type = mailbox_type
Expand Down
4 changes: 2 additions & 2 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Notification < ActiveRecord::Base
class Notification #< ActiveRecord::Base
attr_accessor :recipients
attr_accessible :body, :subject, :global, :expires if Mailboxer.protected_attributes?

belongs_to :sender, :polymorphic => :true
belongs_to :notified_object, :polymorphic => :true
has_many :receipts, :dependent => :destroy

validates_presence_of :subject, :body
validates_presence_of :body#, :subject

scope :recipient, lambda { |recipient|
joins(:receipts).where('receipts.receiver_id' => recipient.id,'receipts.receiver_type' => recipient.class.base_class.to_s)
Expand Down
26 changes: 13 additions & 13 deletions app/models/receipt.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Receipt < ActiveRecord::Base
attr_accessible :trashed, :is_read, :deleted if Mailboxer.protected_attributes?

belongs_to :notification, :validate => true, :autosave => true
belongs_to :message, :validate => true, :autosave => true
belongs_to :receiver, :polymorphic => :true
belongs_to :message, :foreign_key => "notification_id"
#belongs_to :message, :foreign_key => "notification_id"

validates_presence_of :receiver

Expand All @@ -12,13 +12,13 @@ class Receipt < ActiveRecord::Base
}
#Notifications Scope checks type to be nil, not Notification because of STI behaviour
#with the primary class (no type is saved)
scope :notifications_receipts, lambda { joins(:notification).where('notifications.type' => nil) }
scope :messages_receipts, lambda { joins(:notification).where('notifications.type' => Message.to_s) }
scope :notification, lambda { |notification|
where(:notification_id => notification.id)
}
#scope :notifications_receipts, lambda { joins(:notification).where('notifications.type' => nil) }
scope :messages_receipts, lambda { joins(:message).where('messages.type' => Message.to_s) }
#scope :notification, lambda { |notification|
# where(:notification_id => notification.id)
#}
scope :conversation, lambda { |conversation|
joins(:message).where('notifications.conversation_id' => conversation.id)
joins(:message).where('messages.conversation_id' => conversation.id)
}
scope :sentbox, lambda { where(:mailbox_type => "sentbox") }
scope :inbox, lambda { where(:mailbox_type => "inbox") }
Expand Down Expand Up @@ -137,12 +137,12 @@ def conversation
message.conversation if message.is_a? Message
end

#Returns if the participant have read the Notification
#Returns if the participant have read the message
def is_unread?
!self.is_read
end

#Returns if the participant have trashed the Notification
#Returns if the participant have trashed the message
def is_trashed?
self.trashed
end
Expand All @@ -152,9 +152,9 @@ def is_trashed?
#Removes the duplicate error about not present subject from Conversation if it has been already
#raised by Message
def remove_duplicate_errors
if self.errors["notification.conversation.subject"].present? and self.errors["notification.subject"].present?
self.errors["notification.conversation.subject"].each do |msg|
self.errors["notification.conversation.subject"].delete(msg)
if self.errors["message.conversation.subject"].present? and self.errors["message.subject"].present?
self.errors["message.conversation.subject"].each do |msg|
self.errors["message.conversation.subject"].delete(msg)
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions app/views/notification_mailer/new_notification_email.html.erb

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/notification_mailer/new_notification_email.text.erb

This file was deleted.

7 changes: 7 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
mailboxer:
message_mailer:
subject_new: "Mailboxer nouveau message: %{subject}"
subject_reply: "Mailboxer nouvelle réponse: %{subject}"
notification_mailer:
subject: "Mailboxer nouvelle notification: %{subject}"
13 changes: 13 additions & 0 deletions db/migrate/20131003144212_change_table_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ChangeTableNotification < ActiveRecord::Migration

def change
rename_table :notifications, :messages
remove_column :messages, :type
remove_column :messages, :notified_object_id
remove_column :messages, :notified_object_type
remove_column :messages, :notified_object_type
remove_column :messages, :notification_code


end
end
2 changes: 1 addition & 1 deletion lib/generators/mailboxer/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Mailboxer::ViewsGenerator < Rails::Generators::Base
desc "Copy Mailboxer views into your app"
def copy_views
directory('message_mailer', 'app/views/message_mailer')
directory('notification_mailer', 'app/views/notification_mailer')
#directory('notification_mailer', 'app/views/notification_mailer')
end
end
2 changes: 1 addition & 1 deletion lib/mailboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Models
@@email_method = :mailboxer_email
mattr_accessor :name_method
@@name_method = :name
mattr_accessor :notification_mailer
#mattr_accessor :notification_mailer
mattr_accessor :message_mailer

class << self
Expand Down
Loading

0 comments on commit fe842ba

Please sign in to comment.