Sha256: 99268f4fe672f8350a368174ba7fbe4ab558d396791fbdecd6fbf60422068708

Contents?: true

Size: 812 Bytes

Versions: 29

Compression:

Stored size: 812 Bytes

Contents

module Comee
  module Core
    class ClientAddress < Address
      belongs_to :client

      validates :name, presence: true

      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

29 entries across 29 versions & 1 rubygems

Version Path
comee_core-0.3.56 app/models/comee/core/client_address.rb
comee_core-0.3.55 app/models/comee/core/client_address.rb
comee_core-0.3.54 app/models/comee/core/client_address.rb
comee_core-0.3.53 app/models/comee/core/client_address.rb
comee_core-0.3.51 app/models/comee/core/client_address.rb
comee_core-0.3.50 app/models/comee/core/client_address.rb
comee_core-0.3.49 app/models/comee/core/client_address.rb
comee_core-0.3.48 app/models/comee/core/client_address.rb
comee_core-0.3.47 app/models/comee/core/client_address.rb
comee_core-0.3.46 app/models/comee/core/client_address.rb
comee_core-0.3.45 app/models/comee/core/client_address.rb
comee_core-0.3.44 app/models/comee/core/client_address.rb
comee_core-0.3.43 app/models/comee/core/client_address.rb
comee_core-0.3.42 app/models/comee/core/client_address.rb
comee_core-0.3.41 app/models/comee/core/client_address.rb
comee_core-0.3.40 app/models/comee/core/client_address.rb
comee_core-0.3.39 app/models/comee/core/client_address.rb
comee_core-0.3.38 app/models/comee/core/client_address.rb
comee_core-0.3.37 app/models/comee/core/client_address.rb
comee_core-0.3.36 app/models/comee/core/client_address.rb