<commit_before>class ReviewsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_agreement,                  only: [ :show, :vote, :create_comment, :preview, :show_comment, :edit_comment, :update_comment, :destroy_comment ]
  before_action :redirect_without_agreement,     only: [ :show, :vote, :create_comment, :preview, :show_comment, :edit_comment, :update_comment, :destroy_comment ]

  before_action :set_editable_agreement_event,      only: [ :show_comment, :edit_comment, :update_comment, :destroy_comment ]
  before_action :redirect_without_agreement_event,  only: [ :show_comment, :edit_comment, :update_comment, :destroy_comment ]

  def index
    params[:order] = "agreements.last_submitted_at DESC" if params[:order].blank?
    @order = scrub_order(Agreement, params[:order], [:id])
    agreement_scope = current_user.reviewable_agreements.search(params[:search]).order(@order)
    agreement_scope = agreement_scope.where( status: params[:status] ) if params[:status].present?
    @agreements = agreement_scope.page(params[:page]).per( 40 )
  end

  def show
  end

  def vote
    @review = @agreement.reviews.where( user_id: current_user.id ).first_or_create
    original_approval = @review.approved
    @review.update approved: (params[:approved].to_s == '1') if ['0','1'].include?(params[:approved].to_s)

    event_type = if @review.approved == original_approval
      ''
    elsif @review.approved == true and original_approval == false
      'reviewer_changed_from_rejected_to_approved'
    elsif @review.approved == false and original_approval == true
      'reviewer_changed_from_approved_to_rejected'
    elsif @review.approved == true
      'reviewer_approved'
    elsif @review.approved == false
      'reviewer_rejected'
    end

    @agreement.agreement_events.create event_type: event_type, user_id: current_user.id, event_at: Time.now if event_type.present?

    redirect_to review_path(@agreement) + "#c#{@agreement.agreement_events.count}", notice: 'Review was successfully created.'
  end

  # def new
  #   @review = Review.new
  # end

  # def edit
  # end

  def preview
    @agreement_event = @agreement.agreement_events.new(agreement_event_params)
  end

  def create_comment
    @agreement_event = @agreement.agreement_events.where(user_id: current_user.id, event_at: Time.now, event_type: 'commented').new(agreement_event_params)

    respond_to do |format|
      if @agreement_event.save
        format.html { redirect_to review_path(@agreement) + "#c#{@agreement_event.number}", notice: 'Review was successfully created.' }
        format.json { render action: 'show', status: :created, location: @agreement_event }
      else
        format.html { render action: 'new' }
        format.json { render json: @agreement_event.errors, status: :unprocessable_entity }
      end
    end
  end

  def show_comment
  end

  def edit_comment
  end

  def update_comment
    respond_to do |format|
      if @agreement_event.update(agreement_event_params)
        format.html { redirect_to review_path(@agreement) + "#c#{@agreement_event.number}", notice: 'Comment was successfully updated.' }
        format.json { head :no_content }<commit_msg>Make sure submitting blank comments redirects well<commit_after>class ReviewsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_agreement,                  only: [ :show, :vote, :create_comment, :preview, :show_comment, :edit_comment, :update_comment, :destroy_comment ]
  before_action :redirect_without_agreement,     only: [ :show, :vote, :create_comment, :preview, :show_comment, :edit_comment, :update_comment, :destroy_comment ]

  before_action :set_editable_agreement_event,      only: [ :show_comment, :edit_comment, :update_comment, :destroy_comment ]
  before_action :redirect_without_agreement_event,  only: [ :show_comment, :edit_comment, :update_comment, :destroy_comment ]

  def index
    params[:order] = "agreements.last_submitted_at DESC" if params[:order].blank?
    @order = scrub_order(Agreement, params[:order], [:id])
    agreement_scope = current_user.reviewable_agreements.search(params[:search]).order(@order)
    agreement_scope = agreement_scope.where( status: params[:status] ) if params[:status].present?
    @agreements = agreement_scope.page(params[:page]).per( 40 )
  end

  def show
  end

  def vote
    @review = @agreement.reviews.where( user_id: current_user.id ).first_or_create
    original_approval = @review.approved
    @review.update approved: (params[:approved].to_s == '1') if ['0','1'].include?(params[:approved].to_s)

    event_type = if @review.approved == original_approval
      ''
    elsif @review.approved == true and original_approval == false
      'reviewer_changed_from_rejected_to_approved'
    elsif @review.approved == false and original_approval == true
      'reviewer_changed_from_approved_to_rejected'
    elsif @review.approved == true
      'reviewer_approved'
    elsif @review.approved == false
      'reviewer_rejected'
    end

    @agreement.agreement_events.create event_type: event_type, user_id: current_user.id, event_at: Time.now if event_type.present?

    redirect_to review_path(@agreement) + "#c#{@agreement.agreement_events.count}", notice: 'Review was successfully created.'
  end

  # def new
  #   @review = Review.new
  # end

  # def edit
  # end

  def preview
    @agreement_event = @agreement.agreement_events.new(agreement_event_params)
  end

  def create_comment
    @agreement_event = @agreement.agreement_events.where(user_id: current_user.id, event_at: Time.now, event_type: 'commented').new(agreement_event_params)

    respond_to do |format|
      if @agreement_event.save
        format.html { redirect_to review_path(@agreement) + "#c#{@agreement_event.number}", notice: 'Review was successfully created.' }
        format.json { render action: 'show', status: :created, location: @agreement_event }
      else
        format.html { redirect_to review_path(@agreement) + "#c#{@agreement.agreement_events.count}" }
        format.json { render json: @agreement_event.errors, status: :unprocessable_entity }
      end
    end
  end

  def show_comment
  end

  def edit_comment
  end

  def update_comment
    respond_to do |format|
      if @agreement_event.update(agreement_event_params)
        format.html { redirect_to review_path(@agreement) + "#c#{@agreement_event.number}", notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
{'fraction_non_alphanumeric': 0.10915223056875671, 'fraction_numerical': 0.0015330369461904031, 'mean_word_length': 4.463986599664992, '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': '4995394', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1936, 'n_words': 426}