Sha256: fa432f60b50eb47667c484b8432e9bfb620489eb3c81899caf887b62135ab7ff

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module ProfileFieldTypes

  # Phone Number Field
  #
  class Phone < ProfileField
    def self.model_name; ProfileField.model_name; end

    before_save :auto_format_value

    def self.format_phone_number( phone_number_str )
      return "" if phone_number_str.nil?
      value = phone_number_str

      # determine whether this is an international number
      format = :national
      format = :international if value.start_with?( "00" ) or value.start_with? ( "+" )

      # do only format international numbers, since for national numbers, the country
      # is unknown and each country formats its national area codes differently.
      if format == :international
        value = Phony.normalize( value ) # remove spaces etc.
        value = value[ 2..-1 ] if value.start_with?( "00" ) # because Phony can't handle leading 00
        value = value[ 1..-1 ] if value.start_with?( "+" ) # because Phony can't handle leading +
        value = Phony.formatted( value, :format => format, :spaces => ' ' )
      end
      
      return value
    end

    private
    def auto_format_value
      self.value = Phone.format_phone_number( self.value )
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
your_platform-1.0.1 app/models/profile_field_types/phone.rb
your_platform-1.0.0 app/models/profile_field_types/phone.rb
your_platform-0.0.2 app/models/profile_field_types/phone.rb