Sha256: 639cdf8a25890d1338e0dfc31b8d98bd14c3ee9560264e1e7a90a3c13c5eb7f5

Contents?: true

Size: 887 Bytes

Versions: 6

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Attributes
    # Custom Virtus value to parse a String representing a Time using
    # the app TimeZone.
    class TimeWithZone < Virtus::Attribute
      def coerce(value)
        return value unless value.is_a?(String)

        Time.zone.strptime(value, I18n.t("time.formats.decidim_short"))
      rescue ArgumentError
        coerce_fallback(value)
      end

      def type
        Axiom::Types::Time
      end

      private

      def coerce_fallback(value)
        fallback = coercer.coercers[Time].public_send(type.coercion_method, value)
        return Time.zone.strptime(fallback.split(".").first, "%FT%R:%S") if fallback.is_a?(String)
        return nil unless fallback.is_a?(Time)

        ActiveSupport::TimeWithZone.new(fallback, Time.zone)
      rescue ArgumentError, TypeError
        nil
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/attributes/time_with_zone.rb
decidim-core-0.26.9 lib/decidim/attributes/time_with_zone.rb
decidim-core-0.26.8 lib/decidim/attributes/time_with_zone.rb
decidim-core-0.26.7 lib/decidim/attributes/time_with_zone.rb
decidim-core-0.26.5 lib/decidim/attributes/time_with_zone.rb
decidim-core-0.26.4 lib/decidim/attributes/time_with_zone.rb