Skip to content

Commit 4febe61

Browse files
committed
DEV: Move translation custom fields into their own tables
1 parent f7d6ca6 commit 4febe61

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class PostLanguage < ActiveRecord::Base
5+
self.table_name = "discourse_translator_post_languages"
6+
7+
belongs_to :post
8+
9+
validates :post_id, presence: true, uniqueness: true
10+
validates :detected_locale, presence: true
11+
end
12+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class PostTranslation < ActiveRecord::Base
5+
self.table_name = "discourse_translator_post_translations"
6+
7+
belongs_to :post
8+
9+
validates :post_id, presence: true
10+
validates :locale, presence: true
11+
validates :translation, presence: true
12+
validates :locale, uniqueness: { scope: :post_id }
13+
14+
def self.translation_for(post_id, locale)
15+
find_by(post_id: post_id, locale: locale)&.translation
16+
end
17+
end
18+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class TopicLanguage < ActiveRecord::Base
5+
self.table_name = "discourse_translator_topic_languages"
6+
7+
belongs_to :topic
8+
9+
validates :topic_id, presence: true, uniqueness: true
10+
validates :detected_locale, presence: true
11+
end
12+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
class TopicTranslation < ActiveRecord::Base
5+
self.table_name = "discourse_translator_topic_translations"
6+
7+
belongs_to :topic
8+
9+
validates :topic_id, presence: true
10+
validates :locale, presence: true
11+
validates :translation, presence: true
12+
validates :locale, uniqueness: { scope: :topic_id }
13+
14+
def self.translation_for(topic_id, locale)
15+
find_by(topic_id: topic_id, locale: locale)&.translation
16+
end
17+
end
18+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../db/migrate/20210922193500_move_translations_custom_fields_to_table"
4+
5+
RSpec.describe "MoveTranslationsCustomFieldsToTable" do
6+
fab!(:topic) { Fabricate(:topic) }
7+
fab!(:post) { Fabricate(:post) }
8+
9+
before do
10+
TopicCustomField.create!(topic_id: topic.id, name: "post_detected_lang", value: "en")
11+
12+
PostCustomField.create!(post_id: post.id, name: "post_detected_lang", value: "es")
13+
14+
TopicCustomField.create!(
15+
topic_id: topic.id,
16+
name: "translated_text",
17+
value: { fr: "Bonjour", es: "Hola" }.to_json,
18+
)
19+
20+
PostCustomField.create!(
21+
post_id: post.id,
22+
name: "translated_text",
23+
value: { en: "Hello", fr: "Salut" }.to_json,
24+
)
25+
end
26+
27+
it "correctly migrates custom fields to new tables" do
28+
MoveTranslationsCustomFieldsToTable.new.up
29+
30+
# Check languages
31+
topic_lang = DiscourseTranslatorTopicLanguage.find_by(topic_id: topic.id)
32+
expect(topic_lang.detected_locale).to eq("en")
33+
34+
post_lang = DiscourseTranslatorPostLanguage.find_by(post_id: post.id)
35+
expect(post_lang.detected_locale).to eq("es")
36+
37+
# Check translations
38+
topic_translations = DiscourseTranslatorTopicTranslation.where(topic_id: topic.id)
39+
expect(topic_translations.count).to eq(2)
40+
expect(topic_translations.pluck(:locale)).to contain_exactly("fr", "es")
41+
expect(topic_translations.find_by(locale: "fr").translation).to eq("Bonjour")
42+
43+
post_translations = DiscourseTranslatorPostTranslation.where(post_id: post.id)
44+
expect(post_translations.count).to eq(2)
45+
expect(post_translations.pluck(:locale)).to contain_exactly("en", "fr")
46+
expect(post_translations.find_by(locale: "en").translation).to eq("Hello")
47+
end
48+
49+
it "properly cleans up data on rollback" do
50+
MoveTranslationsCustomFieldsToTable.new.up
51+
MoveTranslationsCustomFieldsToTable.new.down
52+
53+
expect(DiscourseTranslatorTopicLanguage.count).to eq(0)
54+
expect(DiscourseTranslatorTopicTranslation.count).to eq(0)
55+
expect(DiscourseTranslatorPostLanguage.count).to eq(0)
56+
expect(DiscourseTranslatorPostTranslation.count).to eq(0)
57+
end
58+
end

0 commit comments

Comments
 (0)