Skip to content

Trek::Phonable

Phone number validation, normalization and formatting, backed by Phonelib.

API

ruby
class Organization < ApplicationRecord
  include Trek::Phonable
  phonable_attributes :phone, :fax
end

For each declared attribute:

  • a validation (phone: { possible: true, allow_blank: true })
  • normalization to E.164 international format on save
  • formatted_{attr} — national format ("01 23 45 67 89")
  • {attr}_with_country_code — E.164 ("+33123456789")

Usage

ruby
org = Organization.create(phone: "0123456789")
org.phone                   # => "+33123456789"
org.formatted_phone         # => "01 23 45 67 89"
org.phone_with_country_code # => "+33123456789"

Released under the MIT License.