Sha256: aaff5db52606a6659fc73c2f805fc71272da3350339a61ebbe4e21a2fabff307

Contents?: true

Size: 976 Bytes

Versions: 3

Compression:

Stored size: 976 Bytes

Contents

module Comee
  module Core
    class ClientAddress < ApplicationRecord
      SHIPPING_ADDRESS = "Shipping Address".freeze
      DELIVERY_ADDRESS = "Delivery Address".freeze
      INVOICING_ADDRESS = "Invoicing Address".freeze

      ADDRESS_TYPES = [SHIPPING_ADDRESS, DELIVERY_ADDRESS, INVOICING_ADDRESS].freeze

      belongs_to :client
      belongs_to :country, -> { where(lookup_type: :country) }, class_name: "Comee::Core::Lookup"

      validates :name, :address_line1, :address_type, presence: true
      validates :address_type, inclusion: {in: ADDRESS_TYPES}

      validate :validate_default

      def validate_default
        return unless client && address_type

        default_count = ClientAddress.where(address_type: address_type, client: client, default: true).count
        return if default_count.zero?

        errors.add(:default, "cannot be set to true. There is already a default address for this client and address type.")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comee_core-0.2.91 app/models/comee/core/client_address.rb
comee_core-0.2.90 app/models/comee/core/client_address.rb
comee_core-0.2.89 app/models/comee/core/client_address.rb