<commit_before>class SubmissionsController < ApplicationController
  skip_before_filter :verify_authenticity_token
  respond_to :json

  def index
    submissions = Submission.find_all_by_assignment_id(params[:assignment_id])
    person_submissions = []
    submissions.each do |submission|
      person = Person.find(submission.person_id)
      person_submissions.push('person' => person, 'submission' => submission)
    end
    respond_with(person_submissions)
  rescue ActiveRecord::RecordNotFound
    raise 'Assignment not found'
  end

  def create
    submission = Submission.create(submission_params)
    respond_with(submission)
   rescue ActiveRecord::RecordInvalid => invalid
    puts invalid.record.errors
  end

  def show
    submission = Submission.find(params[:id])
    person = Person.find(submission.person_id)
    person_submission = { 'person' => person, 'submission' => submission }
    respond_with(person_submission)
  rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  def submission_files
    files = SubmissionFile.find_all_by_submission_id(params[:submission_id])
    respond_with(files)
  rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  def update_feedback_released
    submission = Submission.find(params[:submission_id])
    submission.update_attribute(:feedback_released, params[:feedback_released])

    # NOTE: Rails is really weird with PUT requests and
    # defaults to sending Code 204: No Content responses
    # upon respond_with calls. To respond with an actual
    # resource, we break from our respond_with paradigm
    # and render json instead. You will have to do this
    # for every PUT request.

    render json: submission
 rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  private

  def submission_params
    params.permit(:person_id, :assignment_id, :feedback_released, :date_submitted)
  end
end<commit_msg>add better error messages for submissions_controller<commit_after>class SubmissionsController < ApplicationController
  skip_before_filter :verify_authenticity_token
  respond_to :json

  def index
    begin
      submissions = Submission.find_all_by_assignment_id(params[:assignment_id])
    rescue ActiveRecord::RecordNotFound
      raise "No submissions with assignment id #{params[:assignment_id]} found"
    end

    person_submissions = people_for_submissions submissions
    respond_with person_submissions
  end

  def create
    submission = Submission.create(submission_params)
    respond_with(submission)
   rescue ActiveRecord::RecordInvalid => invalid
    puts invalid.record.errors
  end

  def show
    submission = Submission.find(params[:id])
    person = Person.find(submission.person_id)
    person_submission = { 'person' => person, 'submission' => submission }
    respond_with(person_submission)
  rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  def submission_files
    files = SubmissionFile.find_all_by_submission_id(params[:submission_id])
    respond_with(files)
  rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  def update_feedback_released
    submission = Submission.find(params[:submission_id])
    submission.update_attribute(:feedback_released, params[:feedback_released])

    # NOTE: Rails is really weird with PUT requests and
    # defaults to sending Code 204: No Content responses
    # upon respond_with calls. To respond with an actual
    # resource, we break from our respond_with paradigm
    # and render json instead. You will have to do this
    # for every PUT request.

    render json: submission
 rescue ActiveRecord::RecordNotFound
    raise 'Submission not found'
  end

  private

  def people_for_submissions(submissions)
    person_submissions = []
    submissions.each do |submission|
      begin
        person = Person.find(submission.person_id)
      rescue ActiveRecord::RecordNotFound
        raise "When finding submissions for assignment #{params[:assignment_id]}"\
          ", no person with ID #{submission.person_id} found."
      end

      person_submissions.push('person' => person, 'submission' => submission)
    end
    person_submissions
  end

  def submission_params
    params.permit(:person_id, :assignment_id, :feedback_released, :date_submitted)
  end
end
{'fraction_non_alphanumeric': 0.0700299470168164, 'fraction_numerical': 0.00138217000691085, 'mean_word_length': 4.275820170109356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1998647', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1190, 'n_words': 321}