Class: Phone

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Labelize
Defined in:
app/models/phone.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.canonicalize(digits_and_stuff) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/phone.rb', line 8

def self.canonicalize(digits_and_stuff)
  canonical = digits_and_stuff.gsub(" ", "")
  if canonical
    canonical = canonical[2..100].strip if canonical.starts_with?("+1")
    if self.const_defined?("PhonyRails")
      canonical = PhonyRails.normalize_number(digits_and_stuff, default_country_code: "US").phony_formatted(format: :international)
    else
      if canonical[0] != "+"
        digits = digits_and_stuff.gsub(/[^0-9]/, "")
        digits = digits[1..-1] if digits[0] == '1'
        digits = "805" + digits if digits.length == 7
        canonical = "(#{digits[0..2]}) #{digits[3..5]}-#{digits[6..10]}"
      end
    end
  end
  canonical
end

Instance Method Details

#canonicalizeObject



26
27
28
29
# File 'app/models/phone.rb', line 26

def canonicalize
  return if self.number == "76-BAFFLE-76"
  self.number = self.class.canonicalize(self.number) if self.number
end

#labelObject



31
32
33
34
35
36
37
38
39
# File 'app/models/phone.rb', line 31

def label
  db_label = super
  if not db_label
    self.label = "Work"
    db_label = super
  end

  db_label.value
end

#label=(name) ⇒ Object



41
42
43
44
# File 'app/models/phone.rb', line 41

def label=(name)
  super(Label.get(name))
  self.label
end