From 17aa90da71a752110087d83da795a1eadbb11201 Mon Sep 17 00:00:00 2001 From: Vincent Rolea <3525369+virolea@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:10:43 +0200 Subject: [PATCH] Add a setup step in tests to ensure locale associations are loaded --- .github/workflows/ci.yml | 18 ++--- app/models/rosetta/text_entry.rb | 4 +- app/models/rosetta/translation_key.rb | 2 +- gemfiles/rails_6_1.gemfile.lock | 4 +- gemfiles/rails_7_0.gemfile.lock | 4 +- gemfiles/rails_7_1.gemfile.lock | 8 +- gemfiles/rails_7_2.gemfile.lock | 8 +- gemfiles/rails_main.gemfile.lock | 110 +++++++++++++------------- lib/rosetta/translated.rb | 10 ++- lib/rosetta/translated/create.rb | 2 +- test/test_helper.rb | 6 +- 11 files changed, 87 insertions(+), 89 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f839814..74d08c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,7 @@ name: CI on: pull_request: + branches: [main] push: branches: [main] @@ -9,8 +10,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 @@ -32,6 +32,9 @@ jobs: - rails_7_1 - rails_7_2 - rails_main + exclude: + - ruby: "3.1" + gemfile: rails_main services: postgres: @@ -57,8 +60,7 @@ jobs: - name: Install packages run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libjemalloc2 libsqlite3-0 libvips - - name: Checkout code - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 @@ -70,11 +72,3 @@ jobs: run: | bin/rails db:test:prepare bin/rails test - - - name: Keep screenshots from failed system tests - uses: actions/upload-artifact@v4 - if: failure() - with: - name: screenshots - path: ${{ github.workspace }}/tmp/screenshots - if-no-files-found: ignore diff --git a/app/models/rosetta/text_entry.rb b/app/models/rosetta/text_entry.rb index 5cf2813..a78c2f5 100644 --- a/app/models/rosetta/text_entry.rb +++ b/app/models/rosetta/text_entry.rb @@ -2,9 +2,9 @@ module Rosetta class TextEntry < ApplicationRecord include Translated - belongs_to :locale + translate_in_all_locales - translated_in_all_locales + belongs_to :locale def self.create_later(content) AutodiscoveryJob.perform_later(content) diff --git a/app/models/rosetta/translation_key.rb b/app/models/rosetta/translation_key.rb index cccc3ae..006bed2 100644 --- a/app/models/rosetta/translation_key.rb +++ b/app/models/rosetta/translation_key.rb @@ -2,7 +2,7 @@ module Rosetta class TranslationKey < ApplicationRecord include Translated - translated_in_all_locales + translate_in_all_locales def self.create_later(value) AutodiscoveryJob.perform_later(value) diff --git a/gemfiles/rails_6_1.gemfile.lock b/gemfiles/rails_6_1.gemfile.lock index c0009ee..54541bb 100644 --- a/gemfiles/rails_6_1.gemfile.lock +++ b/gemfiles/rails_6_1.gemfile.lock @@ -172,7 +172,7 @@ GEM rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.1) + rubocop-rails (2.26.2) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) @@ -192,7 +192,7 @@ GEM sprockets (>= 3.0.0) thor (1.3.2) timeout (0.4.1) - turbo-rails (2.0.9) + turbo-rails (2.0.10) actionpack (>= 6.0.0) railties (>= 6.0.0) tzinfo (2.0.6) diff --git a/gemfiles/rails_7_0.gemfile.lock b/gemfiles/rails_7_0.gemfile.lock index 91fc7f3..04b03ff 100644 --- a/gemfiles/rails_7_0.gemfile.lock +++ b/gemfiles/rails_7_0.gemfile.lock @@ -178,7 +178,7 @@ GEM rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.1) + rubocop-rails (2.26.2) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) @@ -198,7 +198,7 @@ GEM sprockets (>= 3.0.0) thor (1.3.2) timeout (0.4.1) - turbo-rails (2.0.9) + turbo-rails (2.0.10) actionpack (>= 6.0.0) railties (>= 6.0.0) tzinfo (2.0.6) diff --git a/gemfiles/rails_7_1.gemfile.lock b/gemfiles/rails_7_1.gemfile.lock index 370f1eb..ee392e1 100644 --- a/gemfiles/rails_7_1.gemfile.lock +++ b/gemfiles/rails_7_1.gemfile.lock @@ -103,7 +103,7 @@ GEM i18n (1.14.6) concurrent-ruby (~> 1.0) io-console (0.7.2) - irb (1.14.0) + irb (1.14.1) rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.2) @@ -207,7 +207,7 @@ GEM rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.1) + rubocop-rails (2.26.2) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) @@ -228,13 +228,13 @@ GEM stringio (3.1.1) thor (1.3.2) timeout (0.4.1) - turbo-rails (2.0.9) + turbo-rails (2.0.10) actionpack (>= 6.0.0) railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.6.0) - webrick (1.8.1) + webrick (1.8.2) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) diff --git a/gemfiles/rails_7_2.gemfile.lock b/gemfiles/rails_7_2.gemfile.lock index 3549152..8fd90c6 100644 --- a/gemfiles/rails_7_2.gemfile.lock +++ b/gemfiles/rails_7_2.gemfile.lock @@ -99,7 +99,7 @@ GEM i18n (1.14.6) concurrent-ruby (~> 1.0) io-console (0.7.2) - irb (1.14.0) + irb (1.14.1) rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.2) @@ -203,7 +203,7 @@ GEM rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.1) + rubocop-rails (2.26.2) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) @@ -225,14 +225,14 @@ GEM stringio (3.1.1) thor (1.3.2) timeout (0.4.1) - turbo-rails (2.0.9) + turbo-rails (2.0.10) actionpack (>= 6.0.0) railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.6.0) useragent (0.16.10) - webrick (1.8.1) + webrick (1.8.2) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) diff --git a/gemfiles/rails_main.gemfile.lock b/gemfiles/rails_main.gemfile.lock index a91df2d..3ebe2a5 100644 --- a/gemfiles/rails_main.gemfile.lock +++ b/gemfiles/rails_main.gemfile.lock @@ -1,31 +1,31 @@ GIT remote: https://github.com/rails/rails.git - revision: fd975a87758713a26abe22ab1c337d4ea24c8899 + revision: cca4db3db4a3a122ca3d9090f4cfd6754aed6487 branch: main specs: - actioncable (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actioncable (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) zeitwerk (~> 2.6) - actionmailbox (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - activejob (= 8.0.0.alpha) - activerecord (= 8.0.0.alpha) - activestorage (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actionmailbox (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) mail (>= 2.8.0) - actionmailer (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - actionview (= 8.0.0.alpha) - activejob (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actionmailer (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + actionview (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) mail (>= 2.8.0) rails-dom-testing (~> 2.2) - actionpack (8.0.0.alpha) - actionview (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actionpack (8.0.0.beta1) + actionview (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) nokogiri (>= 1.8.5) rack (>= 2.2.4) rack-session (>= 1.0.1) @@ -33,35 +33,35 @@ GIT rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) useragent (~> 0.16) - actiontext (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - activerecord (= 8.0.0.alpha) - activestorage (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actiontext (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (8.0.0.alpha) - activesupport (= 8.0.0.alpha) + actionview (8.0.0.beta1) + activesupport (= 8.0.0.beta1) builder (~> 3.1) erubi (~> 1.11) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) - activejob (8.0.0.alpha) - activesupport (= 8.0.0.alpha) + activejob (8.0.0.beta1) + activesupport (= 8.0.0.beta1) globalid (>= 0.3.6) - activemodel (8.0.0.alpha) - activesupport (= 8.0.0.alpha) - activerecord (8.0.0.alpha) - activemodel (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + activemodel (8.0.0.beta1) + activesupport (= 8.0.0.beta1) + activerecord (8.0.0.beta1) + activemodel (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) timeout (>= 0.4.0) - activestorage (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - activejob (= 8.0.0.alpha) - activerecord (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + activestorage (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) marcel (~> 1.0) - activesupport (8.0.0.alpha) + activesupport (8.0.0.beta1) base64 benchmark (>= 0.3) bigdecimal @@ -74,23 +74,23 @@ GIT securerandom (>= 0.3) tzinfo (~> 2.0, >= 2.0.5) uri (>= 0.13.1) - rails (8.0.0.alpha) - actioncable (= 8.0.0.alpha) - actionmailbox (= 8.0.0.alpha) - actionmailer (= 8.0.0.alpha) - actionpack (= 8.0.0.alpha) - actiontext (= 8.0.0.alpha) - actionview (= 8.0.0.alpha) - activejob (= 8.0.0.alpha) - activemodel (= 8.0.0.alpha) - activerecord (= 8.0.0.alpha) - activestorage (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + rails (8.0.0.beta1) + actioncable (= 8.0.0.beta1) + actionmailbox (= 8.0.0.beta1) + actionmailer (= 8.0.0.beta1) + actionpack (= 8.0.0.beta1) + actiontext (= 8.0.0.beta1) + actionview (= 8.0.0.beta1) + activejob (= 8.0.0.beta1) + activemodel (= 8.0.0.beta1) + activerecord (= 8.0.0.beta1) + activestorage (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) bundler (>= 1.15.0) - railties (= 8.0.0.alpha) - railties (8.0.0.alpha) - actionpack (= 8.0.0.alpha) - activesupport (= 8.0.0.alpha) + railties (= 8.0.0.beta1) + railties (8.0.0.beta1) + actionpack (= 8.0.0.beta1) + activesupport (= 8.0.0.beta1) irb (~> 1.13) rackup (>= 1.0.0) rake (>= 12.2) @@ -129,7 +129,7 @@ GEM i18n (1.14.6) concurrent-ruby (~> 1.0) io-console (0.7.2) - irb (1.14.0) + irb (1.14.1) rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.2) diff --git a/lib/rosetta/translated.rb b/lib/rosetta/translated.rb index 993b9be..f67ecf9 100644 --- a/lib/rosetta/translated.rb +++ b/lib/rosetta/translated.rb @@ -2,8 +2,13 @@ module Rosetta module Translated extend ActiveSupport::Concern + included do + scope :missing_translation, ->(locale) { where.missing(:"#{locale.code}_translation") } + scope :with_translated_version, ->(locale) { includes(:"#{locale.code}_translated_version") } + end + class_methods do - def translated_in_all_locales + def translate_in_all_locales Locale.all.each do |locale| translated_in(locale) end @@ -15,9 +20,6 @@ def translated_in(locale) has_one :"#{locale.code}_translation", -> { where(locale_code: locale.code) }, class_name: "Rosetta::Translation", foreign_key: :from_id, dependent: :destroy has_one :"#{locale.code}_translated_version", through: :"#{locale.code}_translation", source: :to - scope :missing_translation, ->(locale) { where.missing(:"#{locale.code}_translation") } - scope :with_translated_version, ->(locale) { includes(:"#{locale.code}_translated_version") } - define_method("content_#{locale.code}") do if translation_changes[locale.code] translation_changes[locale.code].content diff --git a/lib/rosetta/translated/create.rb b/lib/rosetta/translated/create.rb index 0d85364..4688574 100644 --- a/lib/rosetta/translated/create.rb +++ b/lib/rosetta/translated/create.rb @@ -31,7 +31,7 @@ def find_translated_version end def build_translated_version - TextEntry.build(locale: @locale, content: @content) + TextEntry.new(locale: @locale, content: @content) end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index d911616..758bc7c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -24,13 +24,15 @@ class ActiveSupport::TestCase # Provide a clean slate for each test: # - Unset the default locale - # - Reset the registered classes for translations # - Reload all locale stores + # - Make sure the locale-specific associations are loaded on text entries + # - Reset the registered classes for translations # - Set the locale to the default locale setup do Rosetta::Locale.default_locale = nil - Rosetta::Locale.registered_classes_for_translations = [] Rosetta::Store.locale_stores.each { |code, store| store.reload! } + Rosetta::Locale.registered_classes_for_translations = [] + Rosetta::TextEntry.translate_in_all_locales unless Rosetta::TextEntry.respond_to?(:fr_translation) Rosetta.locale = :en end end