From b3726e07771fef3c9852c4eef19447e1d9b00cb5 Mon Sep 17 00:00:00 2001 From: Runar Ingebrigtsen Date: Fri, 23 Jan 2026 21:55:06 +0100 Subject: [PATCH] DRY supported_languages --- app/controllers/admin/dashboard_controller.rb | 3 +- app/controllers/application_controller.rb | 8 ++++-- app/controllers/entries_controller.rb | 10 +++---- app/helpers/application_helper.rb | 3 ++ app/views/admin/dashboard/index.html.erb | 2 +- app/views/entries/_filters.html.erb | 2 +- app/views/entries/show.html.erb | 13 +++++++-- test/models/comment_test.rb | 28 +++++++++++++------ 8 files changed, 46 insertions(+), 23 deletions(-) diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 9a1cdfb..bad4114 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 71997a4..7ddd11d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index dfa55ce..53ca006 100644 --- a/app/controllers/entries_controller.rb +++ b/app/controllers/entries_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..3923f72 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,5 @@ module ApplicationHelper + def language_name(code) + supported_languages.find { |l| l.code == code }&.name + end end diff --git a/app/views/admin/dashboard/index.html.erb b/app/views/admin/dashboard/index.html.erb index dda36ed..6d3a2f8 100644 --- a/app/views/admin/dashboard/index.html.erb +++ b/app/views/admin/dashboard/index.html.erb @@ -115,7 +115,7 @@

Language Completion

- <% @supported_languages.each do |language| %> + <% supported_languages.each do |language| %>
<%= language.native_name %> diff --git a/app/views/entries/_filters.html.erb b/app/views/entries/_filters.html.erb index 89202a4..44249e6 100644 --- a/app/views/entries/_filters.html.erb +++ b/app/views/entries/_filters.html.erb @@ -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", diff --git a/app/views/entries/show.html.erb b/app/views/entries/show.html.erb index cc6364a..0e7e393 100644 --- a/app/views/entries/show.html.erb +++ b/app/views/entries/show.html.erb @@ -33,12 +33,19 @@
- <% @supported_languages.each do |language| %> + <% supported_languages.each do |language| %> <% translation = entry_translation_for(@entry, language.code) %> <% next if translation.blank? %> -
- <%= "#{language.name} (#{language.code.upcase})" %> +
+
+ <%= "#{language.name} (#{language.code.upcase})" %> +

<%= translation %>

+ <% if current_user %> +
+ <%= render "entries/language_comment_details", entry: @entry, language_code: language.code %> +
+ <% end %>
<% end %>
diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index f98117c..ff69869 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -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