Class: Workarea::Address

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/address.rb

Constant Summary collapse

ATTRIBUTES_FOR_LENGTH_VALIDATION =
[
  :first_name,
  :last_name,
  :company,
  :street,
  :street_2,
  :city,
  :region,
  :postal_code,
  :country,
  :phone_number,
  :phone_extension
]

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Method Details

#address_eql?(address) ⇒ Boolean

If this address is the same as another address. Addresses are equal if the address-relevant attributes are the same, ignoring case. Used to check whether we should save an additional address for a user.

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'app/models/workarea/address.rb', line 97

def address_eql?(address)
  Workarea.config.address_attributes.inject(true) do |memo, attr|
    other = address.send(attr).to_s.downcase.gsub(/\s+/, '')
    selfs = self.send(attr).to_s.downcase.gsub(/\s+/, '')

    memo && other == selfs
  end
end

#allow_po_box?Boolean

Whether or not to allow a PO BOX address. Can be overridden in subclasses to configure this behavior.

Returns:

  • (Boolean)

    If the address can be a PO BOX



77
78
79
# File 'app/models/workarea/address.rb', line 77

def allow_po_box?
  true
end

#as_json(*args) ⇒ Object



115
116
117
118
119
# File 'app/models/workarea/address.rb', line 115

def as_json(*args)
  super.tap do |hash|
    hash['country'] = self.country.try(:alpha2)
  end
end

#country=(value) ⇒ Object

Ensures the following situations work: address.country = ‘US’ address.country = ‘USA’ address.country = ‘United States of America’ address.country = Country

Parameters:

  • value


68
69
70
# File 'app/models/workarea/address.rb', line 68

def country=(value)
  super(Country.search_for(value)&.alpha2)
end

#phone_number=(val) ⇒ void

This method returns an undefined value.

Sets the phone number of the address, while removing any non-digit characters.

Parameters:

  • value


52
53
54
55
56
57
58
# File 'app/models/workarea/address.rb', line 52

def phone_number=(val)
  if val.nil?
    super(val)
  else
    super(val.to_s.gsub(/\D/, ''))
  end
end

#po_box?Boolean

Whether the address is a PO BOX address

Returns:

  • (Boolean)

    If the address is PO BOX or not



85
86
87
88
# File 'app/models/workarea/address.rb', line 85

def po_box?
  street.present? && street.strip =~ Workarea.config.po_box_regex ||
    street_2.present? && street_2.strip =~ Workarea.config.po_box_regex
end

#region_nameString

Returns the full name for the region

Returns:



110
111
112
113
# File 'app/models/workarea/address.rb', line 110

def region_name
  return '' if region.blank?
  country.subdivisions[region].try(:name) || region
end