<commit_before>module Surveillance
  module Response
    class AttemptsController < Surveillance::Response::BaseController
      def new
        attempt = if previous_attempt
          if previous_attempt.completed?
            attempt_already_completed!(previous_attempt) and return
          end
          flash[:success] = t("surveillance.attempts.previous_attempt_recovered")
          previous_attempt
        else
          Surveillance::Attempt.create(
            last_answered_section: -1,
            ip_address: request.remote_ip,
            survey: survey
          )
        end

        redirect_to edit_response_attempt_path(attempt)
      end

      def edit
        attempt_already_completed!(attempt) and return if attempt.completed?
        store_attempt!(attempt)
      end

      def update
        if attempt.save
          attempt.complete!
          if survey.last_page_description.presence
            redirect_to complete_response_attempt_path(attempt)
          else
            redirect_to survey_path(attempt.survey)
          end
        else
          flash[:error] = "Votre participation n'a pas pu être prise en compte.<br>" +
            "Merci de vérifier que tous les champs sont remplis correctement."
          render "new"
        end
      end

      def complete
      end

      private
<commit_msg>clean old answers in response/attempts#update<commit_after>module Surveillance
  module Response
    class AttemptsController < Surveillance::Response::BaseController
      def new
        attempt = if previous_attempt
          if previous_attempt.completed?
            attempt_already_completed!(previous_attempt) and return
          end
          flash[:success] = t("surveillance.attempts.previous_attempt_recovered")
          previous_attempt
        else
          Surveillance::Attempt.create(
            last_answered_section: -1,
            ip_address: request.remote_ip,
            survey: survey
          )
        end

        redirect_to edit_response_attempt_path(attempt)
      end

      def edit
        attempt_already_completed!(attempt) and return if attempt.completed?
        store_attempt!(attempt)
      end

      def update
        attempt.answers.destroy_all # Clean old answers
        if attempt.update_attributes(attempt_params)
          attempt.complete!
          if survey.last_page_description.presence
            redirect_to complete_response_attempt_path(attempt)
          else
            redirect_to survey_path(attempt.survey)
          end
        else
          flash[:error] = "Votre participation n'a pas pu être prise en compte.<br>" +
            "Merci de vérifier que tous les champs sont remplis correctement."
          render "new"
        end
      end

      def complete
      end

      private

{'fraction_non_alphanumeric': 0.05390931810067833, 'fraction_numerical': 0.0007140307033202428, 'mean_word_length': 2.075740944017563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4903026', 'n_tokens_mistral': 754, 'n_tokens_neox': 694, 'n_words': 176}