Sha256: 65ff54ff138ebc96a1b0bd90ed6ebb1289ca3a78bb6659686c140a934f4688ed

Contents?: true

Size: 964 Bytes

Versions: 15

Compression:

Stored size: 964 Bytes

Contents

module Avo
  module Fields
    class TimeField < DateTimeField
      attr_reader :format
      attr_reader :picker_format

      def initialize(id, **args, &block)
        super(id, **args, &block)

        add_string_prop args, :picker_format, "H:i:S"
        add_string_prop args, :format, "TT"
      end

      def formatted_value
        return nil if value.nil?

        value.utc.to_time.iso8601
      end

      def edit_formatted_value
        return nil if value.nil?

        value.utc.iso8601
      end

      def fill_field(model, key, value, params)
        if value.in?(["", nil])
          model[id] = value

          return model
        end

        return model if value.blank?

        model[id] = utc_time(value)

        model
      end

      def utc_time(value)
        if timezone.present?
          ActiveSupport::TimeZone.new(timezone).local_to_utc(Time.parse(value))
        else
          value
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
avo-2.22.0 lib/avo/fields/time_field.rb
avo-2.21.3.pre.pr1489 lib/avo/fields/time_field.rb
avo-2.21.2.pre.pr1486 lib/avo/fields/time_field.rb
avo-2.21.1.pre.pr1484 lib/avo/fields/time_field.rb
avo-2.21.0 lib/avo/fields/time_field.rb
avo-2.21.1.pre.pr1476 lib/avo/fields/time_field.rb
avo-2.21.1.pre.issue1450 lib/avo/fields/time_field.rb
avo-2.21.1.pre.issue1444 lib/avo/fields/time_field.rb
avo-2.20.0 lib/avo/fields/time_field.rb
avo-2.19.0 lib/avo/fields/time_field.rb
avo-2.18.1 lib/avo/fields/time_field.rb
avo-2.18.1.pre.1.eagerloaddirs lib/avo/fields/time_field.rb
avo-2.18.0 lib/avo/fields/time_field.rb
avo-2.17.1.pre.5.stackedlayout lib/avo/fields/time_field.rb
avo-2.17.1.pre.4.issue.1342 lib/avo/fields/time_field.rb