Skip to content

Instantly share code, notes, and snippets.

@jamescook
Created March 3, 2011 16:14
Show Gist options
  • Select an option

  • Save jamescook/853020 to your computer and use it in GitHub Desktop.

Select an option

Save jamescook/853020 to your computer and use it in GitHub Desktop.
class PhoneNumber < ActiveRecord::Base
belongs_to :phonable, :polymorphic => true
validates :number, :length => { :minimum => 10, :maximum => 12}
validates_length_of :area_code, :is => 3 # 205
validates_length_of :prefix, :is => 3 # 867
validates_length_of :line, :is => 4 # 5309
def number=(val)
val.gsub! /[^\d]/, '' # Prune dashes, parenthesis, etc
self.area_code, self.prefix, self.line = val.scan( /(\d{3})(\d{3})(\d{4})/ ).flatten
write_attribute(:number, val)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment