From 5b932879e2d7b6611eaa040e0d0516d48c806ca8 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Tue, 13 Mar 2012 19:56:58 +0600 Subject: [PATCH] [refs #265] fixed subscribe dublicates --- app/models/activity_feed_observer.rb | 2 +- app/models/issue.rb | 5 +++-- app/models/subscribe.rb | 2 -- .../20120313130930_delete_dublicate_subscribes.rb | 12 ++++++++++++ 4 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20120313130930_delete_dublicate_subscribes.rb diff --git a/app/models/activity_feed_observer.rb b/app/models/activity_feed_observer.rb index 22d7ee954..0cc2c2737 100644 --- a/app/models/activity_feed_observer.rb +++ b/app/models/activity_feed_observer.rb @@ -33,7 +33,7 @@ class ActivityFeedObserver < ActiveRecord::Observer when 'Comment' if record.commentable.class == Issue - subscribes = record.commentable.subscribes.finder_hack + subscribes = record.commentable.subscribes subscribes.each do |subscribe| if record.user_id != subscribe.user_id UserMailer.delay.new_comment_notification(record, subscribe.user) if record.can_notify_on_new_comment?(subscribe) diff --git a/app/models/issue.rb b/app/models/issue.rb index 492faa1cd..a3bd0f2e1 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -73,8 +73,9 @@ class Issue < ActiveRecord::Base def subscribe_users recipients = collect_recipient_ids recipients.each do |recipient_id| - ss = self.subscribes.build(:user_id => recipient_id) - ss.save! + if User.find(recipient_id).notifier.new_comment && !self.subscribes.exists?(:user_id => recipient_id) + ss = self.subscribes.create(:user_id => recipient_id) + end end end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 0ed6e71df..497314fa4 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -4,8 +4,6 @@ class Subscribe < ActiveRecord::Base belongs_to :user belongs_to :project - scope :finder_hack, order('') # FIXME .subscribes - error; .subscribes.finder_hack - success Oo - def commit_subscribe? subscribeable_type == 'Grit::Commit' end diff --git a/db/migrate/20120313130930_delete_dublicate_subscribes.rb b/db/migrate/20120313130930_delete_dublicate_subscribes.rb new file mode 100644 index 000000000..7f1fcc6b9 --- /dev/null +++ b/db/migrate/20120313130930_delete_dublicate_subscribes.rb @@ -0,0 +1,12 @@ +class DeleteDublicateSubscribes < ActiveRecord::Migration + def up + execute <<-SQL + DELETE FROM subscribes s + WHERE s.id NOT IN (SELECT MIN(s1.id) FROM SUBSCRIBES s1 + GROUP BY s1.subscribeable_type, s1.user_id, s1.status, s1.subscribeable_id) + SQL + end + + def down + end +end