Skip to content

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

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

pmonfort wants to merge 3 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, the issue predates the existence of drafts, but this is the behavior I'd expect now that we have them. Thanks for including drafts in test coverage!

@sarken sarken left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a little nitpicks. If you want to wait to address it until we hear back from Translation about the error message, that's fine!

end
end

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, the issue predates the existence of drafts, but this is the behavior I'd expect now that we have them. Thanks for including drafts in test coverage!

Comment thread spec/models/admin_post_spec.rb Outdated
end
end

describe "translation language uniqueness" do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this can just be #translated_post_id to match the attribute we're validating. (If there are other validations on this attribute that we want to test at some future date, we can put them under here as well.)

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.

Renamed, thanks! 👍

invalid_permissions: Comment permissions are invalid.
translated_post_id:
must_be_posted_first: cannot be posted before the original post
translation_already_exists: already has a translation in this language

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm making sure this message is clear with Translation and will let you know what they say!

This branch has not been deployed

No deployments
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