<commit_before>class RecipesController < ApplicationController
  def show
    @recipe = Recipe.find(params[:id])
    respond_to do |format|
      format.html
      format.pdf do
        filename   = @recipe.title.parameterize + ".pdf"
        markdown   = "# #{@recipe.title}\n\n#{@recipe.instructions}"
        pdf_output = Kramdown::Document.new(markdown).to_pdf
        send_data  pdf_output, filename: filename, type: 'application/pdf'
      end
    end
  end
  
  def new
    @recipe = Recipe.new
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      redirect_to root_path
    else
      render :new
    end
  end

  def edit
    @recipe = Recipe.find(params[:id])
  end

  def update
    @recipe = Recipe.find(params[:id])
    if @recipe.update_attributes(recipe_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.update_attributes(archived: true)
    flash[:alert] = %Q(
      You have deleted the recipe "#{@recipe.title}". Please
      <a href="#{restore_recipe_path(@recipe.id)}">click here</a> to restore it.
    )
    redirect_to root_path
  end

  def restore
    @recipe = Recipe.find(params[:id])
    @recipe.update_attributes(archived: false)
    flash[:notice] = "You have successfully restored \"#{@recipe.title}\"."
    redirect_to root_path
  end

  def print
    @recipe = Recipe.find(params[:id])
  end

  private

  def recipe_params
    new_params = params.require(:recipe).permit!
    new_params.merge(recipe_categories)
  end

  def recipe_categories
    categories = params[:categories].map { |c| Category.find_or_create_by(name: c).id }
    { category_ids: categories }
  end
end<commit_msg>Fix - save recipe with no categories<commit_after>class RecipesController < ApplicationController
  def show
    @recipe = Recipe.find(params[:id])
    respond_to do |format|
      format.html
      format.pdf do
        filename   = @recipe.title.parameterize + ".pdf"
        markdown   = "# #{@recipe.title}\n\n#{@recipe.instructions}"
        pdf_output = Kramdown::Document.new(markdown).to_pdf
        send_data  pdf_output, filename: filename, type: 'application/pdf'
      end
    end
  end
  
  def new
    @recipe = Recipe.new
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      redirect_to root_path
    else
      render :new
    end
  end

  def edit
    @recipe = Recipe.find(params[:id])
  end

  def update
    @recipe = Recipe.find(params[:id])
    if @recipe.update_attributes(recipe_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.update_attributes(archived: true)
    flash[:alert] = %Q(
      You have deleted the recipe "#{@recipe.title}". Please
      <a href="#{restore_recipe_path(@recipe.id)}">click here</a> to restore it.
    )
    redirect_to root_path
  end

  def restore
    @recipe = Recipe.find(params[:id])
    @recipe.update_attributes(archived: false)
    flash[:notice] = "You have successfully restored \"#{@recipe.title}\"."
    redirect_to root_path
  end

  def print
    @recipe = Recipe.find(params[:id])
  end

  private

  def recipe_params
    new_params = params.require(:recipe).permit!
    new_params.merge(recipe_categories)
  end

  def recipe_categories
    categories = 
      if params[:categories]
        params[:categories].map { |c| Category.find_or_create_by(name: c).id }
      else
        []
    end
    { category_ids: categories }
  end
end
{'fraction_non_alphanumeric': 0.11008661637328863, 'fraction_numerical': 0.0, 'mean_word_length': 2.8577586206896552, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1993504', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1176, 'n_words': 262}