6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/russian_phone/active_record.rb', line 6
def russian_phone(field, *args)
name = field.to_s
options = args. || {}
instance_variable_set "@#{name}_phone_options", options
if options[:validate]
validates_with(RussianPhone::FormatValidator, fields: [name])
end
if options[:required]
validates_with(RussianPhone::PresenceValidator, fields: [name])
end
define_method name do
options = self.class.instance_variable_get("@#{name}_phone_options")
RussianPhone::Number.new(read_attribute(name), options)
end
define_method "#{name}=" do |value|
instance_variable_set("@#{name}_phone_before_type_cast", value)
options = self.class.instance_variable_get("@#{name}_phone_options")
self[name] = RussianPhone::Number.new(value, options).mongoize
end
define_method "#{name}_phone_before_type_cast" do
instance_variable_get "@#{name}_phone_before_type_cast"
end
after_save do
instance_variable_set "@#{name}_phone_before_type_cast", nil
end
end
|