From 7a509cfd6e2528a210cfb043dea26b383e840d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Fri, 3 Feb 2023 16:52:08 +0100 Subject: [PATCH] wip translation --- app/models/communication/website/post.rb | 10 +++++++--- app/models/concerns/with_slug.rb | 5 ++++- app/models/concerns/with_translations.rb | 9 +++++++-- .../admin/communication/websites/posts/_form.html.erb | 11 ++--------- .../communication/websites/posts/static.html.erb | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 72b9a95e3..319cc636b 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -126,8 +126,9 @@ class Communication::Website::Post < ApplicationRecord dependencies += git_block_dependencies dependencies += university.communication_blocks.where(template_kind: :posts).includes(:about).map(&:about).uniq if author.present? - dependencies += [author, author.author] + dependencies += [author, author.author, translated_author, translated_author.author] dependencies += author.active_storage_blobs + dependencies += translated_author.active_storage_blobs end dependencies end @@ -144,6 +145,10 @@ class Communication::Website::Post < ApplicationRecord "#{Static.remove_trailing_slash website.url}#{Static.clean_path current_permalink_in_website(website).path}" end + def translated_author + @translated_author ||= author.find_or_translate!(language) + end + def to_s "#{title}" end @@ -179,8 +184,7 @@ class Communication::Website::Post < ApplicationRecord def translate_additional_data!(translation) categories.each do |category| - translated_category = category.translation_for(translation.language) - translated_category ||= category.translate!(translation.language) + translated_category = category.find_or_translate!(translation.language) translation.categories << translated_category end end diff --git a/app/models/concerns/with_slug.rb b/app/models/concerns/with_slug.rb index 26fbf3ab9..f7042b19f 100644 --- a/app/models/concerns/with_slug.rb +++ b/app/models/concerns/with_slug.rb @@ -25,8 +25,11 @@ module WithSlug protected def slug_unavailable?(slug) + existence_params = { university_id: self.university_id, slug: slug } + existence_params[:language_id] = self.language_id if respond_to?(:language_id) + self.class.unscoped - .where(university_id: self.university_id, slug: slug) + .where(**existence_params) .where.not(id: self.id) .exists? end diff --git a/app/models/concerns/with_translations.rb b/app/models/concerns/with_translations.rb index 634f59a9b..dfa25a79a 100644 --- a/app/models/concerns/with_translations.rb +++ b/app/models/concerns/with_translations.rb @@ -7,6 +7,12 @@ module WithTranslations has_many :translations, class_name: base_class.to_s, foreign_key: :original_id, dependent: :nullify end + def find_or_translate!(language) + translation = translation_for(language) + translation ||= translate!(language) + translation + end + def translation_for(language) # If current language is language, returns itself return self if language_id == language.id @@ -28,8 +34,7 @@ module WithTranslations # Translate parent if needed if respond_to?(:parent_id) && parent_id.present? - parent_translation = parent.translation_for(language) - parent_translation ||= parent.translate!(language) + parent_translation = parent.find_or_translate!(language) translation.parent_id = parent_translation&.id end diff --git a/app/views/admin/communication/websites/posts/_form.html.erb b/app/views/admin/communication/websites/posts/_form.html.erb index 1c0f7d002..a1eae1b33 100644 --- a/app/views/admin/communication/websites/posts/_form.html.erb +++ b/app/views/admin/communication/websites/posts/_form.html.erb @@ -39,16 +39,9 @@ </div> <%= f.input :published_at, html5: true, as: :date %> <% end %> - <% if current_user.author? || current_user.contributor? %> - <%= f.input :author_id, - as: :hidden, - input_html: { value: current_user.person&.id }, - wrapper: false %> - <% else %> - <%= f.association :author, - collection: current_university.people.ordered, + <%= f.association :author, + collection: current_university.people.where(language_id: current_university.default_language_id).ordered, label_method: :to_s_alphabetical %> - <% end %> <%= f.input :slug, as: :string, input_html: post.persisted? ? {} : { diff --git a/app/views/admin/communication/websites/posts/static.html.erb b/app/views/admin/communication/websites/posts/static.html.erb index e89b9fd4c..5221ece05 100644 --- a/app/views/admin/communication/websites/posts/static.html.erb +++ b/app/views/admin/communication/websites/posts/static.html.erb @@ -8,7 +8,7 @@ weight: 1 <% end %> <% if @about.author %> authors: - - "<%= @about.author.slug %>" + - "<%= @about.translated_author.slug %>" <% end %> <% if @about.categories.any? %> categories: -- GitLab