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
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/greenwich/conversion.rb', line 6
def time_with_time_zone(utc_time_field, options = {})
time_field = utc_time_field.to_s.gsub /_utc$/, ''
date_field = time_field.gsub(/_at\Z/, '_on')
time_zone_field = options[:time_zone] || "#{time_field}_time_zone"
class_eval do
columns_hash[time_field] = ActiveRecord::ConnectionAdapters::Column.new(time_field, nil, "datetime")
end
define_method "#{time_field}_utc=" do |value|
greenwich_time_fields_converted["#{time_field}_utc"] = true
write_attribute("#{time_field}_utc", value)
end
define_method time_field do
time_zone = Greenwich::Utilities.get_time_zone(self, time_zone_field)
time = send(utc_time_field)
time = time.in_time_zone(time_zone) if time && time_zone
time
end
define_method "#{time_field}=" do |value|
time_zone = Greenwich::Utilities.get_time_zone(self, time_zone_field)
time = Greenwich::Utilities.coerce_to_time_without_zone(value)
time = ActiveSupport::TimeWithZone.new(nil, time_zone, time) if time && time_zone
time = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone.new('UTC'), time) if time && !time_zone
greenwich_time_fields_converted["#{time_field}_utc"] = true unless time_zone.nil?
write_attribute(utc_time_field, time)
end
define_method date_field do
return nil unless send(:"#{time_field}").respond_to? :to_date
send(:"#{time_field}").to_date
end
time_zone time_zone_field.to_sym, :for => utc_time_field.to_sym
end
|