From e6f2b4b45a5bca5c222415625184dab3a77c59db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Andr=C3=A9=20Boissinot?= Date: Fri, 7 Jun 2024 14:29:33 +0200 Subject: [PATCH] =?UTF-8?q?G=C3=A8re=20la=20suppression=20ou=20non=20des?= =?UTF-8?q?=20translations=20lorsqu'on=20supprime=20un=20objet=20direct=20?= =?UTF-8?q?ou=20indirect=20(#2001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 ++- Gemfile.lock | 2 ++ app/models/concerns/translatable.rb | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index bb498ffbd..228bdd98d 100644 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,7 @@ gem "kaminari" gem "leaflet-rails" gem "libretranslate"#, path: "../libretranslate" gem "mini_magick" +gem "observer", "~> 0.1.2" gem "octokit" gem "omniauth-rails_csrf_protection", "~> 1" gem "omniauth-saml", "~> 2" @@ -103,4 +104,4 @@ group :test do gem "simplecov", require: false end -gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby] \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 965802363..6d1f32627 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -425,6 +425,7 @@ GEM rack (>= 1.2, < 4) snaky_hash (~> 2.0) version_gem (~> 1.1) + observer (0.1.2) octokit (8.1.0) base64 faraday (>= 1, < 3) @@ -700,6 +701,7 @@ DEPENDENCIES libretranslate listen (~> 3.3) mini_magick + observer (~> 0.1.2) octokit omniauth-rails_csrf_protection (~> 1) omniauth-saml (~> 2) diff --git a/app/models/concerns/translatable.rb b/app/models/concerns/translatable.rb index 79977054f..82568cd3d 100644 --- a/app/models/concerns/translatable.rb +++ b/app/models/concerns/translatable.rb @@ -10,8 +10,10 @@ module Translatable optional: true has_many :translations, class_name: base_class.to_s, - foreign_key: :original_id, - dependent: :nullify + foreign_key: :original_id + + # has to be before_destroy because of the foreign key constraints + before_destroy :destroy_or_nullify_translations scope :for_language, -> (language) { for_language_id(language.id) } # The for_language_id scope can be used when you have the ID without needing to load the Language itself @@ -118,4 +120,16 @@ def translate_attachment(translation, attachment_name) def translate_additional_data!(translation) # Overridable method to handle custom cases end + + def destroy_or_nullify_translations + # Translatable is included in either Direct or Indirect Objects + # If object is direct we do not want to remove the translations + # If object is indirect we remove the translations + if is_direct_object? + translations.update_all(original_id: nil) + else + translations.destroy_all + end + end + end \ No newline at end of file