active_attr icon indicating copy to clipboard operation
active_attr copied to clipboard

TimeWithZoneTypecaster

Open cgriego opened this issue 13 years ago • 2 comments

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.

cgriego avatar Mar 15 '12 03:03 cgriego

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.

cgriego avatar Mar 15 '12 03:03 cgriego

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

ryansch avatar Nov 17 '14 22:11 ryansch