diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-01-01 17:49:39 +0100 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-01-01 17:49:39 +0100 |
commit | e5bf7d1527491af4663c203ec17697e8855b650f (patch) | |
tree | 09f32d4ab8eecb22f1d4b4262f6e02769e645506 /app/controllers/scores_controller.rb | |
parent | 4d61303eb58cf32f95052227c3158a392dd23373 (diff) |
Cleaned up scores controller and tartiflette scoring service.
Diffstat (limited to 'app/controllers/scores_controller.rb')
-rw-r--r-- | app/controllers/scores_controller.rb | 37 |
1 files changed, 14 insertions, 23 deletions
diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 8d494e4..8754b91 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -1,24 +1,12 @@ class ScoresController < ApplicationController allow_unauthenticated_access - before_action :set_tartiflette, only: [ :new, :create, :edit_all, :update_all ] - before_action :scores_params, only: [ :create, :update_all ] + before_action :set_tartiflette, only: [:new, :create, :edit_all, :update_all] def new end def create - if TartifletteScoringService.scored?(@tartiflette, session) - redirect_to root_path, alert: "Vous avez déja noté cette tartiflette." - return - end - - TartifletteScoringService.submit_scores(@tartiflette, scores_params, session) - redirect_to root_path, - notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été enregistrés." - rescue StandardError => e - redirect_to root_path, - status: :unprocessable_entity, - alert: "Erreur lors de l'enregistrement de vos scores : #{e.message}" + handle_scoring(:submit_scores, "enregistrement") end def edit_all @@ -26,15 +14,7 @@ class ScoresController < ApplicationController end def update_all - scores_params.each do |score_id, score_params| - score = @tartiflette.scores.find(score_id) - score.update!(value: score_params[:value]) - end - redirect_to root_path, - notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été mis à jour." - rescue StandardError => e - redirect_to root_path, - alert: "Erreur lors de l'enregistrement de vos scores : #{e.message}" + handle_scoring(:update_scores, "mise à jour") end private @@ -46,4 +26,15 @@ class ScoresController < ApplicationController def scores_params params.require(:scores).permit!.to_h end + + def handle_scoring(service_method, action) + TartifletteScoringService.send(service_method, @tartiflette, scores_params, session) + redirect_to root_path, notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été #{action}." + rescue StandardError => e + handle_error("Erreur lors de #{action} de vos scores : #{e.message}") + end + + def handle_error(message) + redirect_to root_path, status: :unprocessable_entity, alert: message + end end |