diff options
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/tartiflette_score_export_service.rb | 19 | ||||
-rw-r--r-- | app/services/tartiflette_scoring_service.rb | 54 |
2 files changed, 73 insertions, 0 deletions
diff --git a/app/services/tartiflette_score_export_service.rb b/app/services/tartiflette_score_export_service.rb new file mode 100644 index 0000000..26334a0 --- /dev/null +++ b/app/services/tartiflette_score_export_service.rb @@ -0,0 +1,19 @@ +require "csv" + +class TartifletteScoreExportService + def self.generate_csv + CSV.generate(headers: true) do |csv| + csv << [ "Identifiant", "Critère", "Score", "Création" ] + Tartiflette.all.each do |tartiflette| + tartiflette.scores.each do |score| + csv << [ + tartiflette.scoring_id, + score.scoring_criterium.name, + score.value, + score.created_at + ] + end + end + end + end +end diff --git a/app/services/tartiflette_scoring_service.rb b/app/services/tartiflette_scoring_service.rb new file mode 100644 index 0000000..3514eb1 --- /dev/null +++ b/app/services/tartiflette_scoring_service.rb @@ -0,0 +1,54 @@ +class TartifletteScoringService + def self.scored?(tartiflette, session) + session[:scored_tartiflettes]&.include?(tartiflette.id) + end + + def self.mark_as_scored(tartiflette, session) + session[:scored_tartiflettes] ||= [] + unless scored?(tartiflette, session) + session[:scored_tartiflettes] << tartiflette.id + end + end + + def self.submit_scores(tartiflette, scores, session) + scores.each do |criterium_id, value| + Score.create!( + tartiflette: tartiflette, + scoring_criterium_id: criterium_id, + value: value[:value] + ) + end + mark_as_scored(tartiflette, session) + end + + def self.average_score(tartiflette) + tartiflette.scores.average(:value).to_f + end + + def self.average_score_by_category(tartiflette) + tartiflette + .scores + .group_by { |score| score.scoring_criterium.category } + .transform_values do |scores| + (scores.sum(&:value).to_f / scores.size).round(2) + end + end + + def self.total_score_by_category(tartiflette) + tartiflette + .scores + .group_by { |score| score.scoring_criterium.category } + .transform_values do |scores| + (scores.sum(&:value).to_f / scores.size).round(2) + end + end + + def self.leaderboard + Tartiflette + .joins(:scores) + .select("tartiflettes.*, SUM(scores.value) AS total_score") + .group("tartiflettes.id") + .order("total_score DESC") + .map { |tartiflette| [ tartiflette, tartiflette.total_score.to_f ] } + end +end |