Class: Workarea::Address
- Inherits:
-
Object
- Object
- Workarea::Address
- Includes:
- ApplicationDocument
- Defined in:
- app/models/workarea/address.rb
Direct Known Subclasses
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
-
#address_eql?(address) ⇒ Boolean
If this address is the same as another address.
-
#allow_po_box? ⇒ Boolean
Whether or not to allow a PO BOX address.
- #as_json(*args) ⇒ Object
-
#country=(value) ⇒ Object
Ensures the following situations work: address.country = ‘US’ address.country = ‘USA’ address.country = ‘United States of America’ address.country = Country.
-
#phone_number=(val) ⇒ void
Sets the phone number of the address, while removing any non-digit characters.
-
#po_box? ⇒ Boolean
Whether the address is a PO BOX address.
-
#region_name ⇒ String
Returns the full name for the region.
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Methods included from Mongoid::Document
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.
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.
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
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.
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
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_name ⇒ String
Returns the full name for the region
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 |