<commit_before>class NotesController < ApplicationController
  before_filter :authorize_for_ta_and_admin
  before_filter :ensure_can_modify, :only => [:edit, :update]

  # TODO this method needs explaining ! What is return_id ?
  def notes_dialog
    @return_id = params[:id]
    @cls = params[:noteable_type]
    @noteable = Kernel.const_get(@cls).find_by_id(params[:noteable_id])
    @cont = params[:controller_to]
    @action = params[:action_to]
    @highlight_field = params[:highlight_field]
    @number_of_notes_field = params[:number_of_notes_field]

    @notes = Note.all(:conditions => {:noteable_id => @noteable.id,
                                      :noteable_type => @noteable.class.name})
    render :partial => 'notes/modal_dialogs/notes_dialog_script',
      :formats => [:js]
  end

  def add_note
    return unless request.post?
    @note = Note.new
    @note.creator_id = @current_user.id
    @note.notes_message = params[:new_notes]
    @note.noteable_id = params[:noteable_id]
    @note.noteable_type = params[:noteable_type]
    unless @note.save
      render 'notes/modal_dialogs/notes_dialog_error', :formats => [:js]
    else
      @note.reload
      @number_of_notes_field = params[:number_of_notes_field]
      @highlight_field = params[:highlight_field]
      @number_of_notes = @note.noteable.notes.size
      render 'notes/modal_dialogs/notes_dialog_success', :formats => [:js]
    end
  end

  def index
    @notes = Note.all(:order => "created_at DESC", :include => [:user, :noteable])
    @current_user = current_user
    # Notes are attached to noteables, if there are no noteables, we can't make notes.
    @noteables_available = Note.noteables_exist?
		render 'index', :formats => [:js]
  end

  # gets the objects for groupings on first load.
  def new
    new_retrieve
    @note = Note.new
		render 'new', :formats => [:js]
  end

  def create
    @note = Note.new(params[:note])
    @note.noteable_type = params[:noteable_type]
    @note.creator_id = @current_user.id

    if @note.save
      flash[:success] = I18n.t('notes.create.success')
      redirect_to :action => 'index'
    else
      new_retrieve
      render :new
    end
  end

  # Used to update the values in the groupings dropdown in the new note form
  def new_update_groupings
    retrieve_groupings(Assignment.find(params[:assignment_id]))
    render 'new_update_groupings', :formats => [:js]
  end

  # used for RJS call
  def noteable_object_selector
    case params[:noteable_type]
      when 'Student'
        @students = Student.all(:order => 'user_name')
      when 'Assignment'
        @assignments = Assignment.all
      when 'Grouping'
        new_retrieve
      else
        # default to groupings if all else fails.
        params[:noteable_type] = 'Grouping'
        flash[:error] = I18n.t('notes.new.invalid_selector')
        new_retrieve
    end
		render 'noteable_object_selector', :formats => [:js]
  end

  def edit
		render 'edit', :formats => [:js]
  end

  def update
    if @note.update_attributes(params[:note])
      flash[:success] = I18n.t('notes.update.success')
      redirect_to :action => 'index'
    else
      render :edit
    end
  end

  def destroy
    @note = Note.find(params[:id])
    if @note.user_can_modify?(current_user)
      @note.destroy
      flash[:success] = I18n.t('notes.delete.success')
    else
      flash[:error] = I18n.t('notes.delete.error_permissions')
    end
	  render 'destroy', :formats => [:js]
  end

  private
    def retrieve_groupings(assignment)
      if assignment.nil?
        @groupings = Array.new
        return
      end
      @groupings = Grouping.find_all_by_assignment_id(assignment.id, :include => [:group, {:student_memberships => :user}])
    end

    def new_retrieve
      @assignments = Assignment.all
      retrieve_groupings(@assignments.first)
    end

    # Renders a 404 error if the current user can't modify the given note.
    def ensure_can_modify
      @note = Note.find(params[:id])

      unless @note.user_can_modify?(current_user)
        render 'shared/http_status', :formats => [:html], :locals => { :code => '404', :message => HttpStatusHelper::ERROR_CODE['message']['404'] }, :status => 404, :layout => false
      end
    end

end<commit_msg>Fix failing specs in notes_controller<commit_after>class NotesController < ApplicationController
  before_filter :authorize_for_ta_and_admin
  before_filter :ensure_can_modify, :only => [:edit, :update]

  # TODO this method needs explaining ! What is return_id ?
  def notes_dialog
    @return_id = params[:id]
    @cls = params[:noteable_type]
    @noteable = Kernel.const_get(@cls).find_by_id(params[:noteable_id])
    @cont = params[:controller_to]
    @action = params[:action_to]
    @highlight_field = params[:highlight_field]
    @number_of_notes_field = params[:number_of_notes_field]

    @notes = Note.all(:conditions => {:noteable_id => @noteable.id,
                                      :noteable_type => @noteable.class.name})
    render :partial => 'notes/modal_dialogs/notes_dialog_script',
      :formats => [:js], :handlers => [:erb]
  end

  def add_note
    return unless request.post?
    @note = Note.new
    @note.creator_id = @current_user.id
    @note.notes_message = params[:new_notes]
    @note.noteable_id = params[:noteable_id]
    @note.noteable_type = params[:noteable_type]
    unless @note.save
      render 'notes/modal_dialogs/notes_dialog_error',
        :formats => [:js], :handlers => [:erb]
    else
      @note.reload
      @number_of_notes_field = params[:number_of_notes_field]
      @highlight_field = params[:highlight_field]
      @number_of_notes = @note.noteable.notes.size
      render 'notes/modal_dialogs/notes_dialog_success',
        :formats => [:js], :handlers => [:erb]
    end
  end

  def index
    @notes = Note.all(:order => "created_at DESC", :include => [:user, :noteable])
    @current_user = current_user
    # Notes are attached to noteables, if there are no noteables, we can't make notes.
    @noteables_available = Note.noteables_exist?
		render 'index', :formats => [:html]
  end

  # gets the objects for groupings on first load.
  def new
    new_retrieve
    @note = Note.new
		render 'new', :formats => [:html], :handlers => [:erb]
  end

  def create
    @note = Note.new(params[:note])
    @note.noteable_type = params[:noteable_type]
    @note.creator_id = @current_user.id

    if @note.save
      flash[:success] = I18n.t('notes.create.success')
      redirect_to :action => 'index'
    else
      new_retrieve
      render 'new', :formats => [:html], :handlers => [:erb]
    end
  end

  # Used to update the values in the groupings dropdown in the new note form
  def new_update_groupings
    retrieve_groupings(Assignment.find(params[:assignment_id]))
    render 'new_update_groupings', :formats => [:js], :handlers => [:erb]
  end

  # used for RJS call
  def noteable_object_selector
    case params[:noteable_type]
      when 'Student'
        @students = Student.all(:order => 'user_name')
      when 'Assignment'
        @assignments = Assignment.all
      when 'Grouping'
        new_retrieve
      else
        # default to groupings if all else fails.
        params[:noteable_type] = 'Grouping'
        flash[:error] = I18n.t('notes.new.invalid_selector')
        new_retrieve
    end
		render 'noteable_object_selector', :formats => [:js], :handlers => [:erb]
  end

  def edit
		render 'edit', :formats => [:html], :handlers => [:erb]
  end

  def update
    if @note.update_attributes(params[:note])
      flash[:success] = I18n.t('notes.update.success')
      redirect_to :action => 'index'
    else
      render 'edit', :formats => [:html], :handlers => [:erb]
    end
  end

  def destroy
    @note = Note.find(params[:id])
    if @note.user_can_modify?(current_user)
      @note.destroy
      flash[:success] = I18n.t('notes.delete.success')
    else
      flash[:error] = I18n.t('notes.delete.error_permissions')
    end
	  render 'destroy', :formats => [:js], :handlers => [:erb]
  end

  private
    def retrieve_groupings(assignment)
      if assignment.nil?
        @groupings = Array.new
        return
      end
      @groupings = Grouping.find_all_by_assignment_id(assignment.id,
        :include => [:group, {:student_memberships => :user}])
    end

    def new_retrieve
      @assignments = Assignment.all
      retrieve_groupings(@assignments.first)
    end

    # Renders a 404 error if the current user can't modify the given note.
    def ensure_can_modify
      @note = Note.find(params[:id])

      unless @note.user_can_modify?(current_user)
        render 'shared/http_status', :formats => [:html], :status => 404,
          :layout => false, :locals => {
            :code => '404', :message => HttpStatusHelper::ERROR_CODE['message']['404']
          }
      end
    end

end
{'fraction_non_alphanumeric': 0.12231517069862084, 'fraction_numerical': 0.004973999547818223, 'mean_word_length': 3.1554720526068576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5982886', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2871, 'n_words': 645}