active_attr
active_attr copied to clipboard
TimeWithZoneTypecaster
Time zone support is opt-in with ActiveRecord using time_zone_aware_attributes. TimeTypecaster will skip time zone conversion, TimeWithZoneTypecaster will not. Conversion in ActiveRecord uses the current Time.zone.
Initial assessment was wrong. time_zone_aware_attributes is false by default in the class but enabled by default in the Railtie. The default_timezone option in ActiveRecord only accepts :utc and :local as the possible values.
Possible implementation:
class TimeWithZoneTypecaster
def call(value)
if value.respond_to? :in_time_zone
value.in_time_zone
elsif value.respond_to? :to_s
Time.zone.parse(value)
end
end
end