From f4d84c48062a18f7f93b0d12a04c8e15c7f57e23 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 2 Jan 2024 08:09:35 +0300 Subject: [PATCH] Add links to diaries in preferred languages --- app/controllers/diary_entries_controller.rb | 2 ++ app/views/diary_entries/index.html.erb | 4 ++++ test/system/diary_entry_test.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 6db32797a..1f9574b9c 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -47,6 +47,8 @@ class DiaryEntriesController < ApplicationController @title = t ".in_language_title", :language => Language.find(params[:language]).english_name entries = entries.where(:language_code => params[:language]) else + candidate_codes = preferred_languages.flat_map(&:candidates).uniq.map(&:to_s) + @languages = Language.where(:code => candidate_codes).in_order_of(:code, candidate_codes) @title = t ".title" end end diff --git a/app/views/diary_entries/index.html.erb b/app/views/diary_entries/index.html.erb index 359bc5eb4..e9d7e18c9 100644 --- a/app/views/diary_entries/index.html.erb +++ b/app/views/diary_entries/index.html.erb @@ -16,6 +16,10 @@
  • <%= rss_link_to :action => "rss", :language => params[:language] %>
  • <% end -%> + <% @languages&.each do |language| %> +
  • <%= link_to t(".in_language_title", :language => language.name), :action => "index", :language => language.code %>
  • + <% end %> + <% if @user && @user == current_user || !@user && current_user %>
  • <%= link_to image_tag("new.png", :class => "small_icon") + t(".new"), new_diary_entry_path, :title => t(".new_title") %>
  • <% end %> diff --git a/test/system/diary_entry_test.rb b/test/system/diary_entry_test.rb index 554b89a6d..290b94207 100644 --- a/test/system/diary_entry_test.rb +++ b/test/system/diary_entry_test.rb @@ -3,6 +3,9 @@ require "application_system_test_case" class DiaryEntrySystemTest < ApplicationSystemTestCase def setup create(:language, :code => "en") + create(:language, :code => "pt", :english_name => "Portuguese", :native_name => "Português") + create(:language, :code => "pt-BR", :english_name => "Brazilian Portuguese", :native_name => "Português do Brasil") + create(:language, :code => "ru", :english_name => "Russian", :native_name => "Русский") @diary_entry = create(:diary_entry) end @@ -61,4 +64,14 @@ class DiaryEntrySystemTest < ApplicationSystemTestCase assert_content @deleted_comment.body end + + test "should have links to preferred languages" do + sign_in_as(create(:user, :languages => %w[en-US pt-BR])) + visit diary_entries_path + + assert_link "Diary Entries in English", :href => "/diary/en" + assert_link "Diary Entries in Brazilian Portuguese", :href => "/diary/pt-BR" + assert_link "Diary Entries in Portuguese", :href => "/diary/pt" + assert_no_link "Diary Entries in Russian" + end end -- 2.39.5