From fe842bac13a2220353673a29acf39b016678e756 Mon Sep 17 00:00:00 2001 From: Texicitys Date: Fri, 4 Oct 2013 20:22:18 +0200 Subject: [PATCH] 1st tentative of removing notification from mailboxer --- app/mailers/notification_mailer.rb | 21 -- app/models/conversation.rb | 6 +- app/models/mailbox.rb | 4 +- app/models/message.rb | 154 +++++++++++++- app/models/notification.rb | 4 +- app/models/receipt.rb | 26 +-- .../new_notification_email.html.erb | 20 -- .../new_notification_email.text.erb | 10 - config/locales/fr.yml | 7 + ...0131003144212_change_table_notification.rb | 13 ++ lib/generators/mailboxer/views_generator.rb | 2 +- lib/mailboxer.rb | 2 +- lib/mailboxer/models/messageable.rb | 12 +- spec/integration/message_and_receipt_spec.rb | 130 ++++++------ .../concerns/configurable_mailer_spec.rb | 22 +- spec/mailers/notification_mailer_spec.rb | 61 ------ spec/models/conversation_spec.rb | 12 +- spec/models/mailbox_spec.rb | 4 +- .../mailboxer_models_messageable_spec.rb | 72 +++---- spec/models/message_spec.rb | 191 +++++++++++++++++ spec/models/notification_spec.rb | 200 ------------------ spec/models/receipt_spec.rb | 2 +- 22 files changed, 512 insertions(+), 463 deletions(-) delete mode 100644 app/mailers/notification_mailer.rb delete mode 100644 app/views/notification_mailer/new_notification_email.html.erb delete mode 100644 app/views/notification_mailer/new_notification_email.text.erb create mode 100644 config/locales/fr.yml create mode 100644 db/migrate/20131003144212_change_table_notification.rb delete mode 100644 spec/mailers/notification_mailer_spec.rb delete mode 100644 spec/models/notification_spec.rb diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb deleted file mode 100644 index a1dac565..00000000 --- a/app/mailers/notification_mailer.rb +++ /dev/null @@ -1,21 +0,0 @@ -class NotificationMailer < ActionMailer::Base - default :from => Mailboxer.default_from - #Sends and email for indicating a new notification to a receiver. - #It calls new_notification_email. - def send_email(notification,receiver) - new_notification_email(notification,receiver) - end - - include ActionView::Helpers::SanitizeHelper - - #Sends an email for indicating a new message for the receiver - def new_notification_email(notification,receiver) - @notification = notification - @receiver = receiver - subject = notification.subject.to_s - subject = strip_tags(subject) unless subject.html_safe? - mail :to => receiver.send(Mailboxer.email_method,notification), - :subject => t('mailboxer.notification_mailer.subject', :subject => subject), - :template_name => 'new_notification_email' - end -end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 357d76c8..de9ca88f 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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)) } @@ -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' diff --git a/app/models/mailbox.rb b/app/models/mailbox.rb index cedf697f..7664853f 100644 --- a/app/models/mailbox.rb +++ b/app/models/mailbox.rb @@ -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 @@ -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) diff --git a/app/models/message.rb b/app/models/message.rb index 0032af4b..43e46ff9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 @@ -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 @@ -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. @@ -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 diff --git a/app/models/notification.rb b/app/models/notification.rb index 4e780045..2c1ac778 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,4 +1,4 @@ -class Notification < ActiveRecord::Base +class Notification #< ActiveRecord::Base attr_accessor :recipients attr_accessible :body, :subject, :global, :expires if Mailboxer.protected_attributes? @@ -6,7 +6,7 @@ class Notification < ActiveRecord::Base 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) diff --git a/app/models/receipt.rb b/app/models/receipt.rb index 048e4d29..cb313d28 100644 --- a/app/models/receipt.rb +++ b/app/models/receipt.rb @@ -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 @@ -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") } @@ -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 @@ -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 diff --git a/app/views/notification_mailer/new_notification_email.html.erb b/app/views/notification_mailer/new_notification_email.html.erb deleted file mode 100644 index 5502d7fc..00000000 --- a/app/views/notification_mailer/new_notification_email.html.erb +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - -

