- Create concern to reuse the logic of detection of non-existent
translations in Controllers.
- Add detect_remote_translation method:
* This method will be called from controllers to recover resources
without translation.
* Receive arrays of resources.
* Return an array with hashes of remote_translations values for
every resources that have not translations.
* This array will be the param that will be sent from view to
RemoteTranslationController for create remote translations instances.
30 lines
797 B
Ruby
30 lines
797 B
Ruby
module RemotelyTranslatable
|
|
|
|
private
|
|
|
|
def detect_remote_translations(*args)
|
|
return [] unless Setting["feature.remote_translations"].present?
|
|
|
|
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.translations.where(locale: I18n.locale).empty?
|
|
end
|
|
|
|
def resources_groups(*args)
|
|
feeds = args.detect { |arg| arg&.first.class == Widget::Feed } || []
|
|
|
|
args.compact - [feeds] + feeds.map(&:items)
|
|
end
|
|
|
|
end
|