0.0
No commit activity in last 3 years
No release in over 3 years
ActiveModel field serializer for handling phone numbers via Phony gem
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 1.9.0
>= 3.2.0
 Project Readme

PhonyAttribute Build Status

ActiveModel attribute serialization using phony. Also wraps the phony with a model.

Usage

Assuming you have a column named phone_number on the addresses table, you can easily add phone number handling with:

class Address < ActiveRecord::Base
  phone_attribute :phone_number
end

address = Address.new({
  phone_number: "2065551234"
})
address.phone_number.format(:us_standard)
=> (206) 555-1234

Validating

PhonyAttribute provides validation to ActiveModel using the Phony.plausible? helper. To use:

class Address < ActiveRecord::Base
  phone_attribute :phone_number
  validates :phone_number, phony_plausible: {
    allow_blocked: false, 
    minimum_length: 9,
    message: "is invalid"
  }
end

Model

PhonyAttribute::PhoneNumber can also be used as a standalone model. It wraps the nice methods Phony provides with a model that includes named formatting options.

Formatting

PhonyAttribute comes built with a few named formats. You can add your own by adding to the PhonyAttribute::PhoneNumber.named_formats hash. The value can be either an options hash (passed directly to Phony's format) or a callable Proc/lambda that yields the phone number object.

License

This project rocks and uses MIT-LICENSE.