diff --git a/app/models/article.rb b/app/models/article.rb index 2e48ea9..9299724 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -51,6 +51,11 @@ class Article < ActiveRecord::Base end end + after_destroy :destroy_activity + def destroy_activity + self.activity.destroy if self.activity + end + xss_terminate :only => [ :name ], :on => 'validation', :with => 'white_list' named_scope :in_category, lambda { |category| diff --git a/db/migrate/20120818030329_remove_action_tracker_with_target_nil.rb b/db/migrate/20120818030329_remove_action_tracker_with_target_nil.rb new file mode 100644 index 0000000..946d8d0 --- /dev/null +++ b/db/migrate/20120818030329_remove_action_tracker_with_target_nil.rb @@ -0,0 +1,14 @@ +class RemoveActionTrackerWithTargetNil < ActiveRecord::Migration + def self.up + select_all("SELECT id FROM action_tracker").each do |tracker| + activity = ActionTracker::Record.find_by_id(tracker['id']) + if activity && activity.target.nil? + activity.destroy + end + end + end + + def self.down + say "this migration can't be reverted" + end +end diff --git a/db/schema.rb b/db/schema.rb index 759728f..a739307 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120813163139) do +ActiveRecord::Schema.define(:version => 20120818030329) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 066b3fb..e2a830f 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -977,9 +977,9 @@ class ArticleTest < ActiveSupport::TestCase assert_equivalent [p1,p2,community], ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).map(&:profile) end - should 'not track action when a published article is removed' do + should 'destroy activity when a published article is removed' do a = create(TinyMceArticle, :profile_id => profile.id) - assert_no_difference ActionTracker::Record, :count do + assert_difference ActionTracker::Record, :count, -1 do a.destroy end end @@ -1115,6 +1115,51 @@ class ArticleTest < ActiveSupport::TestCase assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(article2.activity.id).count end + should 'destroy activity and notifications of friends when destroying an article' do + friend = fast_create(Person) + profile.add_friend(friend) + Article.destroy_all + ActionTracker::Record.destroy_all + ActionTrackerNotification.destroy_all + UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once + article = create(TinyMceArticle, :profile_id => profile.id) + activity = article.activity + + process_delayed_job_queue + assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).count + + assert_difference ActionTrackerNotification, :count, -2 do + article.destroy + end + + assert_raise ActiveRecord::RecordNotFound do + ActionTracker::Record.find(activity.id) + end + end + + should 'destroy action_tracker and notifications when an article is destroyed in a community' do + community = fast_create(Community) + p1 = fast_create(Person) + p2 = fast_create(Person) + community.add_member(p1) + community.add_member(p2) + UserStampSweeper.any_instance.expects(:current_user).returns(p1).at_least_once + + article = create(TinyMceArticle, :profile_id => community.id) + activity = article.activity + + process_delayed_job_queue + assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(activity.id).count + + assert_difference ActionTrackerNotification, :count, -3 do + article.destroy + end + + assert_raise ActiveRecord::RecordNotFound do + ActionTracker::Record.find(activity.id) + end + end + should 'found articles with published date between a range' do start_date = DateTime.parse('2010-07-06') end_date = DateTime.parse('2010-08-02') diff --git a/test/unit/textile_article_test.rb b/test/unit/textile_article_test.rb index a4e858c..95618fb 100644 --- a/test/unit/textile_article_test.rb +++ b/test/unit/textile_article_test.rb @@ -70,20 +70,20 @@ class TextileArticleTest < ActiveSupport::TestCase end end - should 'not notify activity on destroy' do + should 'remove activity after destroying article' do ActionTracker::Record.delete_all a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true - assert_no_difference ActionTracker::Record, :count do + assert_difference ActionTracker::Record, :count, -1 do a.destroy end end - should 'not notify when an article is destroyed' do + should 'remove activity after article is destroyed' do ActionTracker::Record.delete_all a1 = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true a2 = TextileArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true assert_equal 2, ActionTracker::Record.count - assert_no_difference ActionTracker::Record, :count do + assert_difference ActionTracker::Record, :count, -2 do a1.destroy a2.destroy end diff --git a/test/unit/tiny_mce_article_test.rb b/test/unit/tiny_mce_article_test.rb index 3591c1b..5467360 100644 --- a/test/unit/tiny_mce_article_test.rb +++ b/test/unit/tiny_mce_article_test.rb @@ -168,11 +168,11 @@ class TinyMceArticleTest < ActiveSupport::TestCase end end - should 'not notify when an article is destroyed' do + should 'remove activity when an article is destroyed' do ActionTracker::Record.delete_all a1 = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true a2 = TinyMceArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true - assert_no_difference ActionTracker::Record, :count do + assert_difference ActionTracker::Record, :count, -2 do a1.destroy a2.destroy end -- libgit2 0.21.2