<commit_before>class AssignmentsController < ApplicationController

  before_action :set_case, only: [:new, :create, :edit, :accept_or_reject, :rejected]
  before_action :set_assignment, only: [:edit, :accept_or_reject]
  before_action :validate_response, only: :accept_or_reject

  def new
    @assignment = @case.assignments.new
  end

  def create
    @assignment = @case.create_assignment(
      assignment_params.merge(assignment_type: 'drafter', assigner: current_user)
    )

    if @assignment.valid?
      flash[:notice] = t('.case_created')
      redirect_to cases_path
    else
      render :new
    end
  end

  def edit
    if @assignment
      if already_accepted?
        redirect_to case_path @case, accepted_now: false
      else
        render :edit
      end
    elsif @assignment.nil? && already_rejected?
      redirect_to case_assignments_rejected_path @case, rejected_now: false
    end
  end

  def accept_or_reject
    if accept?
      @assignment.accept
      redirect_to case_path @assignment.case, accepted_now: true
    elsif valid_reject?
      @assignment.reject assignment_params[:reasons_for_rejection]
      redirect_to case_assignments_rejected_path @case, rejected_now: true
    else
      @assignment.assign_and_validate_state(assignment_params[:state])
      render :edit
    end
  end

  def rejected
    @rejected_now = params[:rejected_now]
  end

  private

  def assignment_params
    params.require(:assignment).permit(
      :state,
      :assignee_id,
      :reasons_for_rejection
    )
  end

  def set_assignment
    if Assignment.exists?(id: params[:id])
      @assignment = Assignment.find(params[:id])
    end
  end

  def already_rejected?
    rejection = @case.transitions.detect do |transition|
      transition.assignment_id == params[:id].to_i && transition.event =='reject_responder_assignment'
    end
    rejection.present?
  end

  def already_accepted?
    Assignment.where(id: params[:id], state: 'accepted').present?
  end

  def set_case
    @case = Case.find(params[:case_id])
  end

  def validate_response
    if assignment_params[:state].nil?
      @assignment.errors.add(:state, :blank)
    end
  end

  def accept?<commit_msg>CT-363 use .any? instead of .detect to find out if a case assignment has been rejected<commit_after>class AssignmentsController < ApplicationController

  before_action :set_case, only: [:new, :create, :edit, :accept_or_reject, :rejected]
  before_action :set_assignment, only: [:edit, :accept_or_reject]
  before_action :validate_response, only: :accept_or_reject

  def new
    @assignment = @case.assignments.new
  end

  def create
    @assignment = @case.create_assignment(
      assignment_params.merge(assignment_type: 'drafter', assigner: current_user)
    )

    if @assignment.valid?
      flash[:notice] = t('.case_created')
      redirect_to cases_path
    else
      render :new
    end
  end

  def edit
    if @assignment
      if already_accepted?
        redirect_to case_path @case, accepted_now: false
      else
        render :edit
      end
    elsif @assignment.nil? && already_rejected?
      redirect_to case_assignments_rejected_path @case, rejected_now: false
    end
  end

  def accept_or_reject
    if accept?
      @assignment.accept
      redirect_to case_path @assignment.case, accepted_now: true
    elsif valid_reject?
      @assignment.reject assignment_params[:reasons_for_rejection]
      redirect_to case_assignments_rejected_path @case, rejected_now: true
    else
      @assignment.assign_and_validate_state(assignment_params[:state])
      render :edit
    end
  end

  def rejected
    @rejected_now = params[:rejected_now]
  end

  private

  def assignment_params
    params.require(:assignment).permit(
      :state,
      :assignee_id,
      :reasons_for_rejection
    )
  end

  def set_assignment
    if Assignment.exists?(id: params[:id])
      @assignment = Assignment.find(params[:id])
    end
  end

  def already_rejected?
    @case.transitions.any? do |transition|
      transition.assignment_id == params[:id].to_i && transition.event =='reject_responder_assignment'
    end
  end

  def already_accepted?
    Assignment.where(id: params[:id], state: 'accepted').present?
  end

  def set_case
    @case = Case.find(params[:case_id])
  end

  def validate_response
    if assignment_params[:state].nil?
      @assignment.errors.add(:state, :blank)
    end
  end

  def accept?
{'fraction_non_alphanumeric': 0.08762420957542909, 'fraction_numerical': 0.0006775067750677507, 'mean_word_length': 3.4245754245754245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1071061', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1399, 'n_words': 314}