Sha256: 609d26651b7ac406938bcf8175e44e7dc48e158f7a46d78135d1c93ee0e671ee

Contents?: true

Size: 1.45 KB

Versions: 39

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Renalware
  class Address < ApplicationRecord
    belongs_to :country, class_name: "System::Country"
    validates :email, email: true, allow_blank: true
    delegate :uk?, to: :country, allow_nil: true

    belongs_to :addressable, polymorphic: true

    # Set to true to avoid address validation - useful when archiving a letter with a migrated
    # address that might not have a postcode for instance
    attr_accessor :skip_validation
    validates_with AddressValidator, unless: :skip_validation

    def self.reject_if_blank
      lambda { |attrs|
        %w(name organisation_name street_1 street_2 street_3 town county postcode)
          .all? { |a| attrs[a].blank? }
      }
    end

    def copy_from(source)
      return self if source.blank?

      self.name = source.name
      self.organisation_name = source.organisation_name
      self.street_1 = source.street_1
      self.street_2 = source.street_2
      self.street_3 = source.street_3
      self.town = source.town
      self.county = source.county
      self.postcode = source.postcode
      self.country = source.country
      self
    end

    def to_s(format = nil)
      parts = [name, organisation_name, street_1, street_2, street_3, town, county]
      parts += [postcode, country] unless format == :without_postcode
      parts.reject(&:blank?).join(", ")
    end

    def street
      [street_1, street_2, street_3].reject(&:blank?).join(", ")
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
renalware-core-2.0.149 app/values/renalware/address.rb
renalware-core-2.0.148 app/values/renalware/address.rb
renalware-core-2.0.147 app/values/renalware/address.rb
renalware-core-2.0.146 app/values/renalware/address.rb
renalware-core-2.0.145 app/values/renalware/address.rb
renalware-core-2.0.144 app/values/renalware/address.rb
renalware-core-2.0.143 app/values/renalware/address.rb
renalware-core-2.0.142 app/values/renalware/address.rb
renalware-core-2.0.141 app/values/renalware/address.rb
renalware-core-2.0.140 app/values/renalware/address.rb
renalware-core-2.0.139 app/values/renalware/address.rb
renalware-core-2.0.138 app/values/renalware/address.rb
renalware-core-2.0.137 app/values/renalware/address.rb
renalware-core-2.0.136 app/values/renalware/address.rb
renalware-core-2.0.135 app/values/renalware/address.rb
renalware-core-2.0.134 app/values/renalware/address.rb
renalware-core-2.0.133 app/values/renalware/address.rb
renalware-core-2.0.132 app/values/renalware/address.rb
renalware-core-2.0.131 app/values/renalware/address.rb
renalware-core-2.0.130 app/values/renalware/address.rb