diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 72b9a95e3131e1bca5e8a4773889d6e64a605a58..319cc636bb9926b23efa41e48a2417fafc3c44ca 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 26fbf3ab9f5a784aa76967808c5c2bd57c570426..f7042b19fc7f9514d5bb80d7c8e831a65dcb051e 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 634f59a9bc9c6ee429fe459abeaffe358a9b62a4..dfa25a79a1f7c1d8f1cf835330bd59fd7cac30e3 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 1c0f7d0024a6a74c5e5622fc1382d748d6b9c1ef..a1eae1b33421e2f0d8434295746d2c5c8db1c9e6 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 e89b9fd4cac31e15f5478a9a45f9680a84c91c29..5221ece0587cd49c0597d08f203b3ede1126f00c 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: