FIX: converting topic to message for a second time was broken

This commit is contained in:
Arpit Jalan 2018-03-08 17:59:04 +05:30
parent 0c2be8b775
commit 4b23634092
2 changed files with 14 additions and 2 deletions

View File

@ -49,7 +49,7 @@ class TopicConverter
@topic.category_id = nil
@topic.archetype = Archetype.private_message
add_allowed_users
@topic.save
@topic.save!
watch_topic(topic)
end
@topic
@ -77,7 +77,7 @@ class TopicConverter
user.user_stat.post_count -= 1
user.user_stat.save!
end
@topic.topic_allowed_users.build(user_id: @user.id)
@topic.topic_allowed_users.build(user_id: @user.id) unless @topic.topic_allowed_users.where(user_id: @user.id).exists?
# update topics count
@topic.user.user_stat.topic_count -= 1
@topic.user.user_stat.save!

View File

@ -131,5 +131,17 @@ describe TopicConverter do
expect(topic.reload.user.user_stat.post_count).to eq(0)
end
end
context 'when user already exists in topic_allowed_users table' do
before do
topic.topic_allowed_users.create!(user_id: admin.id)
end
it "works" do
private_message = topic.convert_to_private_message(admin)
expect(private_message).to be_valid
expect(topic.archetype).to eq("private_message")
end
end
end
end