From da784397bafa5949f26bbd87daf1f441b9afa86b Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:16:11 -0300 Subject: [PATCH] Ticket CV2-5813: Set default language when creating explainer without language --- app/models/explainer.rb | 11 ++++++++--- ...1216215050_set_language_for_explainers.rake | 18 ++++++++++++++++++ test/models/explainer_test.rb | 9 +++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 lib/tasks/migrate/20241216215050_set_language_for_explainers.rake diff --git a/app/models/explainer.rb b/app/models/explainer.rb index feee0df35..2d11ae908 100644 --- a/app/models/explainer.rb +++ b/app/models/explainer.rb @@ -7,10 +7,10 @@ class Explainer < ApplicationRecord has_many :explainer_items, dependent: :destroy has_many :project_medias, through: :explainer_items - before_validation :set_team + before_validation :set_team, :set_language validates_format_of :url, with: URI.regexp, allow_blank: true, allow_nil: true validates_presence_of :team, :title, :description - validate :language_in_allowed_values, unless: proc { |e| e.language.blank? } + validate :language_in_allowed_values after_save :update_paragraphs_in_alegre after_update :detach_explainer_if_trashed @@ -137,8 +137,13 @@ def set_team self.team ||= Team.current end + def set_language + default_language = self.team&.get_language || 'unk' + self.language ||= default_language + end + def language_in_allowed_values - allowed_languages = self.team.get_languages || ['en'] + allowed_languages = self.team&.get_languages || ['en'] allowed_languages << 'und' errors.add(:language, I18n.t(:"errors.messages.invalid_article_language_value")) unless allowed_languages.include?(self.language) end diff --git a/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake b/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake new file mode 100644 index 000000000..3084967c2 --- /dev/null +++ b/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake @@ -0,0 +1,18 @@ +namespace :check do + namespace :migrate do + task set_language_for_explainers: :environment do + started = Time.now.to_i + query = Explainer.where(language: nil) + n = query.count + i = 0 + query.find_each do |explainer| + i += 1 + language = explainer.team&.get_language || 'unk' + explainer.update_column(:language, language) + puts "[#{Time.now}] [#{i}/#{n}] Setting language for explainer ##{explainer.id} as #{language}" + end + minutes = ((Time.now.to_i - started) / 60).to_i + puts "[#{Time.now}] Done in #{minutes} minutes. Number of explainers without language: #{query.count}" + end + end +end diff --git a/test/models/explainer_test.rb b/test/models/explainer_test.rb index c694152dd..7c40dce9d 100644 --- a/test/models/explainer_test.rb +++ b/test/models/explainer_test.rb @@ -5,6 +5,10 @@ def setup Explainer.delete_all end + def teardown + User.current = Team.current = nil + end + test "should create explainer" do assert_difference 'Explainer.count' do create_explainer @@ -159,4 +163,9 @@ def setup models_thresholds = Explainer.get_alegre_models_and_thresholds(ex.team_id) assert_kind_of Hash, models_thresholds end + + test "should set default language when language is not set" do + ex = create_explainer language: nil + assert_equal 'en', ex.reload.language + end end