<commit_before>class PlacementsController < ApplicationController
  before_action :set_refugee, only: [
    :new, :create, :edit, :move_out, :update, :move_out_update, :destroy]
  before_action :set_placement, only: [
    :edit, :move_out, :update, :move_out_update, :destroy]

  def new
    @placement = @refugee.placements.new
    @pre_selected = LegalCode.where(pre_selected: true).first.try(:id)
    authorize! :create, @placement
  end

  def edit
    @pre_selected = @placement.legal_code_id
    authorize! :edit, @placement
  end

  def move_out
    authorize! :edit, @placement
  end

  def create
    @placement = @refugee.placements.new(placement_params)
    authorize! :create, @placement

    logger.debug params
    logger.debug placement_params

    if @placement.save
      redirect_to @refugee, notice: 'Placeringen registrerades'
    else
      render :new
    end
  end

  def update
    authorize! :update, @placement
    if @placement.update(placement_params)
      redirect_to @refugee, notice: 'Placeringen uppdaterades'
    else
      render :edit
    end
  end

  def move_out_update
    authorize! :edit, @placement
    if @placement.update(placement_params)
      redirect_to @refugee, notice: 'Placeringen uppdaterades'
    else
      render :move_out
    end
  end

  def destroy
    Placement.find(params[:id]).destroy
    redirect_to @refugee, notice: 'Placeringen raderades'
  end

  private
    def set_refugee
      @refugee = Refugee.find(params[:refugee_id])
      @homes = Home.where(active: true)
    end

    def set_placement
      @refugee = Refugee.find(params[:refugee_id])
      id = params[:id] || params[:placement_id]
      @placement = @refugee.placements.find(id)
      @homes = Home.where(active: true)
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def placement_params
      home = @homes.where(id: params[:placement][:home_id]).first
      params[:specification] == '' if home.present? && home.use_placement_specification
      params[:cost] == nil if home.present? && home.use_placement_cost
<commit_msg>Fix for pre-selected legal code<commit_after>class PlacementsController < ApplicationController
  before_action :set_refugee, only: [
    :new, :create, :edit, :move_out, :update, :move_out_update, :destroy]
  before_action :set_placement, only: [
    :edit, :move_out, :update, :move_out_update, :destroy]

  def new
    @placement = @refugee.placements.new
    @pre_selected = default_legal_code
    authorize! :create, @placement
  end

  def edit
    @pre_selected = @placement.legal_code_id || default_legal_code
    authorize! :edit, @placement
  end

  def move_out
    authorize! :edit, @placement
  end

  def create
    @placement = @refugee.placements.new(placement_params)
    authorize! :create, @placement

    logger.debug params
    logger.debug placement_params

    if @placement.save
      redirect_to @refugee, notice: 'Placeringen registrerades'
    else
      render :new
    end
  end

  def update
    authorize! :update, @placement
    if @placement.update(placement_params)
      redirect_to @refugee, notice: 'Placeringen uppdaterades'
    else
      render :edit
    end
  end

  def move_out_update
    authorize! :edit, @placement
    if @placement.update(placement_params)
      redirect_to @refugee, notice: 'Placeringen uppdaterades'
    else
      render :move_out
    end
  end

  def destroy
    Placement.find(params[:id]).destroy
    redirect_to @refugee, notice: 'Placeringen raderades'
  end

  private
    def set_refugee
      @refugee = Refugee.find(params[:refugee_id])
      @homes = Home.where(active: true)
    end

    def set_placement
      @refugee = Refugee.find(params[:refugee_id])
      id = params[:id] || params[:placement_id]
      @placement = @refugee.placements.find(id)
      @homes = Home.where(active: true)
    end

    def default_legal_code
      LegalCode.where(pre_selected: true).first.try(:id)
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def placement_params
      home = @homes.where(id: params[:placement][:home_id]).first
      params[:specification] == '' if home.present? && home.use_placement_specification
      params[:cost] == nil if home.present? && home.use_placement_cost

{'fraction_non_alphanumeric': 0.09826589595375723, 'fraction_numerical': 0.0, 'mean_word_length': 3.473629782833506, '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': '962673', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1378, 'n_words': 331}