Move remote translations concern methods to the model

This way it'll be easier to change the code.
This commit is contained in:
Javi Martín
2023-01-29 16:27:53 +01:00
parent f8df90010e
commit d09a47a023
2 changed files with 23 additions and 19 deletions

View File

@@ -4,25 +4,7 @@ module RemotelyTranslatable
def detect_remote_translations(*args)
return [] unless Setting["feature.remote_translations"].present? && api_key_has_been_set_in_secrets?
resources_groups(*args).flatten.select { |resource| translation_empty?(resource) }.map do |resource|
remote_translation_for(resource)
end
end
def remote_translation_for(resource)
{ "remote_translatable_id" => resource.id.to_s,
"remote_translatable_type" => resource.class.to_s,
"locale" => I18n.locale }
end
def translation_empty?(resource)
resource.class.translates? && resource.translations.where(locale: I18n.locale).empty?
end
def resources_groups(*args)
feeds = args.find { |arg| arg&.first.class == Widget::Feed } || []
args.compact - [feeds] + feeds.map(&:items)
RemoteTranslation.remote_translations_for(*args)
end
def api_key_has_been_set_in_secrets?

View File

@@ -19,6 +19,28 @@ class RemoteTranslation < ApplicationRecord
error_message: nil).any?
end
def self.remote_translations_for(*args)
resources_groups(*args).flatten.select { |resource| translation_empty?(resource) }.map do |resource|
remote_translation_for(resource)
end
end
def self.resources_groups(*args)
feeds = args.find { |arg| arg&.first.class == Widget::Feed } || []
args.compact - [feeds] + feeds.map(&:items)
end
def self.remote_translation_for(resource)
{ "remote_translatable_id" => resource.id.to_s,
"remote_translatable_type" => resource.class.to_s,
"locale" => I18n.locale }
end
def self.translation_empty?(resource)
resource.class.translates? && resource.translations.where(locale: I18n.locale).empty?
end
def already_translated_resource
if remote_translatable&.translations&.where(locale: locale).present?
errors.add(:locale, :already_translated)