Skip to content

AO3-4122 Prevent duplicate translation languages for admin posts - #6003

Open
pmonfort wants to merge 2 commits into
otwcode:masterfrom
pmonfort:AO3-4122
Open

AO3-4122 Prevent duplicate translation languages for admin posts#6003
pmonfort wants to merge 2 commits into
otwcode:masterfrom
pmonfort:AO3-4122

Conversation

@pmonfort

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-4122

Purpose

Adds a validation so an admin post can't be saved as a translation of another post when that post already has a translation in the same language. The error is reported on the translated post field, alongside the existing "cannot be same language as original post" and "cannot be posted before the original post" errors.

Testing Instructions

  1. Log in as an admin with the translation role.
  2. Post a news post in English.
  3. Go to Post AO3 News, choose a language (e.g. Deutsch), ID in "Translation of", and post it.
  4. Repeat step 3 with the same language and the same "Tran
  5. Expected: the post is not saved and the error "Translated post already has a translation in this language" is shown.
  6. Repeat step 3 with a different language: the translation is saved.
  7. Edit the translation from step 3 (change the title only) and save: no error.

Credit

Pablo Monfort (he/him)

Comment thread app/models/admin_post.rb
Comment on lines +37 to +42
# rubocop:disable Rails/UniqueValidationWithoutIndex
validates :translated_post_id,
uniqueness: { scope: :language_id,
message: :translation_already_exists },
allow_nil: true
# rubocop:enable Rails/UniqueValidationWithoutIndex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled this cop instead of adding a unique index on (translated_post_id, language_id). Production likely has duplicates already (that's what this issue is about), so the index would need a data cleanup first, and that felt out of scope here. The validation itself runs the same query either way and uses the existing index on translated_post_id.

end
end

context "when the original post has a draft translation in the language" do

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue only mentions published translations, but this implementation also blocks the case where the existing translation is still a draft. Drafts are shared between all admins, otherwise a second translation in the same language could be posted while the draft is pending.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants