<commit_before>class LearningElementsController < ApplicationController

  def potential_capsules_for_path

    @learning_path = LearningPath.find(params[:path_id])
    if @learning_path.capsules.count > 0
      @potential_capsules = Capsule.where('id not in (?)', @learning_path.capsules.map(&:id))
    else
      puts 'Returning all capsules'
      @potential_capsules = Capsule.all
      puts ("Number of capsules returned: #{@potential_capsules.size}")
    end

    # Don't render layout when request is AJAX
    render :layout => !request.xhr?
  end


  def add_capsules_to_path
    puts 'add_capsules_to_path called!!'
    @learning_path = LearningPath.find(params[:path_id])
    capsules_hash = params[:learning_path]
    puts capsules_hash.class

    if capsules_hash.nil?
      redirect_to @learning_path, notice: 'No updates were made to the path!'
      return
    end

    selected_capsules = Capsule.find capsules_hash[:capsule_ids]
    position = @learning_path.capsules.size + 1

    selected_capsules.each{ |capsule|
      @learning_path.learning_path_elements.create(:capsule_id => capsule.id, :position => position)
      position = position + 1
    }

    redirect_to @learning_path, notice: 'Learning path was successfully updated.'

    end

end<commit_msg>Minor refactoring to use save and before_action<commit_after>class LearningElementsController < ApplicationController

  before_action :get_learning_path

  def potential_capsules_for_path

    if @learning_path.capsules.count > 0
      @potential_capsules = Capsule.where('id not in (?)', @learning_path.capsules.map(&:id))
    else
      puts 'Returning all capsules'
      @potential_capsules = Capsule.all
      puts ("Number of capsules returned: #{@potential_capsules.size}")
    end

    # Don't render layout when request is AJAX
    render :layout => !request.xhr?
  end


  def add_capsules_to_path
    redirect_to @learning_path, notice: 'No updates were made to the path!' and return if (params[:learning_path].nil?)

    position = @learning_path.capsules.size + 1
    selected_capsules = Capsule.find params[:learning_path][:capsule_ids]

    selected_capsules.each{ |capsule|
      @learning_path.learning_path_elements.build(:capsule_id => capsule.id, :position => position)
      position = position + 1
    }

    if(@learning_path.save)
      redirect_to @learning_path, notice: 'Learning path was successfully updated.'
    else
      redirect_to @learning_path, error: 'Learning path could not be updated!'
    end

  end

  private

      def get_learning_path
        @learning_path = LearningPath.find(params[:path_id])
      end

end
{'fraction_non_alphanumeric': 0.09028831562974203, 'fraction_numerical': 0.002276176024279211, 'mean_word_length': 3.80327868852459, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '3130241', 'n_tokens_mistral': 828, 'n_tokens_neox': 782, 'n_words': 194}