Use remote translations objects instead of hashes

This way we can simplify the code dealing with the translatable
association.
This commit is contained in:
Javi Martín
2023-01-29 16:30:48 +01:00
parent 26cc75a891
commit 2f0327acf8
4 changed files with 14 additions and 23 deletions

View File

@@ -13,8 +13,6 @@ class Layout::RemoteTranslationsButtonComponent < ApplicationComponent
private private
def display_remote_translation_button? def display_remote_translation_button?
remote_translations.none? do |remote_translation| remote_translations.none?(&:enqueued?)
RemoteTranslation.remote_translation_enqueued?(remote_translation)
end
end end
end end

View File

@@ -4,7 +4,7 @@ module RemotelyTranslatable
def detect_remote_translations(*args) def detect_remote_translations(*args)
return [] unless Setting["feature.remote_translations"].present? && api_key_has_been_set_in_secrets? return [] unless Setting["feature.remote_translations"].present? && api_key_has_been_set_in_secrets?
RemoteTranslation.remote_translations_for(*args) RemoteTranslation.for(*args)
end end
def api_key_has_been_set_in_secrets? def api_key_has_been_set_in_secrets?

View File

@@ -12,16 +12,9 @@ class RemoteTranslation < ApplicationRecord
RemoteTranslations::Caller.new(self).delay.call RemoteTranslations::Caller.new(self).delay.call
end end
def self.remote_translation_enqueued?(remote_translation) def self.for(*args)
where(remote_translatable_id: remote_translation["remote_translatable_id"],
remote_translatable_type: remote_translation["remote_translatable_type"],
locale: remote_translation["locale"],
error_message: nil).any?
end
def self.remote_translations_for(*args)
resources_groups(*args).flatten.select { |resource| translation_empty?(resource) }.map do |resource| resources_groups(*args).flatten.select { |resource| translation_empty?(resource) }.map do |resource|
remote_translation_for(resource) new(remote_translatable: resource, locale: I18n.locale)
end end
end end
@@ -31,20 +24,14 @@ class RemoteTranslation < ApplicationRecord
args.compact - [feeds] + feeds.map(&:items) args.compact - [feeds] + feeds.map(&:items)
end 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) def self.translation_empty?(resource)
resource.class.translates? && resource.translations.where(locale: I18n.locale).empty? resource.class.translates? && resource.translations.where(locale: I18n.locale).empty?
end end
def self.create_all(remote_translations_params) def self.create_all(remote_translations_params)
remote_translations_params.each do |remote_translation_params| remote_translations_params.map do |remote_translation_params|
create!(remote_translation_params) unless remote_translation_enqueued?(remote_translation_params) new(remote_translation_params)
end end.reject(&:enqueued?).each(&:save!)
end end
def already_translated_resource def already_translated_resource
@@ -52,4 +39,10 @@ class RemoteTranslation < ApplicationRecord
errors.add(:locale, :already_translated) errors.add(:locale, :already_translated)
end end
end end
def enqueued?
self.class.where(remote_translatable: remote_translatable,
locale: locale,
error_message: nil).any?
end
end end

View File

@@ -1,7 +1,7 @@
require "rails_helper" require "rails_helper"
describe Layout::RemoteTranslationsButtonComponent do describe Layout::RemoteTranslationsButtonComponent do
let(:translations) { [{}] } let(:translations) { [RemoteTranslation.new] }
let(:component) { Layout::RemoteTranslationsButtonComponent.new(translations) } let(:component) { Layout::RemoteTranslationsButtonComponent.new(translations) }
before do before do