FIX: remove latest empty revision

This commit is contained in:
Régis Hanol 2015-05-29 20:08:39 +02:00
parent 80a108e3cf
commit 477f352e8f
2 changed files with 51 additions and 20 deletions

View File

@ -91,7 +91,7 @@ class PostRevisor
@fields[:category_id] = @fields[:category_id].to_i if @fields.has_key?(:category_id) @fields[:category_id] = @fields[:category_id].to_i if @fields.has_key?(:category_id)
# always reset edit_reason unless provided # always reset edit_reason unless provided
@fields[:edit_reason] = nil unless @fields.has_key?(:edit_reason) @fields[:edit_reason] = nil unless @fields[:edit_reason].present?
return false unless should_revise? return false unless should_revise?
@ -199,17 +199,18 @@ class PostRevisor
create_or_update_revision create_or_update_revision
end end
def update_post USER_ACTIONS_TO_REMOVE ||= [UserAction::NEW_TOPIC, UserAction::REPLY, UserAction::RESPONSE]
prev_owner, new_owner = nil, nil
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id def update_post
prev_owner = User.find_by_id(@post.user_id) if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id
new_owner = User.find_by_id(@fields["user_id"]) prev_owner = User.find(@post.user_id)
new_owner = User.find(@fields["user_id"])
UserAction.where( target_post_id: @post.id,
user_id: prev_owner.id,
action_type: [UserAction::NEW_TOPIC, UserAction::REPLY, UserAction::RESPONSE] )
.find_each { |ua| ua.destroy }
# UserActionObserver will create new UserAction records for the new owner # UserActionObserver will create new UserAction records for the new owner
UserAction.where(target_post_id: @post.id)
.where(user_id: prev_owner.id)
.where(action_type: USER_ACTIONS_TO_REMOVE)
.destroy_all
end end
POST_TRACKED_FIELDS.each do |field| POST_TRACKED_FIELDS.each do |field|
@ -229,17 +230,20 @@ class PostRevisor
# post owner changed # post owner changed
if prev_owner && new_owner && prev_owner != new_owner if prev_owner && new_owner && prev_owner != new_owner
likes = UserAction.where( target_post_id: @post.id, likes = UserAction.where(target_post_id: @post.id)
user_id: prev_owner.id, .where(user_id: prev_owner.id)
action_type: UserAction::WAS_LIKED ).update_all(user_id: new_owner.id) .where(action_type: UserAction::WAS_LIKED)
.update_all(user_id: new_owner.id)
prev_owner.user_stat.post_count -= 1 prev_owner.user_stat.post_count -= 1
prev_owner.user_stat.topic_count -= 1 if @post.is_first_post? prev_owner.user_stat.topic_count -= 1 if @post.is_first_post?
prev_owner.user_stat.likes_received -= likes prev_owner.user_stat.likes_received -= likes
prev_owner.user_stat.update_topic_reply_count prev_owner.user_stat.update_topic_reply_count
if @post.created_at == prev_owner.user_stat.first_post_created_at if @post.created_at == prev_owner.user_stat.first_post_created_at
prev_owner.user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at) prev_owner.user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at)
end end
prev_owner.user_stat.save prev_owner.user_stat.save
new_owner.user_stat.post_count += 1 new_owner.user_stat.post_count += 1
@ -302,14 +306,26 @@ class PostRevisor
modifications = post_changes.merge(@topic_changes.diff) modifications = post_changes.merge(@topic_changes.diff)
modifications.each_key do |field| modifications.each_key do |field|
if revision.modifications.has_key?(field) if revision.modifications.has_key?(field)
old_value = revision.modifications[field][0] old_value = revision.modifications[field][0].to_s
new_value = modifications[field][1] new_value = modifications[field][1].to_s
revision.modifications[field] = [old_value, new_value] if old_value != new_value
revision.modifications[field] = [old_value, new_value]
else
revision.modifications.delete(field)
end
else else
revision.modifications[field] = modifications[field] revision.modifications[field] = modifications[field]
end end
end end
revision.save if modifications.length # should probably do this before saving the post!
if revision.modifications.empty?
revision.destroy
@post.version -= 1
@post.public_version -= 1
@post.save
else
revision.save
end
end end
def post_changes def post_changes
@ -410,4 +426,3 @@ class PostRevisor
end end
end end

View File

@ -5,7 +5,7 @@ describe PostRevisor do
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
let(:newuser) { Fabricate(:newuser) } let(:newuser) { Fabricate(:newuser) }
let(:post_args) { {user: newuser, topic: topic} } let(:post_args) { { user: newuser, topic: topic } }
context 'TopicChanges' do context 'TopicChanges' do
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
@ -74,7 +74,8 @@ describe PostRevisor do
describe 'ninja editing' do describe 'ninja editing' do
it 'correctly applies edits' do it 'correctly applies edits' do
SiteSetting.ninja_edit_window = 1.minute.to_i SiteSetting.stubs(:ninja_edit_window).returns(1.minute)
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds) subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds)
post.reload post.reload
@ -84,6 +85,21 @@ describe PostRevisor do
expect(post.last_version_at).to eq(first_version_at) expect(post.last_version_at).to eq(first_version_at)
expect(subject.category_changed).to be_blank expect(subject.category_changed).to be_blank
end end
it "doesn't create a new version" do
SiteSetting.stubs(:ninja_edit_window).returns(1.minute)
# making a revision
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.ninja_edit_window + 1.seconds)
# "roll back"
subject.revise!(post.user, { raw: 'Hello world' }, revised_at: post.updated_at + SiteSetting.ninja_edit_window + 2.seconds)
post.reload
expect(post.version).to eq(1)
expect(post.public_version).to eq(1)
expect(post.revisions.size).to eq(0)
end
end end
describe 'revision much later' do describe 'revision much later' do