From cfc9f36b5ea5e2ec16989ea4df07314456c2476e Mon Sep 17 00:00:00 2001 From: Toks Date: Thu, 29 Aug 2013 23:33:09 -0400 Subject: [PATCH] fixes #1461 --- app/models/post.rb | 25 ++++++++++++--- app/views/posts/partials/show/_edit.html.erb | 3 ++ test/unit/post_test.rb | 32 ++++++++++++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 6d8a78a41..bfbdf7f9e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,13 +3,13 @@ class Post < ActiveRecord::Base class DisapprovalError < Exception ; end class SearchError < Exception ; end - attr_accessor :old_tag_string, :old_parent_id, :has_constraints, :disable_versioning + attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning after_destroy :delete_files after_destroy :delete_remote_files after_save :create_version after_save :update_parent_on_save after_save :apply_post_metatags, :on => :create - before_save :merge_old_tags + before_save :merge_old_changes before_save :normalize_tags before_save :create_tags before_save :update_tag_post_counts @@ -34,7 +34,7 @@ class Post < ActiveRecord::Base has_many :favorites validates_uniqueness_of :md5 validate :post_is_not_its_own_parent - attr_accessible :source, :rating, :tag_string, :old_tag_string, :last_noted_at, :parent_id, :as => [:member, :builder, :gold, :platinum, :contributor, :janitor, :moderator, :admin, :default] + attr_accessible :source, :rating, :tag_string, :old_tag_string, :old_parent_id, :old_source, :old_rating, :last_noted_at, :parent_id, :as => [:member, :builder, :gold, :platinum, :contributor, :janitor, :moderator, :admin, :default] attr_accessible :is_rating_locked, :is_note_locked, :as => [:builder, :contributor, :janitor, :moderator, :admin] attr_accessible :is_status_locked, :as => [:admin] @@ -364,7 +364,7 @@ class Post < ActiveRecord::Base end end - def merge_old_tags + def merge_old_changes if old_tag_string # If someone else committed changes to this post before we did, # then try to merge the tag changes together. @@ -373,6 +373,23 @@ class Post < ActiveRecord::Base old_tags = Tag.scan_tags(old_tag_string) set_tag_string(((current_tags + new_tags) - old_tags + (current_tags & new_tags)).uniq.sort.join(" ")) end + + if old_parent_id == "" + old_parent_id = nil + else + old_parent_id = old_parent_id.to_i + end + if old_parent_id == parent_id + self.parent_id = parent_id_was + end + + if old_source == source.to_s + self.source = source_was + end + + if old_rating == rating + self.rating = rating_was + end end def reset_tag_array_cache diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 84131bc23..59386e619 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -8,6 +8,9 @@ <%= hidden_field_tag :tags_query, params[:tags] %> <%= hidden_field_tag :pool_id, params[:pool_id] %> <%= f.hidden_field :old_tag_string, :value => post.tag_string %> + <%= f.hidden_field :old_parent_id, :value => post.parent_id %> + <%= f.hidden_field :old_source, :value => post.source %> + <%= f.hidden_field :old_rating, :value => post.rating %>
<% if post.is_rating_locked? %> diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index ba6ecd099..cb698d033 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -655,7 +655,7 @@ class PostTest < ActiveSupport::TestCase assert_equal(1, new_post.tag_count) end - should "merge any changes that were made after loading the initial set of tags part 1" do + should "merge any tag changes that were made after loading the initial set of tags part 1" do post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc") # user a adds @@ -675,7 +675,7 @@ class PostTest < ActiveSupport::TestCase assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort) end - should "merge any changes that were made after loading the initial set of tags part 2" do + should "merge any tag changes that were made after loading the initial set of tags part 2" do # This is the same as part 1, only the order of operations is reversed. # The results should be the same. @@ -697,6 +697,34 @@ class PostTest < ActiveSupport::TestCase final_post = Post.find(post.id) assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort) end + + should "merge any parent, source, and rating changes that were made after loading the initial set" do + post = FactoryGirl.create(:post, :parent => nil, :source => nil, :rating => "q") + parent_post = FactoryGirl.create(:post) + + # user a changes rating to safe, adds parent + post_edited_by_user_a = Post.find(post.id) + post_edited_by_user_a.old_parent_id = nil + post_edited_by_user_a.old_source = nil + post_edited_by_user_a.old_rating = "q" + post_edited_by_user_a.parent_id = parent_post.id + post_edited_by_user_a.rating = "s" + post_edited_by_user_a.save + + # user b adds source + post_edited_by_user_b = Post.find(post.id) + post_edited_by_user_b.old_parent_id = nil + post_edited_by_user_b.old_source = nil + post_edited_by_user_b.old_rating = "q" + post_edited_by_user_b.source = "http://example.com" + post_edited_by_user_b.save + + # final post should be rated safe and have the set parent and source + final_post = Post.find(post.id) + assert_equal(parent_post.id, final_post.parent_id) + assert_equal("http://example.com", final_post.source) + assert_equal("s", final_post.rating) + end end context "that has been tagged with a metatag" do