You have a new notification: <%= @notification.subject.html_safe? ? @notification.subject : strip_tags(@notification.subject) %>

-

- You have received a new notification: -

-
-

- <%= raw @notification.body %> -

-
-

- Visit <%= link_to root_url,root_url %> and go to your notifications for more info. -

- - \ No newline at end of file diff --git a/app/views/notification_mailer/new_notification_email.text.erb b/app/views/notification_mailer/new_notification_email.text.erb deleted file mode 100644 index e55639b5..00000000 --- a/app/views/notification_mailer/new_notification_email.text.erb +++ /dev/null @@ -1,10 +0,0 @@ -You have a new notification: <%= @notification.subject.html_safe? ? @notification.subject : strip_tags(@notification.subject) %> -=============================================== - -You have received a new notification: - ------------------------------------------------ -<%= @notification.body.html_safe? ? @notification.body : strip_tags(@notification.body) %> ------------------------------------------------ - -Visit <%= root_url %> and go to your notifications for more info. diff --git a/config/locales/fr.yml b/config/locales/fr.yml new file mode 100644 index 00000000..ac971783 --- /dev/null +++ b/config/locales/fr.yml @@ -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}" diff --git a/db/migrate/20131003144212_change_table_notification.rb b/db/migrate/20131003144212_change_table_notification.rb new file mode 100644 index 00000000..143e148d --- /dev/null +++ b/db/migrate/20131003144212_change_table_notification.rb @@ -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 diff --git a/lib/generators/mailboxer/views_generator.rb b/lib/generators/mailboxer/views_generator.rb index 80d604d9..2cfd859d 100644 --- a/lib/generators/mailboxer/views_generator.rb +++ b/lib/generators/mailboxer/views_generator.rb @@ -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 \ No newline at end of file diff --git a/lib/mailboxer.rb b/lib/mailboxer.rb index 560fa6c0..2ef8655d 100644 --- a/lib/mailboxer.rb +++ b/lib/mailboxer.rb @@ -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 diff --git a/lib/mailboxer/models/messageable.rb b/lib/mailboxer/models/messageable.rb index c4c442ce..2c1ff1fe 100644 --- a/lib/mailboxer/models/messageable.rb +++ b/lib/mailboxer/models/messageable.rb @@ -53,7 +53,7 @@ def mailbox #Sends a notification to the messageable def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil,send_mail=true) - Notification.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail) + Message.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail) end #Sends a messages, starting a new conversation, with the messageable @@ -117,7 +117,7 @@ def mark_as_read(obj) case obj when Receipt obj.mark_as_read if obj.receiver == self - when Message, Notification + when Message#, Notification obj.mark_as_read(self) when Conversation obj.mark_as_read(self) @@ -138,7 +138,7 @@ def mark_as_unread(obj) case obj when Receipt obj.mark_as_unread if obj.receiver == self - when Message, Notification + when Message#, Notification obj.mark_as_unread(self) when Conversation obj.mark_as_unread(self) @@ -159,7 +159,7 @@ def mark_as_deleted(obj) case obj when Receipt return obj.mark_as_deleted if obj.receiver == self - when Message, Notification + when Message#, Notification obj.mark_as_deleted(self) when Conversation obj.mark_as_deleted(self) @@ -182,7 +182,7 @@ def trash(obj) case obj when Receipt obj.move_to_trash if obj.receiver == self - when Message, Notification + when Message#, Notification obj.move_to_trash(self) when Conversation obj.move_to_trash(self) @@ -203,7 +203,7 @@ def untrash(obj) case obj when Receipt obj.untrash if obj.receiver == self - when Message, Notification + when Message#, Notification obj.untrash(self) when Conversation obj.untrash(self) diff --git a/spec/integration/message_and_receipt_spec.rb b/spec/integration/message_and_receipt_spec.rb index 49612917..7f374f83 100644 --- a/spec/integration/message_and_receipt_spec.rb +++ b/spec/integration/message_and_receipt_spec.rb @@ -12,7 +12,7 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") - @message1 = @receipt1.notification + @message1 = @receipt1.message end it "should create proper message" do @@ -24,7 +24,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity1).notification(@message1).first + mail = Receipt.recipient(@entity1).message(@message1).first assert mail if mail mail.is_read.should==true @@ -32,7 +32,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity2).notification(@message1).first + mail = Receipt.recipient(@entity2).message(@message1).first assert mail if mail mail.is_read.should==false @@ -54,8 +54,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_sender(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -67,7 +67,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -75,7 +75,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -100,8 +100,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -113,7 +113,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -121,7 +121,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -145,8 +145,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_conversation(@receipt1.conversation,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -158,7 +158,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -166,7 +166,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -198,7 +198,7 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") - @message1 = @receipt1.notification + @message1 = @receipt1.message end it "should create proper message" do @@ -210,7 +210,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity1).notification(@message1).first + mail = Receipt.recipient(@entity1).message(@message1).first assert mail if mail mail.is_read.should==true @@ -218,7 +218,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity2).notification(@message1).first + mail = Receipt.recipient(@entity2).message(@message1).first assert mail if mail mail.is_read.should==false @@ -240,8 +240,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_sender(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -253,7 +253,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -261,7 +261,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -286,8 +286,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -299,7 +299,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -307,7 +307,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -364,7 +364,7 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") - @message1 = @receipt1.notification + @message1 = @receipt1.message end it "should create proper message" do @@ -376,7 +376,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity1).notification(@message1).first + mail = Receipt.recipient(@entity1).message(@message1).first assert mail if mail mail.is_read.should==true @@ -385,7 +385,7 @@ end #Receiver Mails @recipients.each do |receiver| - mail = Receipt.recipient(receiver).notification(@message1).first + mail = Receipt.recipient(receiver).message(@message1).first assert mail if mail mail.is_read.should==false @@ -409,8 +409,8 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") @receipt2 = @entity2.reply_to_sender(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -422,7 +422,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -430,7 +430,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -439,7 +439,7 @@ end #No Receiver, No Mail - mail = Receipt.recipient(@entity3).notification(@message2).first + mail = Receipt.recipient(@entity3).message(@message2).first assert mail.nil? end @@ -461,8 +461,8 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message @recipients2 = Array.new @recipients2 << @entity1 @recipients2 << @entity3 @@ -478,7 +478,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -487,7 +487,7 @@ end #Receiver Mails @recipients2.each do |receiver| - mail = Receipt.recipient(receiver).notification(@message2).first + mail = Receipt.recipient(receiver).message(@message2).first assert mail if mail mail.is_read.should==false @@ -546,7 +546,7 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") - @message1 = @receipt1.notification + @message1 = @receipt1.message end it "should create proper message" do @@ -558,7 +558,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity1).notification(@message1).first + mail = Receipt.recipient(@entity1).message(@message1).first assert mail if mail mail.is_read.should==true @@ -567,7 +567,7 @@ end #Receiver Mails @recipients.each do |receiver| - mail = Receipt.recipient(receiver).notification(@message1).first + mail = Receipt.recipient(receiver).message(@message1).first assert mail if mail mail.is_read.should==false @@ -591,8 +591,8 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") @receipt2 = @entity2.reply_to_sender(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -604,7 +604,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -612,7 +612,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -621,7 +621,7 @@ end #No Receiver, No Mail - mail = Receipt.recipient(@entity3).notification(@message2).first + mail = Receipt.recipient(@entity3).message(@message2).first assert mail.nil? end @@ -643,8 +643,8 @@ before do @receipt1 = @entity1.send_message(@recipients,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message @recipients2 = Array.new @recipients2 << @entity1 @recipients2 << @entity3 @@ -660,7 +660,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -669,7 +669,7 @@ end #Receiver Mails @recipients2.each do |receiver| - mail = Receipt.recipient(receiver).notification(@message2).first + mail = Receipt.recipient(receiver).message(@message2).first assert mail if mail mail.is_read.should==false @@ -725,7 +725,7 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") - @message1 = @receipt1.notification + @message1 = @receipt1.message end it "should create proper message" do @@ -737,7 +737,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity1).notification(@message1).first + mail = Receipt.recipient(@entity1).message(@message1).first assert mail if mail mail.is_read.should==true @@ -745,7 +745,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity2).notification(@message1).first + mail = Receipt.recipient(@entity2).message(@message1).first assert mail if mail mail.is_read.should==false @@ -767,8 +767,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_sender(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -780,7 +780,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -788,7 +788,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -813,8 +813,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -826,7 +826,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -834,7 +834,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false @@ -858,8 +858,8 @@ before do @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_conversation(@receipt1.conversation,"Reply body") - @message1 = @receipt1.notification - @message2 = @receipt2.notification + @message1 = @receipt1.message + @message2 = @receipt2.message end it "should create proper message" do @@ -871,7 +871,7 @@ it "should create proper mails" do #Sender Mail - mail = Receipt.recipient(@entity2).notification(@message2).first + mail = Receipt.recipient(@entity2).message(@message2).first assert mail if mail mail.is_read.should==true @@ -879,7 +879,7 @@ mail.mailbox_type.should=="sentbox" end #Receiver Mail - mail = Receipt.recipient(@entity1).notification(@message2).first + mail = Receipt.recipient(@entity1).message(@message2).first assert mail if mail mail.is_read.should==false diff --git a/spec/mailboxer/concerns/configurable_mailer_spec.rb b/spec/mailboxer/concerns/configurable_mailer_spec.rb index 5de42dd0..9d38fa86 100644 --- a/spec/mailboxer/concerns/configurable_mailer_spec.rb +++ b/spec/mailboxer/concerns/configurable_mailer_spec.rb @@ -1,17 +1,17 @@ require 'spec_helper' describe Concerns::ConfigurableMailer do - describe "Notification instance#get_mailer" do - before { @obj = Notification.new } - it "returns default_mailer" do - @obj.get_mailer.should eq NotificationMailer - end - it "returns 'foo' from Mailerbox.notification_mailer" do - Mailboxer.notification_mailer = 'foo' - @obj.get_mailer.should eq 'foo' - end - after { Mailboxer.notification_mailer = nil } - end + #describe "Notification instance#get_mailer" do + # before { @obj = Notification.new } + # it "returns default_mailer" do + # @obj.get_mailer.should eq NotificationMailer + # end + # it "returns 'foo' from Mailerbox.notification_mailer" do + # Mailboxer.notification_mailer = 'foo' + # @obj.get_mailer.should eq 'foo' + # end + # after { Mailboxer.notification_mailer = nil } + #end describe "Message instance#get_mailer" do before { @obj = Message.new } diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb deleted file mode 100644 index 7993803e..00000000 --- a/spec/mailers/notification_mailer_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'spec_helper' - -describe NotificationMailer do - before do - @entity1 = FactoryGirl.create(:user) - @entity2 = FactoryGirl.create(:duck) - @entity3 = FactoryGirl.create(:cylon) - @receipt1 = Notification.notify_all([@entity1,@entity2,@entity3],"Subject", "Body Body Body Body Body Body Body Body Body Body Body Body") - end - - it "should send emails when should_email? is true (2 out of 3)" do - ActionMailer::Base.deliveries.empty?.should==false - ActionMailer::Base.deliveries.size.should==2 - end - - it "should send an email to user entity" do - temp = false - ActionMailer::Base.deliveries.each do |email| - if email.to.first.to_s.eql? @entity1.email - temp = true - end - end - temp.should==true - end - - it "should send an email to duck entity" do - temp = false - ActionMailer::Base.deliveries.each do |email| - if email.to.first.to_s.eql? @entity2.email - temp = true - end - end - temp.should==true - end - - it "shouldn't send an email to cylon entity" do - temp = false - ActionMailer::Base.deliveries.each do |email| - if email.to.first.to_s.eql? @entity3.email - temp = true - end - end - temp.should==false - end -end - -def print_emails - ActionMailer::Base.deliveries.each do |email| - puts "----------------------------------------------------" - puts email.to - puts "---" - puts email.from - puts "---" - puts email.subject - puts "---" - puts email.body - puts "---" - puts email.encoded - puts "----------------------------------------------------" - end -end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 9f2c1293..df5c0bca 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -9,8 +9,8 @@ @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1") @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2") @receipt4 = @entity2.reply_to_all(@receipt3,"Reply body 3") - @message1 = @receipt1.notification - @message4 = @receipt4.notification + @message1 = @receipt1.message + @message4 = @receipt4.message @conversation = @message1.conversation end @@ -79,8 +79,8 @@ describe "scopes" do let(:participant) { FactoryGirl.create(:user) } - let!(:inbox_conversation) { @entity1.send_message(participant, "Body", "Subject").notification.conversation } - let!(:sentbox_conversation) { participant.send_message(@entity1, "Body", "Subject").notification.conversation } + let!(:inbox_conversation) { @entity1.send_message(participant, "Body", "Subject").message.conversation } + let!(:sentbox_conversation) { participant.send_message(@entity1, "Body", "Subject").message.conversation } describe ".participant" do @@ -103,7 +103,7 @@ describe ".trash" do it "finds trash conversations with receipts for participant" do - trashed_conversation = @entity1.send_message(participant, "Body", "Subject").notification.conversation + trashed_conversation = @entity1.send_message(participant, "Body", "Subject").message.conversation trashed_conversation.move_to_trash(participant) Conversation.trash(participant).should == [trashed_conversation] @@ -113,7 +113,7 @@ describe ".unread" do it "finds unread conversations with receipts for participant" do [sentbox_conversation, inbox_conversation].each {|c| c.mark_as_read(participant) } - unread_conversation = @entity1.send_message(participant, "Body", "Subject").notification.conversation + unread_conversation = @entity1.send_message(participant, "Body", "Subject").message.conversation Conversation.unread(participant).should == [unread_conversation] end diff --git a/spec/models/mailbox_spec.rb b/spec/models/mailbox_spec.rb index 62f097f1..197fd6ee 100644 --- a/spec/models/mailbox_spec.rb +++ b/spec/models/mailbox_spec.rb @@ -9,8 +9,8 @@ @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1") @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2") @receipt4 = @entity2.reply_to_all(@receipt3,"Reply body 3") - @message1 = @receipt1.notification - @message4 = @receipt4.notification + @message1 = @receipt1.message + @message4 = @receipt4.message @conversation = @message1.conversation end diff --git a/spec/models/mailboxer_models_messageable_spec.rb b/spec/models/mailboxer_models_messageable_spec.rb index 5fa3cf20..95e2b530 100644 --- a/spec/models/mailboxer_models_messageable_spec.rb +++ b/spec/models/mailboxer_models_messageable_spec.rb @@ -159,72 +159,72 @@ - it "should be able to unread an owned Notification (mark as unread)" do + it "should be able to unread an owned message (mark as unread)" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.is_read.should==false - @entity1.mark_as_read(@notification) - @entity1.mark_as_unread(@notification) - @notification.receipt_for(@entity1).first.is_read.should==false + @entity1.mark_as_read(@message) + @entity1.mark_as_unread(@message) + @message.receipt_for(@entity1).first.is_read.should==false end - it "should be able to read an owned Notification (mark as read)" do + it "should be able to read an owned message (mark as read)" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.is_read.should==false - @entity1.mark_as_read(@notification) - @notification.receipt_for(@entity1).first.is_read.should==true + @entity1.mark_as_read(@message) + @message.receipt_for(@entity1).first.is_read.should==true end - it "should not be able to unread a not owned Notification (mark as unread)" do + it "should not be able to unread a not owned message (mark as unread)" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.is_read.should==false - @entity1.mark_as_read(@notification) - @entity2.mark_as_unread(@notification) - @notification.receipt_for(@entity1).first.is_read.should==true + @entity1.mark_as_read(@message) + @entity2.mark_as_unread(@message) + @message.receipt_for(@entity1).first.is_read.should==true end - it "should not be able to read a not owned Notification (mark as read)" do + it "should not be able to read a not owned message (mark as read)" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.is_read.should==false - @entity2.mark_as_read(@notification) - @notification.receipt_for(@entity1).first.is_read.should==false + @entity2.mark_as_read(@message) + @message.receipt_for(@entity1).first.is_read.should==false end - it "should be able to trash an owned Notification" do + it "should be able to trash an owned message" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.trashed.should==false - @entity1.trash(@notification) - @notification.receipt_for(@entity1).first.trashed.should==true + @entity1.trash(@message) + @message.receipt_for(@entity1).first.trashed.should==true end - it "should be able to untrash an owned Notification" do + it "should be able to untrash an owned message" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.trashed.should==false - @entity1.trash(@notification) - @entity1.untrash(@notification) - @notification.receipt_for(@entity1).first.trashed.should==false + @entity1.trash(@message) + @entity1.untrash(@message) + @message.receipt_for(@entity1).first.trashed.should==false end - it "should not be able to trash a not owned Notification" do + it "should not be able to trash a not owned message" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.trashed.should==false - @entity2.trash(@notification) - @notification.receipt_for(@entity1).first.trashed.should==false + @entity2.trash(@message) + @message.receipt_for(@entity1).first.trashed.should==false end - it "should not be able to untrash a not owned Notification" do + it "should not be able to untrash a not owned message" do @receipt = @entity1.notify("Subject","Body") - @notification = @receipt.notification + @message = @receipt.message @receipt.trashed.should==false - @entity1.trash(@notification) - @entity2.untrash(@notification) - @notification.receipt_for(@entity1).first.trashed.should==true + @entity1.trash(@message) + @entity2.untrash(@message) + @message.receipt_for(@entity1).first.trashed.should==true end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 9cb1c5d3..2f6d768d 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -5,6 +5,7 @@ before do @entity1 = FactoryGirl.create(:user) @entity2 = FactoryGirl.create(:user) + @entity3 = FactoryGirl.create(:user) @receipt1 = @entity1.send_message(@entity2,"Body","Subject") @receipt2 = @entity2.reply_to_all(@receipt1,"Reply body 1") @receipt3 = @entity1.reply_to_all(@receipt2,"Reply body 2") @@ -26,5 +27,195 @@ @message1.mark_as_deleted @entity1 @message1.is_deleted?(@entity1).should==true end + + #Ajout depuit notification + it "should notify one user" do + @entity1.notify("Subject", "Body") + + #Check getting ALL receipts + @entity1.mailbox.receipts.size.should==1 + receipt = @entity1.mailbox.receipts.first + message = receipt.message + message.subject.should=="Subject" + message.body.should=="Body" + + #Check getting message receipts only + @entity1.mailbox.messages.size.should==1 + message = @entity1.mailbox.messages.first + message.subject.should=="Subject" + message.body.should=="Body" + end + + it "should be unread by default" do + @entity1.notify("Subject", "Body") + @entity1.mailbox.receipts.size.should==1 + message = @entity1.mailbox.receipts.first.message + message.should be_is_unread(@entity1) + end + + it "should be able to marked as read" do + @entity1.notify("Subject", "Body") + @entity1.mailbox.receipts.size.should==1 + message = @entity1.mailbox.receipts.first.message + message.mark_as_read(@entity1) + message.should be_is_read(@entity1) + end + + it "should notify several users" do + recipients = Set.new [@entity1, @entity2, @entity3] + message.notify_all(recipients, "Subject", "Body") + + #Check getting ALL receipts + @entity1.mailbox.receipts.size.should==1 + receipt = @entity1.mailbox.receipts.first + message = receipt.message + message.subject.should=="Subject" + message.body.should=="Body" + @entity2.mailbox.receipts.size.should==1 + receipt = @entity2.mailbox.receipts.first + message = receipt.message + message.subject.should=="Subject" + message.body.should=="Body" + @entity3.mailbox.receipts.size.should==1 + receipt = @entity3.mailbox.receipts.first + message = receipt.message + message.subject.should=="Subject" + message.body.should=="Body" + + #Check getting message receipts only + @entity1.mailbox.messages.size.should==1 + message = @entity1.mailbox.messages.first + message.subject.should=="Subject" + message.body.should=="Body" + @entity2.mailbox.messages.size.should==1 + message = @entity2.mailbox.messages.first + message.subject.should=="Subject" + message.body.should=="Body" + @entity3.mailbox.messages.size.should==1 + message = @entity3.mailbox.messages.first + message.subject.should=="Subject" + message.body.should=="Body" + + end + + it "should notify a single recipient" do + message.notify_all(@entity1, "Subject", "Body") + + #Check getting ALL receipts + @entity1.mailbox.receipts.size.should==1 + receipt = @entity1.mailbox.receipts.first + message = receipt.message + message.subject.should=="Subject" + message.body.should=="Body" + + #Check getting message receipts only + @entity1.mailbox.messages.size.should==1 + message = @entity1.mailbox.messages.first + message.subject.should=="Subject" + message.body.should=="Body" + end + + describe "#expire" do + subject { Message.new } + + describe "when the message is already expired" do + before do + subject.stub(:expired? => true) + end + it 'should not update the expires attribute' do + subject.should_not_receive :expires= + subject.should_not_receive :save + subject.expire + end + end + + describe "when the message is not expired" do + let(:now) { Time.now } + let(:one_second_ago) { now - 1.second } + before do + Time.stub(:now => now) + subject.stub(:expired? => false) + end + it 'should update the expires attribute' do + subject.should_receive(:expires=).with(one_second_ago) + subject.expire + end + it 'should not save the record' do + subject.should_not_receive :save + subject.expire + end + end + + end + + describe "#expire!" do + subject { Message.new } + + describe "when the message is already expired" do + before do + subject.stub(:expired? => true) + end + it 'should not call expire' do + subject.should_not_receive :expire + subject.should_not_receive :save + subject.expire! + end + end + + describe "when the message is not expired" do + let(:now) { Time.now } + let(:one_second_ago) { now - 1.second } + before do + Time.stub(:now => now) + subject.stub(:expired? => false) + end + it 'should call expire' do + subject.should_receive(:expire) + subject.expire! + end + it 'should save the record' do + subject.should_receive :save + subject.expire! + end + end + + end + + describe "#expired?" do + subject { Message.new } + context "when the expiration date is in the past" do + before { subject.stub(:expires => Time.now - 1.second) } + it 'should be expired' do + subject.expired?.should be_true + end + end + + context "when the expiration date is now" do + before { + time = Time.now + Time.stub(:now => time) + subject.stub(:expires => time) + } + + it 'should not be expired' do + subject.expired?.should be_false + end + end + + context "when the expiration date is in the future" do + before { subject.stub(:expires => Time.now + 1.second) } + it 'should not be expired' do + subject.expired?.should be_false + end + end + + context "when the expiration date is not set" do + before {subject.stub(:expires => nil)} + it 'should not be expired' do + subject.expired?.should be_false + end + end + + end end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb deleted file mode 100644 index 7a0856ba..00000000 --- a/spec/models/notification_spec.rb +++ /dev/null @@ -1,200 +0,0 @@ -require 'spec_helper' - -describe Message do - - before do - @entity1 = FactoryGirl.create(:user) - @entity2 = FactoryGirl.create(:user) - @entity3 = FactoryGirl.create(:user) - end - - it "should notify one user" do - @entity1.notify("Subject", "Body") - - #Check getting ALL receipts - @entity1.mailbox.receipts.size.should==1 - receipt = @entity1.mailbox.receipts.first - notification = receipt.notification - notification.subject.should=="Subject" - notification.body.should=="Body" - - #Check getting NOTIFICATION receipts only - @entity1.mailbox.notifications.size.should==1 - notification = @entity1.mailbox.notifications.first - notification.subject.should=="Subject" - notification.body.should=="Body" - end - - it "should be unread by default" do - @entity1.notify("Subject", "Body") - @entity1.mailbox.receipts.size.should==1 - notification = @entity1.mailbox.receipts.first.notification - notification.should be_is_unread(@entity1) - end - - it "should be able to marked as read" do - @entity1.notify("Subject", "Body") - @entity1.mailbox.receipts.size.should==1 - notification = @entity1.mailbox.receipts.first.notification - notification.mark_as_read(@entity1) - notification.should be_is_read(@entity1) - end - - it "should notify several users" do - recipients = Set.new [@entity1, @entity2, @entity3] - Notification.notify_all(recipients, "Subject", "Body") - - #Check getting ALL receipts - @entity1.mailbox.receipts.size.should==1 - receipt = @entity1.mailbox.receipts.first - notification = receipt.notification - notification.subject.should=="Subject" - notification.body.should=="Body" - @entity2.mailbox.receipts.size.should==1 - receipt = @entity2.mailbox.receipts.first - notification = receipt.notification - notification.subject.should=="Subject" - notification.body.should=="Body" - @entity3.mailbox.receipts.size.should==1 - receipt = @entity3.mailbox.receipts.first - notification = receipt.notification - notification.subject.should=="Subject" - notification.body.should=="Body" - - #Check getting NOTIFICATION receipts only - @entity1.mailbox.notifications.size.should==1 - notification = @entity1.mailbox.notifications.first - notification.subject.should=="Subject" - notification.body.should=="Body" - @entity2.mailbox.notifications.size.should==1 - notification = @entity2.mailbox.notifications.first - notification.subject.should=="Subject" - notification.body.should=="Body" - @entity3.mailbox.notifications.size.should==1 - notification = @entity3.mailbox.notifications.first - notification.subject.should=="Subject" - notification.body.should=="Body" - - end - - it "should notify a single recipient" do - Notification.notify_all(@entity1, "Subject", "Body") - - #Check getting ALL receipts - @entity1.mailbox.receipts.size.should==1 - receipt = @entity1.mailbox.receipts.first - notification = receipt.notification - notification.subject.should=="Subject" - notification.body.should=="Body" - - #Check getting NOTIFICATION receipts only - @entity1.mailbox.notifications.size.should==1 - notification = @entity1.mailbox.notifications.first - notification.subject.should=="Subject" - notification.body.should=="Body" - end - - describe "#expire" do - subject { Notification.new } - - describe "when the notification is already expired" do - before do - subject.stub(:expired? => true) - end - it 'should not update the expires attribute' do - subject.should_not_receive :expires= - subject.should_not_receive :save - subject.expire - end - end - - describe "when the notification is not expired" do - let(:now) { Time.now } - let(:one_second_ago) { now - 1.second } - before do - Time.stub(:now => now) - subject.stub(:expired? => false) - end - it 'should update the expires attribute' do - subject.should_receive(:expires=).with(one_second_ago) - subject.expire - end - it 'should not save the record' do - subject.should_not_receive :save - subject.expire - end - end - - end - - describe "#expire!" do - subject { Notification.new } - - describe "when the notification is already expired" do - before do - subject.stub(:expired? => true) - end - it 'should not call expire' do - subject.should_not_receive :expire - subject.should_not_receive :save - subject.expire! - end - end - - describe "when the notification is not expired" do - let(:now) { Time.now } - let(:one_second_ago) { now - 1.second } - before do - Time.stub(:now => now) - subject.stub(:expired? => false) - end - it 'should call expire' do - subject.should_receive(:expire) - subject.expire! - end - it 'should save the record' do - subject.should_receive :save - subject.expire! - end - end - - end - - describe "#expired?" do - subject { Notification.new } - context "when the expiration date is in the past" do - before { subject.stub(:expires => Time.now - 1.second) } - it 'should be expired' do - subject.expired?.should be_true - end - end - - context "when the expiration date is now" do - before { - time = Time.now - Time.stub(:now => time) - subject.stub(:expires => time) - } - - it 'should not be expired' do - subject.expired?.should be_false - end - end - - context "when the expiration date is in the future" do - before { subject.stub(:expires => Time.now + 1.second) } - it 'should not be expired' do - subject.expired?.should be_false - end - end - - context "when the expiration date is not set" do - before {subject.stub(:expires => nil)} - it 'should not be expired' do - subject.expired?.should be_false - end - end - - end - -end diff --git a/spec/models/receipt_spec.rb b/spec/models/receipt_spec.rb index 4f31c28a..b08d04e4 100644 --- a/spec/models/receipt_spec.rb +++ b/spec/models/receipt_spec.rb @@ -9,7 +9,7 @@ end it "should belong to a message" do - assert @mail1.notification + assert @mail1.message end it "should belong to a conversation" do