This commit is contained in:
Toks
2013-08-29 23:33:09 -04:00
parent fedb398720
commit cfc9f36b5e
3 changed files with 54 additions and 6 deletions

View File

@@ -3,13 +3,13 @@ class Post < ActiveRecord::Base
class DisapprovalError < Exception ; end class DisapprovalError < Exception ; end
class SearchError < 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_files
after_destroy :delete_remote_files after_destroy :delete_remote_files
after_save :create_version after_save :create_version
after_save :update_parent_on_save after_save :update_parent_on_save
after_save :apply_post_metatags, :on => :create after_save :apply_post_metatags, :on => :create
before_save :merge_old_tags before_save :merge_old_changes
before_save :normalize_tags before_save :normalize_tags
before_save :create_tags before_save :create_tags
before_save :update_tag_post_counts before_save :update_tag_post_counts
@@ -34,7 +34,7 @@ class Post < ActiveRecord::Base
has_many :favorites has_many :favorites
validates_uniqueness_of :md5 validates_uniqueness_of :md5
validate :post_is_not_its_own_parent 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_rating_locked, :is_note_locked, :as => [:builder, :contributor, :janitor, :moderator, :admin]
attr_accessible :is_status_locked, :as => [:admin] attr_accessible :is_status_locked, :as => [:admin]
@@ -364,7 +364,7 @@ class Post < ActiveRecord::Base
end end
end end
def merge_old_tags def merge_old_changes
if old_tag_string if old_tag_string
# If someone else committed changes to this post before we did, # If someone else committed changes to this post before we did,
# then try to merge the tag changes together. # then try to merge the tag changes together.
@@ -373,6 +373,23 @@ class Post < ActiveRecord::Base
old_tags = Tag.scan_tags(old_tag_string) old_tags = Tag.scan_tags(old_tag_string)
set_tag_string(((current_tags + new_tags) - old_tags + (current_tags & new_tags)).uniq.sort.join(" ")) set_tag_string(((current_tags + new_tags) - old_tags + (current_tags & new_tags)).uniq.sort.join(" "))
end 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 end
def reset_tag_array_cache def reset_tag_array_cache

View File

@@ -8,6 +8,9 @@
<%= hidden_field_tag :tags_query, params[:tags] %> <%= hidden_field_tag :tags_query, params[:tags] %>
<%= hidden_field_tag :pool_id, params[:pool_id] %> <%= hidden_field_tag :pool_id, params[:pool_id] %>
<%= f.hidden_field :old_tag_string, :value => post.tag_string %> <%= 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 %>
<div class="input"> <div class="input">
<% if post.is_rating_locked? %> <% if post.is_rating_locked? %>

View File

@@ -655,7 +655,7 @@ class PostTest < ActiveSupport::TestCase
assert_equal(1, new_post.tag_count) assert_equal(1, new_post.tag_count)
end 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") post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc")
# user a adds <ddd> # user a adds <ddd>
@@ -675,7 +675,7 @@ class PostTest < ActiveSupport::TestCase
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort) assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort)
end 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. # This is the same as part 1, only the order of operations is reversed.
# The results should be the same. # The results should be the same.
@@ -697,6 +697,34 @@ class PostTest < ActiveSupport::TestCase
final_post = Post.find(post.id) final_post = Post.find(post.id)
assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort) assert_equal(%w(aaa bbb ddd eee), Tag.scan_tags(final_post.tag_string).sort)
end 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 end
context "that has been tagged with a metatag" do context "that has been tagged with a metatag" do