diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index f798c3443ff366010e4f78d143922482ba0cf63c..7dcf06a8e205b47a2d3bdb55e7848e331ea30fac 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -43,6 +43,7 @@ class Communication::Website < ApplicationRecord include WithGit include WithGitRepository include WithImport + include WithLanguages include WithOldDependencies include WithProgramCategories include WithReferences @@ -57,16 +58,6 @@ class Communication::Website < ApplicationRecord gitlab: 1 } - belongs_to :default_language, class_name: "Language" - has_and_belongs_to_many :languages, - class_name: 'Language', - join_table: 'communication_websites_languages', - foreign_key: 'communication_website_id', - association_foreign_key: 'language_id' - - validates :languages, length: { minimum: 1 } - validate :languages_must_include_default_language - before_validation :sanitize_fields scope :ordered, -> { order(:name) } @@ -102,12 +93,6 @@ class Communication::Website < ApplicationRecord [] end - def best_language_for(iso_code) - # We look for the language by the ISO code in the websites languages. - # If not found, we fallback to the default language. - languages.find_by(iso_code: iso_code) || default_language - end - def website self end @@ -126,8 +111,4 @@ class Communication::Website < ApplicationRecord self.repository = Osuny::Sanitizer.sanitize(self.repository, 'string') self.url = Osuny::Sanitizer.sanitize(self.url, 'string') end - - def languages_must_include_default_language - errors.add(:languages, :must_include_default) unless language_ids.include?(default_language_id) - end end diff --git a/app/models/communication/website/with_git_repository.rb b/app/models/communication/website/with_git_repository.rb index d8e99687e3a292195146148babb68e7896599e28..dd22d6592cffc9aa083b2471b3d65406b0862a78 100644 --- a/app/models/communication/website/with_git_repository.rb +++ b/app/models/communication/website/with_git_repository.rb @@ -5,6 +5,8 @@ module Communication::Website::WithGitRepository has_many :website_git_files, class_name: 'Communication::Website::GitFile', dependent: :destroy + + after_save :destroy_obsolete_git_files, if: :should_clean_on_git? end def git_repository @@ -30,4 +32,8 @@ module Communication::Website::WithGitRepository def exportable_to_git? true end + + def should_clean_on_git? + saved_change_to_about_id? || language_was_removed + end end diff --git a/app/models/communication/website/with_languages.rb b/app/models/communication/website/with_languages.rb new file mode 100644 index 0000000000000000000000000000000000000000..835a37c445206d989b4c9b6372ec4e9a4bb17eed --- /dev/null +++ b/app/models/communication/website/with_languages.rb @@ -0,0 +1,36 @@ +module Communication::Website::WithLanguages + extend ActiveSupport::Concern + + included do + attr_accessor :language_was_removed + + belongs_to :default_language, class_name: "Language" + has_and_belongs_to_many :languages, + class_name: 'Language', + join_table: 'communication_websites_languages', + foreign_key: 'communication_website_id', + association_foreign_key: 'language_id', + after_remove: :flag_languages_change + + validates :languages, length: { minimum: 1 } + validate :languages_must_include_default_language + + end + + def best_language_for(iso_code) + # We look for the language by the ISO code in the websites languages. + # If not found, we fallback to the default language. + languages.find_by(iso_code: iso_code) || default_language + end + + protected + + def languages_must_include_default_language + errors.add(:languages, :must_include_default) unless language_ids.include?(default_language_id) + end + + def flag_languages_change(_) + @language_was_removed = true + end + +end