From 29f3aef6b6b71a0d7d2c44b912a170ede0fd2efd Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 18 Oct 2016 04:51:34 -0500 Subject: [PATCH] Log all tag alias/implication changes. Creates a mod action any time an alias or implication is changed. This includes creations, edits to pending aliases/implications, deletions, and approvals. Also it logs each status change from pending -> queued -> processing -> approved. Call are changed from `update_column` to `update` so that the create_mod_action callback will run at every point in the lifecycle. --- app/models/tag_alias.rb | 30 +++++++++++++++++++++++++----- app/models/tag_implication.rb | 30 ++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index d7fa41245..f8423395e 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -4,6 +4,7 @@ class TagAlias < ActiveRecord::Base before_save :ensure_tags_exist after_save :clear_all_cache after_destroy :clear_all_cache + after_save :create_mod_action before_validation :initialize_creator, :on => :create before_validation :normalize_names validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|error: .*)\Z/ @@ -106,15 +107,14 @@ class TagAlias < ActiveRecord::Base begin admin = CurrentUser.user || approver || User.admins.first CurrentUser.scoped(admin, "127.0.0.1") do - update_column(:status, "processing") + update({ :status => "processing" }, :as => CurrentUser.role) move_aliases_and_implications move_saved_searches clear_all_cache ensure_category_consistency update_posts update_forum_topic_for_approve if update_topic - update_column(:post_count, consequent_tag.post_count) - update_column(:status, "active") + update({ :status => "active", :post_count => consequent_tag.post_count }, :as => CurrentUser.role) end rescue Exception => e if tries < 5 @@ -124,7 +124,7 @@ class TagAlias < ActiveRecord::Base end update_forum_topic_for_error(e) - update_column(:status, "error: #{e}") + update({ :status => "error: #{e}" }, :as => CurrentUser.role) NewRelic::Agent.notice_error(e, :custom_params => {:tag_alias_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name}) end end @@ -312,7 +312,7 @@ class TagAlias < ActiveRecord::Base end def reject! - update_column(:status, "deleted") + update({ :status => "deleted", }, :as => CurrentUser.role) clear_all_cache update_forum_topic_for_reject destroy @@ -340,4 +340,24 @@ class TagAlias < ActiveRecord::Base execute_sql("UPDATE tag_aliases SET post_count = tags.post_count FROM tags WHERE tags.name = tag_aliases.consequent_name") end end + + def create_mod_action + alias_desc = %Q("tag alias ##{id}":[#{Rails.application.routes.url_helpers.tag_alias_path(self)}] ([[#{antecedent_name}]] -> [[#{consequent_name}]])) + + if id_changed? + ModAction.create(:description => "created #{status} #{alias_desc}.") + else + # format the changes hash more nicely. + change_desc = changes.except(:updated_at).map do |attribute, values| + old, new = values[0], values[1] + if old.nil? + %Q(set #{attribute} to "#{new}") + else + %Q(changed #{attribute} from "#{old}" to "#{new}") + end + end.join(", ") + + ModAction.create(:description => "updated #{alias_desc}: #{change_desc}.") + end + end end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index bf033b0e6..3fca48e97 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -4,6 +4,7 @@ class TagImplication < ActiveRecord::Base before_save :update_descendant_names after_save :update_descendant_names_for_parents after_destroy :update_descendant_names_for_parents + after_save :create_mod_action belongs_to :creator, :class_name => "User" belongs_to :approver, :class_name => "User" belongs_to :forum_topic @@ -57,7 +58,7 @@ class TagImplication < ActiveRecord::Base def update_descendant_names! clear_descendants_cache update_descendant_names - update_column(:descendant_names, descendant_names) + update({ :descendant_names => descendant_names }, :as => CurrentUser.role) end def update_descendant_names_for_parents @@ -139,9 +140,9 @@ class TagImplication < ActiveRecord::Base begin admin = CurrentUser.user || approver || User.admins.first CurrentUser.scoped(admin, "127.0.0.1") do - update_column(:status, "processing") + update({ :status => "processing" }, :as => CurrentUser.role) update_posts - update_column(:status, "active") + update({ :status => "active" }, :as => CurrentUser.role) update_descendant_names_for_parents update_forum_topic_for_approve if update_topic end @@ -153,7 +154,7 @@ class TagImplication < ActiveRecord::Base end update_forum_topic_for_error(e) - update_column(:status, "error: #{e}") + update({ :status => "error: #{e}" }, :as => CurrentUser.role) NewRelic::Agent.notice_error(e, :custom_params => {:tag_implication_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name}) end end @@ -287,7 +288,28 @@ class TagImplication < ActiveRecord::Base end def reject! + update({ :status => "deleted", }, :as => CurrentUser.role) update_forum_topic_for_reject destroy end + + def create_mod_action + implication = %Q("tag implication ##{id}":[#{Rails.application.routes.url_helpers.tag_implication_path(self)}] ([[#{antecedent_name}]] -> [[#{consequent_name}]])) + + if id_changed? + ModAction.create(:description => "created #{status} #{implication}.") + else + # format the changes hash more nicely. + change_desc = changes.except(:updated_at).map do |attribute, values| + old, new = values[0], values[1] + if old.nil? + %Q(set #{attribute} to "#{new}") + else + %Q(changed #{attribute} from "#{old}" to "#{new}") + end + end.join(", ") + + ModAction.create(:description => "updated #{implication}: #{change_desc}.") + end + end end