Skip to content

Commit

Permalink
♻️ Extraie la logique de calcul de pourcentage
Browse files Browse the repository at this point in the history
Pour éviter les erreurs et les répétitions
  • Loading branch information
cprodhomme authored and Guitguitou committed Dec 23, 2024
1 parent 285a531 commit 145f919
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/models/restitution/evacob/score_metacompetence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def calcule(evenements, metacompetence)
def calcule_pourcentage_reussite(reponses)
scores = reponses.map { |e| [e['scoreMax'] || 0, e['score'] || 0] }
score_max, score = scores.transpose.map(&:sum)
score_max.zero? ? nil : (score.to_f * 100 / score_max.to_f).round

Pourcentage.new(valeur: score, valeur_max: score_max).calcul&.round
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/restitution/evacob/score_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def calcule_pourcentage_reussite(evenements, nom_module)

return if evenements.blank?

score_max = evenements&.sum(&:score_max_reponse).to_f
score = evenements&.sum(&:score_reponse).to_f
score_max = evenements&.sum(&:score_max_reponse)
score = evenements&.sum(&:score_reponse)

(score / score_max) * 100 if score.present? && score_max.present?
Pourcentage.new(valeur: score, valeur_max: score_max).calcul
end

private
Expand Down
3 changes: 2 additions & 1 deletion app/models/restitution/positionnement/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def remplis_par_sous_sous_domaine(ligne, sous_code, evenements, export)
def pourcentage_reussite(reponses)
scores = reponses.map { |e| [e['scoreMax'] || 0, e['score'] || 0] }
score_max, score = scores.transpose.map(&:sum)
score_max.zero? ? 'non applicable' : "#{(score.to_f * 100 / score_max.to_f).round}%"
pourcentage = Pourcentage.new(valeur: score, valeur_max: score_max).calcul&.round
score_max.zero? ? 'non applicable' : "#{pourcentage}%"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/statistiques_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def evaluations_par_genre(niveau)
def transforme_valeurs_en_pourcentage(hash)
total_valeurs = hash.values.sum
hash.update(hash) do |_cle, valeur|
(valeur.to_f / total_valeurs * 100)
Pourcentage.new(valeur: valeur, valeur_max: total_valeurs).calcul
end
end

Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ class Application < Rails::Application
config.active_storage.track_variants = false

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.autoload_lib(ignore: %w(assets tasks generateur_aleatoire.rb importeur_telephone.rb google_drive_storage.rb custom_exceptions_app_wrapper.rb importeur_commentaires.rb rake_logger.rb))

Rails.autoloaders.main.ignore(Rails.root.join('app/controllers/active_admin/**/*'))
config.i18n.available_locales = [:fr]
config.i18n.default_locale = :fr

config.middleware.use I18n::JS::Middleware

config.view_component_storybook.stories_path = Rails.root.join("spec/components/stories")
Expand Down
17 changes: 17 additions & 0 deletions lib/pourcentage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class Pourcentage
def initialize(valeur:, valeur_max:)
@valeur = valeur
@valeur_max = valeur_max
end

# retourne un pourcentage (float) ou nil
def calcul
return if @valeur.blank?
return if @valeur_max.blank?
return if @valeur_max.zero?

(@valeur.to_f / @valeur_max) * 100
end
end

0 comments on commit 145f919

Please sign in to comment.