DRY supported_languages

This commit is contained in:
2026-01-23 21:55:06 +01:00
parent a7713b962f
commit b3726e0777
8 changed files with 46 additions and 23 deletions
@@ -22,8 +22,7 @@ class Admin::DashboardController < Admin::BaseController
.where("invitation_sent_at > ?", 14.days.ago)
.count
@supported_languages = SupportedLanguage.where(active: true).order(:sort_order)
@language_completion = @supported_languages.index_with do |language|
@language_completion = supported_languages.index_with do |language|
next 0 if @entry_count.zero?
(Entry.where.not(language.code => [ nil, "" ]).count * 100.0 / @entry_count).round
+6 -2
View File
@@ -1,12 +1,16 @@
class ApplicationController < ActionController::Base
# Changes to the importmap will invalidate the etag for HTML responses
stale_when_importmap_changes
helper_method :current_user, :logged_in?, :admin?, :reviewer_or_admin?, :contributor_or_above?, :setup_completed?
helper_method :supported_languages, :current_user, :logged_in?, :admin?, :reviewer_or_admin?,
:contributor_or_above?, :setup_completed?
private
def supported_languages
@supported_languages ||= SupportedLanguage.where(active: true).order(:sort_order, :name)
end
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
+4 -6
View File
@@ -2,7 +2,6 @@ class EntriesController < ApplicationController
before_action :set_entry, only: [ :show ]
def index
@supported_languages = SupportedLanguage.where(active: true).order(:sort_order, :name)
@language_code = params[:language].presence
@category = params[:category].presence
@query = params[:q].to_s.strip
@@ -24,21 +23,21 @@ class EntriesController < ApplicationController
@entry_count = Entry.count
@verified_count = Entry.where(verified: true).count
@needs_review_count = @entry_count - @verified_count
@complete_entries_count = @supported_languages.reduce(Entry.all) do |scope, language|
@complete_entries_count = supported_languages.reduce(Entry.all) do |scope, language|
scope.where.not(language.code => [ nil, "" ])
end.count
@missing_entries_count = @entry_count - @complete_entries_count
@language_completion = @supported_languages.index_with do |language|
@language_completion = supported_languages.index_with do |language|
next 0 if @entry_count.zero?
(Entry.where.not(language.code => [ nil, "" ]).count * 100.0 / @entry_count).round
end
if @language_code.present?
primary_language, other_languages = @supported_languages.partition { |language| language.code == @language_code }
primary_language, other_languages = supported_languages.partition { |language| language.code == @language_code }
@display_languages = primary_language + other_languages
else
@display_languages = @supported_languages
@display_languages = supported_languages
end
respond_to do |format|
@@ -48,7 +47,6 @@ class EntriesController < ApplicationController
end
def show
@supported_languages = SupportedLanguage.where(active: true).order(:sort_order, :name)
end
def download
+3
View File
@@ -1,2 +1,5 @@
module ApplicationHelper
def language_name(code)
supported_languages.find { |l| l.code == code }&.name
end
end
+1 -1
View File
@@ -115,7 +115,7 @@
<h3 class="text-lg leading-6 font-medium text-gray-900">Language Completion</h3>
<div class="mt-5">
<div class="space-y-4">
<% @supported_languages.each do |language| %>
<% supported_languages.each do |language| %>
<div>
<div class="flex items-center justify-between">
<span class="text-sm font-medium text-gray-700"><%= language.native_name %></span>
+1 -1
View File
@@ -19,7 +19,7 @@
entries_path(all_language_params),
class: "px-3 py-1 rounded-full #{@language_code.blank? ? 'bg-slate-900 text-white' : 'bg-white border border-slate-200 text-slate-600 hover:border-slate-300'} text-xs font-semibold uppercase tracking-wider",
data: { turbo_stream: true } %>
<% @supported_languages.each do |language| %>
<% supported_languages.each do |language| %>
<%= link_to "#{language.name} (#{language.code.upcase})",
entries_path(all_language_params.merge(language: language.code)),
class: "px-3 py-1 rounded-full #{@language_code == language.code ? 'bg-slate-900 text-white' : 'bg-white border border-slate-200 text-slate-600 hover:border-slate-300'} text-xs font-semibold uppercase tracking-wider",
+9 -2
View File
@@ -33,12 +33,19 @@
<div class="p-6">
<div class="grid grid-cols-1 md:grid-cols-2 gap-y-6 gap-x-12">
<% @supported_languages.each do |language| %>
<% supported_languages.each do |language| %>
<% translation = entry_translation_for(@entry, language.code) %>
<% next if translation.blank? %>
<div class="space-y-1">
<div class="space-y-2">
<div class="grid grid-cols-2">
<span class="text-[10px] font-bold text-slate-400 uppercase tracking-tight"><%= "#{language.name} (#{language.code.upcase})" %></span>
</div>
<p class="text-2xl font-semibold text-slate-800"><%= translation %></p>
<% if current_user %>
<div id="comment-details-<%= language.code %>">
<%= render "entries/language_comment_details", entry: @entry, language_code: language.code %>
</div>
<% end %>
</div>
<% end %>
</div>
+20 -8
View File
@@ -1,29 +1,41 @@
require "test_helper"
class CommentTest < ActiveSupport::TestCase
test "should be valid with a user, body, and commentable" do
user = users(:admin_user)
test "should be valid with a user, body, commentable, and language code" do
user = users(:contributor_user)
entry = entries(:one)
comment = Comment.new(user: user, body: "This is a comment.", commentable: entry)
language = supported_languages(:one)
comment = Comment.new(user: user, body: "This is a comment.", commentable: entry, language: language)
assert comment.valid?
end
test "should be valid without a language code" do
user = users(:contributor_user)
entry = entries(:one)
comment = Comment.new(user: user, body: "General note.", commentable: entry, language_code: nil)
assert comment.valid?
end
test "should be invalid without a body" do
user = users(:admin_user)
user = users(:contributor_user)
entry = entries(:one)
comment = Comment.new(user: user, commentable: entry)
language = supported_languages(:one)
comment = Comment.new(user: user, commentable: entry, language: language)
assert_not comment.valid?
end
test "should be invalid without a user" do
entry = entries(:one)
comment = Comment.new(body: "This is a comment.", commentable: entry)
language = supported_languages(:one)
comment = Comment.new(body: "This is a comment.", commentable: entry, language: language)
assert_not comment.valid?
end
test "should be invalid without a commentable" do
user = users(:admin_user)
comment = Comment.new(user: user, body: "This is a comment.")
user = users(:contributor_user)
language = supported_languages(:one)
comment = Comment.new(user: user, body: "This is a comment.", language: language)
assert_not comment.valid?
end
end