<commit_before>class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]

  # GET /contacts
  # GET /contacts.json
  def index
    @contacts = Contact.all
  end

  # GET /contacts/1
  # GET /contacts/1.json
  def show
  end

  # GET /contacts/new
  def new
    @contact = Contact.new

    # Init embedded documents
    @contact.phones.build
    @contact.emails.build
    @contact.addresses.build
  end

  # GET /contacts/1/edit
  def edit
  end

  # POST /contacts
  # POST /contacts.json
  def create
    @contact = Contact.new(contact_params)

    respond_to do |format|
      if @contact.save
        format.html do
          redirect_to @contact, notice: t('contact.success.create')
        end
        format.json { render :show, status: :created, location: @contact }
      else
        format.html do
          set_flash_errors
          render :new
        end
        format.json do
          render json: @contact.errors, status: :unprocessable_entity
        end
      end
    end
  end

  def update
    respond_to do |format|
      if @contact.update(contact_params)
        format.html do
          redirect_to @contact, notice: t('contact.success.update')
        end
        format.json { render :show, status: :ok, location: @contact }
      else
        format.html do
          set_flash_errors
          render :edit
        end
        format.json do
          render json: @contact.errors, status: :unprocessable_entity
        end
      end
    end
  end

  def destroy
    @contact.destroy
    redirect_to contacts_url, notice: t('contact.success.destroy')
  end

  private

  def set_contact
    @contact = Contact.find(params[:id])
  end

  def contact_params
    params
      .require(:contact)
      .permit(
        :firstname,
        :lastname,
        emails_attributes: [:id, :email],
        phones_attributes: [:id, :number],
        addresses_attributes: [
          :id,
          :street_address_1,
          :street_address_2,
          :city,
          :state,<commit_msg>Refactor contacts controller for DRYer code, same functionality (responds to json/html)<commit_after>class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]
  respond_to :html, :json

  def index
    @contacts = Contact.all
  end

  def show
  end

  def new
    @contact = Contact.new

    # Init embedded documents
    @contact.phones.build
    @contact.emails.build
    @contact.addresses.build
  end

  def edit
  end

  def create
    @contact = Contact.new(contact_params)
    flash[:notice] = t('contact.success.create') if @contact.save
    set_flash_errors

    respond_with(@contact)
  end

  def update
    flash[:notice] = t('contact.success.update') if @contact.update(contact_params)
    set_flash_errors

    respond_with(@contact)
  end

  def destroy
    @contact.destroy
    redirect_to contacts_url, notice: t('contact.success.destroy')
  end

  private

  def set_contact
    @contact = Contact.find(params[:id])
  end

  def contact_params
    params
      .require(:contact)
      .permit(
        :firstname,
        :lastname,
        emails_attributes: [:id, :email],
        phones_attributes: [:id, :number],
        addresses_attributes: [
          :id,
          :street_address_1,
          :street_address_2,
          :city,
          :state,
{'fraction_non_alphanumeric': 0.09176194664321313, 'fraction_numerical': 0.0020521841102316037, 'mean_word_length': 2.2998065764023212, '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': '5092768', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1026, 'n_words': 258}