discourse-translator/app/models/discourse_translator/topic_translation.rb
Natalie Tay 7fc45d5be8
DEV: Move translation custom fields into their own tables (#201)
Currently, `Topic` and `Post` have detected_language and translations in custom fields, e.g.
```
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]
post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
topic.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
```

We are moving this into 4 tables/models
```
post has_one :content_locale, class_name: "DiscourseTranslator::PostLocale"
post has_many :translations, class_name: "DiscourseTranslator::PostTranslation"
topic has_one :content_locale, class_name: "DiscourseTranslator::TopicLocale"
topic has_many :translations, class_name: "DiscourseTranslator::TopicTranslation"
```

Since there are a lot of duplicates, this is implemented on the `Post` and `Topic` using a `Concern`, and any future translatable content can inherit this concern.

This PR also gets rid of the previous N+1 which happens when determining if the 🌐 translate button should appear for each post.
2025-02-10 12:29:07 +08:00

34 lines
925 B
Ruby

# frozen_string_literal: true
module DiscourseTranslator
class TopicTranslation < ActiveRecord::Base
self.table_name = "discourse_translator_topic_translations"
belongs_to :topic
validates :topic_id, presence: true
validates :locale, presence: true
validates :translation, presence: true
validates :locale, uniqueness: { scope: :topic_id }
def self.translation_for(topic_id, locale)
find_by(topic_id: topic_id, locale: locale)&.translation
end
end
end
# == Schema Information
#
# Table name: discourse_translator_topic_translations
#
# id :bigint not null, primary key
# topic_id :integer not null
# locale :string not null
# translation :text not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# idx_on_topic_id_locale_70b2f83213 (topic_id,locale) UNIQUE
#