Sha256: 62b256925bd3dc797dc62bb643659035026125873b7abc4582850df70fe75f30

Contents?: true

Size: 1.28 KB

Versions: 80

Compression:

Stored size: 1.28 KB

Contents

module Avo
  module Fields
    class DateTimeField < DateField
      attr_reader :format
      attr_reader :picker_format
      attr_reader :time_24hr
      attr_reader :relative

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

        add_boolean_prop args, :time_24hr
        add_string_prop args, :picker_format, "Y-m-d H:i:S"
        add_string_prop args, :format, "yyyy-LL-dd TT"
        add_string_prop args, :timezone
        add_boolean_prop args, :relative, true
      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

      def timezone
        Avo::ExecutionContext.new(target: @timezone, record: resource.record, resource: resource, view: view).handle
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
avo-3.0.1.beta21 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta17 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta22 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta18 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta19 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta24 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta15 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta16 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta14 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta12 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta13 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta10 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta11 lib/avo/fields/date_time_field.rb
avo-3.0.0.pre19 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta8 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta9 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta6 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta7 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta5 lib/avo/fields/date_time_field.rb
avo-3.0.1.beta2 lib/avo/fields/date_time_field.rb