<commit_before>=begin
Currently the way address cards have been setup is that every user will
have at least one contact list when their account is created within Backstage.
All Address Cards will be created / edited through this contact list.
=end
class AddressCardsController < ApplicationController
  before_action :require_user
  respond_to :html, :json
  expose(:address_card, attributes: :address_card_params)
  expose(:address_cards, attributes: :address_card_params)
  expose(:user)
  expose(:contact_list) { user.contact_lists.first }

  def new
  end

  def show
    check_user
  end

  def edit
    check_user
  end

  def update
    if address_card.save
      redirect_to user_address_card_path(user, address_card), notice: 'Address Card was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def create
    present_contact_list = user.contact_lists.find(address_card_params[:contact_list_id])
    if contact_list && contact_list.address_cards << address_card
      present_contact_list.address_cards << address_card if present_contact_list.id != contact_list.id
      redirect_to user_contact_list_path(user, present_contact_list), notice: 'Address Card was successfully created.'
    else
      render action: 'new'
    end
  end

  def destroy
    address_card.destroy
    redirect_to address_cards_url, notice: 'Address Card was successfully destroyed.'
  end

  private

  def check_user
    if address_card.contact_lists.any? {|cl| cl.users.include?(current_user)}
      respond_with(address_card)
    else
      flash[:notice] = 'You need to login to view this Address Card'
      redirect_to login_path
    end
  end

  def address_card_params
    params.require(:address_card).permit(:address_card, :first_name, :last_name, :contact_list_id, emails_attributes: [:kind, :value, :_destroy, :id],
                                        addresses_attributes: [:line_1, :line_2, :line_3, :city, :province, :country, :postal_code, :_destroy, :id],
                                        phones_attributes: [:kind, :value, :_destroy, :id])
  end
end
<commit_msg>Find address card through user.  80 char formatting.<commit_after>=begin
Currently the way address cards have been setup is that every user will
have at least one contact list when their account is created within Backstage.
All Address Cards will be created / edited through this contact list.
=end
class AddressCardsController < ApplicationController
  before_action :require_user
  respond_to :html, :json
  expose(:user)
  expose(:address_card, attributes: :address_card_params) { user.address_card }
  expose(:address_cards, attributes: :address_card_params) do
    user.address_cards
  end
  expose(:contact_list) { user.contact_lists.first }

  def new
  end

  def show
    check_user
  end

  def edit
    check_user
  end

  def update
    if address_card.save
      redirect_to user_address_card_path(user, address_card),
        notice: 'Address Card was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def create
    present_contact_list = user.contact_lists.find(
      address_card_params[:contact_list_id])
    if contact_list && contact_list.address_cards << address_card
      if present_contact_list.id != contact_list.id
        present_contact_list.address_cards << address_card
      end
      redirect_to user_contact_list_path(user, present_contact_list),
        notice: 'Address Card was successfully created.'
    else
      render action: 'new'
    end
  end

  def destroy
    address_card.destroy
    redirect_to address_cards_url,
      notice: 'Address Card was successfully destroyed.'
  end

  private

  def check_user
    if address_card.contact_lists.any? {|cl| cl.users.include?(current_user)}
      respond_with(address_card)
    else
      flash[:notice] = 'You need to login to view this Address Card'
      redirect_to login_path
    end
  end

  def address_card_params
    params.require(:address_card).permit(
      :address_card, :first_name, :last_name, :contact_list_id,
      emails_attributes: [:kind, :value, :_destroy, :id],
      addresses_attributes: [:line_1, :line_2, :line_3, :city, :province,
        :country, :postal_code, :_destroy, :id],
      phones_attributes: [:kind, :value, :_destroy, :id]
    )
  end
end

{'fraction_non_alphanumeric': 0.0762081784386617, 'fraction_numerical': 0.0018587360594795538, 'mean_word_length': 3.6240601503759398, 'pattern_counts': {'":': 0, '<': 13, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708954', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1262, 'n_words': 368}