16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/backframe/activerecord/acts_as_phone.rb', line 16
def acts_as_phone(entity)
class_eval "\n after_initialize :uncast_phone_\#{entity}, :if => Proc.new { |c| !c.new_record? && c.\#{entity}.present? }\n before_save :cast_phone_\#{entity}, :if => Proc.new { |c| c.\#{entity}.present? }\n after_save :uncast_phone_\#{entity}, :if => Proc.new { |c| c.\#{entity}.present? }\n\n validate :validates_phone_\#{entity}, :if => Proc.new { |c| c.\#{entity}.present? }\n\n private\n\n def uncast_phone_\#{entity}\n self.\#{entity} = self.\#{entity}[0]+'-'+self.\#{entity}[1,3]+'-'+self.\#{entity}[4,3]+'-'+self.\#{entity}[7,4]\n end\n\n def cast_phone_\#{entity}\n self.\#{entity} = \#{entity}_to_international(self.\#{entity})\n end\n\n def validates_phone_\#{entity}\n testvalue = \#{entity}_to_international(self.\#{entity})\n if !Phony.plausible?(testvalue)\n self.errors.add(:\#{entity}, 'invalid \#{entity} number')\n end\n end\n\n def \#{entity}_to_international(value)\n newvalue = value.gsub('(', '').gsub(')', '').gsub('.', '').gsub('-', '').gsub(' ', '')\n newvalue = (newvalue.length == 10) ? '1'+value : value\n Phony.normalize(newvalue)\n end\n\n EOV\n\nend\n"
|