Sha256: b5b3a9ca4f00acca92439ec265df6efdbfd78803d8a551c2a2d8ec4786307c30

Contents?: true

Size: 1.26 KB

Versions: 22

Compression:

Stored size: 1.26 KB

Contents

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

      # ADDRESS_TYPES = [BASIC_ADDRESS, SHIPPING_ADDRESS, DELIVERY_ADDRESS, INVOICING_ADDRESS].freeze

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

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

      validate :validate_default

      def validate_default
        return unless client && address_type && default

        default_count = if new_record?
                          ClientAddress.where(address_type: address_type, client: client, default: true).count
                        else
                          ClientAddress.where(address_type: address_type, client: client, default: true)
                                       .where.not(id: id).count
                        end

        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

22 entries across 22 versions & 1 rubygems

Version Path
comee_core-0.3.6 app/models/comee/core/client_address.rb
comee_core-0.3.5 app/models/comee/core/client_address.rb