From 809ee8a678ead8bfb7d7b33d33e0ddde86f07993 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Mon, 28 Feb 2011 10:24:42 -0800 Subject: [PATCH] Started notifications refactor --- app/models/jobs/mail_request_acceptance.rb | 2 +- app/models/jobs/mail_request_received.rb | 2 +- app/models/notification.rb | 13 +------- app/models/notifications/also_commented.rb | 5 ++++ app/models/notifications/comment_on_post.rb | 5 ++++ app/models/notifications/mentioned.rb | 5 ++++ app/models/notifications/new_request.rb | 5 ++++ app/models/notifications/request_accepted.rb | 5 ++++ ...0130072907_notification_multiple_people.rb | 16 ++++------ .../20110228180709_notification_subclasses.rb | 30 +++++++++++++++++++ 10 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 app/models/notifications/also_commented.rb create mode 100644 app/models/notifications/comment_on_post.rb create mode 100644 app/models/notifications/mentioned.rb create mode 100644 app/models/notifications/new_request.rb create mode 100644 app/models/notifications/request_accepted.rb create mode 100644 db/migrate/20110228180709_notification_subclasses.rb diff --git a/app/models/jobs/mail_request_acceptance.rb b/app/models/jobs/mail_request_acceptance.rb index ce1d3667e..22840356d 100644 --- a/app/models/jobs/mail_request_acceptance.rb +++ b/app/models/jobs/mail_request_acceptance.rb @@ -6,7 +6,7 @@ module Job class MailRequestAcceptance < Base @queue = :mail - def self.perform_delegate(recipient_id, sender_id) + def self.perform_delegate(recipient_id, sender_id, target_id) Notifier.request_accepted(recipient_id, sender_id).deliver end end diff --git a/app/models/jobs/mail_request_received.rb b/app/models/jobs/mail_request_received.rb index d6c77878b..3384b71f9 100644 --- a/app/models/jobs/mail_request_received.rb +++ b/app/models/jobs/mail_request_received.rb @@ -6,7 +6,7 @@ module Job class MailRequestReceived < Base @queue = :mail - def self.perform_delegate(recipient_id, sender_id) + def self.perform_delegate(recipient_id, sender_id, target_id) Notifier.new_request(recipient_id, sender_id).deliver end end diff --git a/app/models/notification.rb b/app/models/notification.rb index 29222b9f0..14bc6bb7a 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -31,18 +31,7 @@ class Notification < ActiveRecord::Base end def email_the_user(target, actor) - case self.action - when "new_request" - self.recipient.mail(Job::MailRequestReceived, self.recipient_id, actor.id) - when "request_accepted" - self.recipient.mail(Job::MailRequestAcceptance, self.recipient_id, actor.id) - when "comment_on_post" - self.recipient.mail(Job::MailCommentOnPost, self.recipient_id, actor.id, target.id) - when "also_commented" - self.recipient.mail(Job::MailAlsoCommented, self.recipient_id, actor.id, target.id) - when "mentioned" - self.recipient.mail(Job::MailMentioned, self.recipient_id, actor.id, target.id) - end + self.recipient.mail(self.mail_job, self.recipient_id, actor.id, target.id) end private diff --git a/app/models/notifications/also_commented.rb b/app/models/notifications/also_commented.rb new file mode 100644 index 000000000..16d568013 --- /dev/null +++ b/app/models/notifications/also_commented.rb @@ -0,0 +1,5 @@ +class Notifications::AlsoCommented < Notification + def mail_job + Job::MailAlsoCommented + end +end diff --git a/app/models/notifications/comment_on_post.rb b/app/models/notifications/comment_on_post.rb new file mode 100644 index 000000000..cbadc7813 --- /dev/null +++ b/app/models/notifications/comment_on_post.rb @@ -0,0 +1,5 @@ +class Notifications::CommentOnPost < Notification + def mail_job + Job::MailCommentOnPost + end +end diff --git a/app/models/notifications/mentioned.rb b/app/models/notifications/mentioned.rb new file mode 100644 index 000000000..471b3f028 --- /dev/null +++ b/app/models/notifications/mentioned.rb @@ -0,0 +1,5 @@ +class Notifications::Mentioned < Notification + def mail_job + Job::MailMentioned + end +end diff --git a/app/models/notifications/new_request.rb b/app/models/notifications/new_request.rb new file mode 100644 index 000000000..3a5ed14f8 --- /dev/null +++ b/app/models/notifications/new_request.rb @@ -0,0 +1,5 @@ +class Notifications::NewRequest < Notification + def mail_job + Job::MailRequestReceived + end +end diff --git a/app/models/notifications/request_accepted.rb b/app/models/notifications/request_accepted.rb new file mode 100644 index 000000000..0ed74560c --- /dev/null +++ b/app/models/notifications/request_accepted.rb @@ -0,0 +1,5 @@ +class Notifications::RequestAccepted < Notification + def mail_job + Job::MailRequestAcceptance + end +end diff --git a/db/migrate/20110130072907_notification_multiple_people.rb b/db/migrate/20110130072907_notification_multiple_people.rb index f0322a96d..4e4259636 100644 --- a/db/migrate/20110130072907_notification_multiple_people.rb +++ b/db/migrate/20110130072907_notification_multiple_people.rb @@ -5,7 +5,7 @@ class NotificationMultiplePeople < ActiveRecord::Migration t.integer :person_id t.timestamps end - + add_index :notification_actors, :notification_id add_index :notification_actors, [:notification_id, :person_id] , :unique => true add_index :notification_actors, :person_id ## if i am not mistaken we don't need this one because we won't query person.notifications @@ -14,15 +14,15 @@ class NotificationMultiplePeople < ActiveRecord::Migration execute "INSERT INTO notification_actors (notification_id, person_id) " + " SELECT id , actor_id " + " FROM notifications" - + #update the notifications to reference the post execute "UPDATE notifications, comments " + "SET notifications.target_id = comments.post_id, " + - "target_type = 'Post' " + + "target_type = 'Post' " + "WHERE (notifications.target_id = comments.id " + "AND (notifications.action = 'comment_on_post' " + "OR notifications.action = 'also_commented'))" - + #select all the notifications to keep execute "CREATE TEMPORARY TABLE keep_table " + "(SELECT id as keep_id, actor_id , target_type , target_id , recipient_id , action " + @@ -49,7 +49,7 @@ class NotificationMultiplePeople < ActiveRecord::Migration #delete all the notifications that need to be deleted execute "DELETE notifications.* " + - "FROM notifications, keep_delete " + + "FROM notifications, keep_delete " + "WHERE notifications.id != keep_delete.keep_id AND "+ "notifications.target_type = keep_delete.target_type AND "+ "notifications.target_id = keep_delete.target_id AND "+ @@ -62,10 +62,6 @@ class NotificationMultiplePeople < ActiveRecord::Migration end def self.down - remove_index :notification_actors, :notification_id - remove_index :notification_actors, [:notification_id, :person_id] - remove_index :notification_actors, :person_id - - drop_table :notification_actors + raise ActiveRecord::IrreversibleMigration.new end end diff --git a/db/migrate/20110228180709_notification_subclasses.rb b/db/migrate/20110228180709_notification_subclasses.rb new file mode 100644 index 000000000..96f2d04fc --- /dev/null +++ b/db/migrate/20110228180709_notification_subclasses.rb @@ -0,0 +1,30 @@ +class NotificationSubclasses < ActiveRecord::Migration + def self.up + add_column :notifications, :type, :string, :null => :false + {:new_request => 'Notifications::NewRequest', + :request_accepted => 'Notifications::RequestAccepted', + :comment_on_post => 'Notifications::CommentOnPost', + :also_commented => 'Notifications::AlsoCommented', + :mentioned => 'Notifications::Mentioned' + }.each_pair do |key, value| + execute("UPDATE notifications + set type = #{value} + where action = #{key.to_s}") + end + remove_column :notifications, :action + end + + def self.down + add_column :notifications, :action, :string + {:new_request => 'Notifications::NewRequest', + :request_accepted => 'Notifications::RequestAccepted', + :comment_on_post => 'Notifications::CommentOnPost', + :also_commented => 'Notifications::AlsoCommented', + :mentioned => 'Notifications::Mentioned' + }.each_pair do |key, value| + execute("UPDATE notifications + set action = #{key.to_s} + where type = #{value}") + remove_column :notifications, :type + end +end