Skip to content

Commit f4e0842

Browse files
authored
DEV: Use localizable concerns across post, topic, categories (#34137)
This is the first in the series and helps prepare us for crawler_view. Existing tests should cover the use of these concerns. /t/160415
1 parent c242814 commit f4e0842

File tree

14 files changed

+48
-37
lines changed

14 files changed

+48
-37
lines changed

app/models/category.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Category < ActiveRecord::Base
1616
include CategoryHashtag
1717
include AnonCacheInvalidator
1818
include HasDestroyedWebHook
19+
include Localizable
1920

2021
SLUG_REF_SEPARATOR = ":"
2122
DEFAULT_TEXT_COLORS = %w[FFFFFF 000000]

app/models/category_localization.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class CategoryLocalization < ActiveRecord::Base
4+
include LocaleMatchable
5+
46
belongs_to :category
57

68
validates :locale, presence: true, length: { maximum: 20 }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module LocaleMatchable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
scope :matching_locale,
8+
->(locale) do
9+
regionless_locale = locale.to_s.split("_").first
10+
where("locale LIKE ?", "#{regionless_locale}%")
11+
end
12+
end
13+
end

app/models/concerns/localizable.rb

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 Localizable
4+
extend ActiveSupport::Concern
5+
6+
included { has_many :localizations, class_name: "#{model_name}Localization", dependent: :destroy }
7+
8+
def get_localization(locale = I18n.locale)
9+
locale_str = locale.to_s.sub("-", "_")
10+
11+
# prioritise exact match
12+
if match = localizations.find { |l| l.locale == locale_str }
13+
return match
14+
end
15+
16+
localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
17+
end
18+
end

app/models/post.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Post < ActiveRecord::Base
99
include Searchable
1010
include HasCustomFields
1111
include LimitedEdit
12+
include Localizable
1213

1314
self.ignored_columns = [
1415
"avg_time", # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
@@ -67,8 +68,6 @@ class Post < ActiveRecord::Base
6768

6869
has_many :user_actions, foreign_key: :target_post_id
6970

70-
has_many :post_localizations, dependent: :destroy
71-
7271
belongs_to :image_upload, class_name: "Upload"
7372

7473
has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia"
@@ -1343,17 +1342,6 @@ def in_user_locale?
13431342
LocaleNormalizer.is_same?(locale, I18n.locale)
13441343
end
13451344

1346-
def get_localization(locale = I18n.locale)
1347-
locale_str = locale.to_s.sub("-", "_")
1348-
1349-
# prioritise exact match
1350-
if match = post_localizations.find { |l| l.locale == locale_str }
1351-
return match
1352-
end
1353-
1354-
post_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
1355-
end
1356-
13571345
private
13581346

13591347
def parse_quote_into_arguments(quote)

app/models/post_localization.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class PostLocalization < ActiveRecord::Base
4+
include LocaleMatchable
5+
46
belongs_to :post
57

68
validates :post_version, presence: true

app/models/topic.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class NotAllowed < StandardError
1111
include Trashable
1212
include Searchable
1313
include LimitedEdit
14+
include Localizable
1415
extend Forwardable
1516

1617
EXTERNAL_ID_MAX_LENGTH = 50
@@ -31,8 +32,6 @@ class NotAllowed < StandardError
3132

3233
attr_accessor :allowed_user_ids, :allowed_group_ids, :tags_changed, :includes_destination_category
3334

34-
has_many :topic_localizations, dependent: :destroy
35-
3635
def self.max_fancy_title_length
3736
400
3837
end
@@ -2137,24 +2136,13 @@ def self.editable_custom_fields(guardian)
21372136
end
21382137

21392138
def has_localization?(locale = I18n.locale)
2140-
topic_localizations.exists?(locale: locale.to_s.sub("-", "_"))
2139+
localizations.exists?(locale: locale.to_s.sub("-", "_"))
21412140
end
21422141

21432142
def in_user_locale?
21442143
LocaleNormalizer.is_same?(locale, I18n.locale)
21452144
end
21462145

2147-
def get_localization(locale = I18n.locale)
2148-
locale_str = locale.to_s.sub("-", "_")
2149-
2150-
# prioritise exact match
2151-
if match = topic_localizations.find { |l| l.locale == locale_str }
2152-
return match
2153-
end
2154-
2155-
topic_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
2156-
end
2157-
21582146
private
21592147

21602148
def invite_to_private_message(invited_by, target_user, guardian)

app/models/topic_list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def load_topics
139139
{ category: :parent_category },
140140
]
141141

142-
topic_preloader_associations << :topic_localizations if SiteSetting.content_localization_enabled
142+
topic_preloader_associations << :localizations if SiteSetting.content_localization_enabled
143143

144144
DiscoursePluginRegistry.topic_preloader_associations.each do |a|
145145
fields = a[:fields]

app/models/topic_localization.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class TopicLocalization < ActiveRecord::Base
4+
include LocaleMatchable
5+
46
belongs_to :topic
57

68
validates :locale, presence: true, length: { maximum: 20 }

app/serializers/post_serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,11 @@ def include_mentioned_users?
656656
end
657657

658658
def has_post_localizations
659-
object.post_localizations.any?
659+
object.localizations.any?
660660
end
661661

662662
def post_localizations_count
663-
object.post_localizations.size
663+
object.localizations.size
664664
end
665665

666666
def include_has_post_localizations?

0 commit comments

Comments
 (0)