Skip to content

Commit

Permalink
Gère la suppression ou non des translations lorsqu'on supprime un obj…
Browse files Browse the repository at this point in the history
…et direct ou indirect (#2001)
  • Loading branch information
pabois committed Jun 7, 2024
1 parent 5fb68be commit e6f2b4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -700,6 +701,7 @@ DEPENDENCIES
libretranslate
listen (~> 3.3)
mini_magick
observer (~> 0.1.2)
octokit
omniauth-rails_csrf_protection (~> 1)
omniauth-saml (~> 2)
Expand Down
18 changes: 16 additions & 2 deletions app/models/concerns/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e6f2b4b

Please sign in to comment.