From a4e56849942f214f5dc46ba0c8f1a1c76b9d1ea8 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Sun, 16 Dec 2012 20:27:54 -0600 Subject: [PATCH] moderation log: fix tag change summary; fixes issue #43 --- app/models/story.rb | 30 +++++++++++++++++++----------- spec/models/story_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/app/models/story.rb b/app/models/story.rb index 8d40bef..eefb99c 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -138,24 +138,19 @@ class Story < ActiveRecord::Base return end + all_changes = self.changes.merge(self.tagging_changes) + m = Moderation.new m.moderator_user_id = self.editor_user_id m.story_id = self.id - if self.changes["is_expired"] && self.is_expired? + if all_changes["is_expired"] && self.is_expired? m.action = "deleted story" - elsif self.changes["is_expired"] && !self.is_expired? + elsif all_changes["is_expired"] && !self.is_expired? m.action = "undeleted story" else - actions = self.changes.map{|k,v| "changed #{k} from #{v[0].inspect} " << - "to #{v[1].inspect}" } - - if (old_tags = self.tags.map{|t| t.tag }) != self.tags_a - actions.push "changed tags from \"#{old_tags.join(", ")}\" to " << - "\"#{self.tags_a.join(", ")}\"" - end - - m.action = actions.join(", ") + m.action = all_changes.map{|k,v| "changed #{k} from #{v[0].inspect} " << + "to #{v[1].inspect}" }.join(", ") end m.reason = self.moderation_reason @@ -306,6 +301,19 @@ class Story < ActiveRecord::Base end end end + + def tagging_changes + old_tags_a = self.taggings.reject{|tg| tg.new_record? }.map{|tg| + tg.tag.tag }.join(" ") + new_tags_a = self.taggings.reject{|tg| tg.marked_for_destruction? + }.map{|tg| tg.tag.tag }.join(" ") + + if old_tags_a == new_tags_a + {} + else + { "tags" => [ old_tags_a, new_tags_a ] } + end + end def url=(u) # strip out stupid google analytics parameters diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index ef790a5..e9dcfe0 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -112,4 +112,33 @@ describe Story do s.valid? s.url.should == "https://factorable.net/" end + + it "calculates tag changes properly" do + s = Story.make!(:title => "blah", :tags_a => [ "tag1", "tag2" ]) + + s.tags_a = [ "tag2" ] + s.tagging_changes.should == { "tags" => [ "tag1 tag2", "tag2" ] } + end + + it "logs moderations properly" do + mod = User.make!(:username => "mod", :is_moderator => true) + + s = Story.make!(:title => "blah", :tags_a => [ "tag1", "tag2" ], + :description => "desc") + + s.title = "changed title" + s.description = nil + s.tags_a = [ "tag1" ] + + s.editor_user_id = mod.id + s.moderation_reason = "because i hate you" + s.save! + + mod_log = Moderation.last + mod_log.moderator_user_id.should == mod.id + mod_log.story_id.should == s.id + mod_log.reason.should == "because i hate you" + mod_log.action.should match(/title from "blah" to "changed title"/) + mod_log.action.should match(/tags from "tag1 tag2" to "tag1"/) + end end