Skip to content

Commit

Permalink
Ticket CV2-5813: Set default language when creating explainer without…
Browse files Browse the repository at this point in the history
… language
  • Loading branch information
caiosba committed Dec 17, 2024
1 parent 933c3a9 commit da78439
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/tasks/migrate/20241216215050_set_language_for_explainers.rake
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit da78439

Please sign in to comment.