View raw

1 class ScoresController < ApplicationController 2 allow_unauthenticated_access 3 before_action :set_tartiflette, only: [ :new, :create, :edit_all, :update_all ] 4 before_action :ensure_session_id 5 6 def new 7 end 8 9 def create 10 handle_scoring(:submit_scores, "enregistrement") 11 end 12 13 def edit_all 14 @scores = @tartiflette.scores.where(session_id: session[:id]) 15 end 16 17 def update_all 18 handle_scoring(:update_scores, "mise à jour") 19 end 20 21 private 22 23 def set_tartiflette 24 @tartiflette = Tartiflette.find(params[:tartiflette_id]) 25 end 26 27 def ensure_session_id 28 session[:id] ||= SecureRandom.uuid 29 end 30 31 def scores_params 32 params.require(:scores).permit!.to_h 33 end 34 35 def handle_scoring(service_method, action) 36 TartifletteScoringService.send(service_method, @tartiflette, scores_params, session) 37 redirect_to root_path, notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été #{action}." 38 rescue StandardError => e 39 handle_error("Erreur lors de #{action} de vos scores : #{e.message}") 40 end 41 42 def handle_error(message) 43 redirect_to root_path, status: :unprocessable_entity, alert: message 44 end 45 end 46