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
36
37
38
39
|
# File 'lib/mongoid-friendly-timestamps.rb', line 9
def friendly_timestamp(raw_name, *friendly_name)
args = friendly_name
friendly_name = args.shift
raw_options = {}
friendly_options = {}
if friendly_name.is_a? Hash
raw_options = friendly_name
friendly_name = args.shift
end
friendly_options = args.shift unless args.empty?
update_friendly_field_method_name = "update_friendly_timestamp_field_#{friendly_name}"
define_method update_friendly_field_method_name do
self[friendly_name] = Time.at(self[raw_name]).utc.strftime FRIENDLY_FORMAT
end
define_method "#{raw_name}=" do |value|
value = value.to_f unless value.is_a? Float
self[raw_name] = value
self.send update_friendly_field_method_name
end
self.class_eval do
field raw_name, raw_options.merge(:type => Float)
field friendly_name, friendly_options.merge(:type => String)
before_validation { self.send(update_friendly_field_method_name) }
end
end
|