diff --git a/app/controllers/tag_aliases_controller.rb b/app/controllers/tag_aliases_controller.rb index 49ddb9f0a..263ccc2d6 100644 --- a/app/controllers/tag_aliases_controller.rb +++ b/app/controllers/tag_aliases_controller.rb @@ -44,9 +44,7 @@ class TagAliasesController < ApplicationController def destroy @tag_alias = TagAlias.find(params[:id]) if @tag_alias.deletable_by?(CurrentUser.user) - @tag_alias.update_column(:status, "deleted") - @tag_alias.clear_all_cache - @tag_alias.destroy + @tag_alias.reject! respond_with(@tag_alias, :location => tag_aliases_path) else access_denied diff --git a/app/controllers/tag_implications_controller.rb b/app/controllers/tag_implications_controller.rb index 0d20a4485..84e4fe181 100644 --- a/app/controllers/tag_implications_controller.rb +++ b/app/controllers/tag_implications_controller.rb @@ -44,7 +44,7 @@ class TagImplicationsController < ApplicationController def destroy @tag_implication = TagImplication.find(params[:id]) if @tag_implication.deletable_by?(CurrentUser.user) - @tag_implication.destroy + @tag_implication.reject! respond_with(@tag_implication) do |format| format.html do flash[:notice] = "Tag implication was deleted" diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index c3f2b4c88..e64f34afc 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -31,6 +31,7 @@ class BulkUpdateRequest < ActiveRecord::Base extend SearchMethods def approve! + update_forum_topic_for_approve AliasAndImplicationImporter.new(script, forum_topic_id, "1").process! update_attribute(:status, "approved") end @@ -70,6 +71,7 @@ class BulkUpdateRequest < ActiveRecord::Base end def reject! + update_forum_topic_for_reject update_attribute(:status, "rejected") end @@ -91,4 +93,25 @@ class BulkUpdateRequest < ActiveRecord::Base errors.add(:base, "Forum topic ID is invalid") end end + + + def update_forum_topic_for_approve + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The bulk update request ##{id} has been approved." + ) + end + end + end + + def update_forum_topic_for_reject + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The bulk update request ##{id} has been rejected." + ) + end + end + end end diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index ad39069a6..483f0b0d6 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -86,6 +86,7 @@ class TagAlias < ActiveRecord::Base clear_all_cache ensure_category_consistency update_posts + update_forum_topic_for_approve update_column(:status, "active") rescue Exception => e update_column(:status, "error: #{e}") @@ -226,4 +227,31 @@ class TagAlias < ActiveRecord::Base def editable_by?(user) deletable_by?(user) end + + def update_forum_topic_for_approve + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The tag alias #{antecedent_name} -> #{consequent_name} has been approved." + ) + end + end + end + + def update_forum_topic_for_reject + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The tag alias #{antecedent_name} -> #{consequent_name} has been rejected." + ) + end + end + end + + def reject! + update_column(:status, "deleted") + clear_all_cache + update_forum_topic_for_reject + destroy + end end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 8c971d6ae..22288d609 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -3,6 +3,7 @@ class TagImplication < ActiveRecord::Base after_save :update_descendant_names_for_parents after_destroy :update_descendant_names_for_parents belongs_to :creator, :class_name => "User" + belongs_to :forum_topic before_validation :initialize_creator, :on => :create before_validation :normalize_names validates_presence_of :creator_id, :antecedent_name, :consequent_name @@ -127,6 +128,7 @@ class TagImplication < ActiveRecord::Base update_posts update_column(:status, "active") update_descendant_names_for_parents + update_forum_topic_for_approve rescue Exception => e update_column(:status, "error: #{e}") end @@ -213,4 +215,29 @@ class TagImplication < ActiveRecord::Base def editable_by?(user) deletable_by?(user) end + + def update_forum_topic_for_approve + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The tag implication #{antecedent_name} -> #{consequent_name} has been approved." + ) + end + end + end + + def update_forum_topic_for_reject + if forum_topic + CurrentUser.scoped(User.admins.first, "127.0.0.1") do + forum_topic.posts.create( + :body => "The tag implication #{antecedent_name} -> #{consequent_name} has been rejected." + ) + end + end + end + + def reject! + update_forum_topic_for_reject + destroy + end end diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 744dc488e..d176cf0d4 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -15,5 +15,27 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase BulkUpdateRequest.create(:title => "abc", :reason => "zzz", :script => "create alias aaa -> bbb") end end + + context "with an associated forum topic" do + setup do + @admin = FactoryGirl.create(:admin_user) + @topic = FactoryGirl.create(:forum_topic) + @req = FactoryGirl.create(:bulk_update_request, :script => "create alias aaa -> bbb", :forum_topic => @topic) + end + + should "update the topic when processed" do + assert_difference("ForumPost.count") do + CurrentUser.scoped(@admin, "127.0.0.1") do + @req.approve! + end + end + end + + should "update the topic when rejected" do + assert_difference("ForumPost.count") do + @req.reject! + end + end + end end end diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 70c13d496..9a6f43649 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -91,5 +91,25 @@ class TagAliasTest < ActiveSupport::TestCase tag2.reload assert_equal(1, tag2.category) end + + context "with an associated forum topic" do + setup do + @admin = FactoryGirl.create(:admin_user) + @topic = FactoryGirl.create(:forum_topic) + @alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic) + end + + should "update the topic when processed" do + assert_difference("ForumPost.count") do + @alias.process! + end + end + + should "update the topic when rejected" do + assert_difference("ForumPost.count") do + @alias.reject! + end + end + end end end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index f92180baf..5a4fc922b 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -151,5 +151,25 @@ class TagImplicationTest < ActiveSupport::TestCase p1.reload assert_equal("aaa bbb ccc xxx yyy", p1.tag_string) end + + context "with an associated forum topic" do + setup do + @admin = FactoryGirl.create(:admin_user) + @topic = FactoryGirl.create(:forum_topic) + @implication = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic) + end + + should "update the topic when processed" do + assert_difference("ForumPost.count") do + @implication.process! + end + end + + should "update the topic when rejected" do + assert_difference("ForumPost.count") do + @implication.reject! + end + end + end end end