Module: Mock::PhoneNumberConcern

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/mock/phone_number_concern.rb

Overview

This module provides a method that reformats our related phone numbers into a hash structure. This can be useful for many things, though our primary use is to facilitate generating JSON responses via JBuilder

Include this module into any model that has_many :phone_numbers.

Instance Method Summary collapse

Instance Method Details

#formatted_phone_numbers_hashObject

Organizes all of our associated PhoneNumbers into a hash structure, where the key is the label and the value is an Array of all raw phone number values that have that given label. This facilitates situations where for example, you have two home phone numbers.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/concerns/mock/phone_number_concern.rb', line 16

def formatted_phone_numbers_hash
  numbers_hash = {}
  phone_numbers.each do |phone_number_object|
    iterated_label = phone_number_object.label
    # Does the label already exist in our hash?
    if numbers_hash[iterated_label]
      # YES: Just add the number to the existing value array:
      numbers_hash[iterated_label] << phone_number_object.number
    else
      # NO: Create a new entry for this label:
      numbers_hash[iterated_label] = [phone_number_object.number]
    end
  end

  numbers_hash
end