Sha256: 547c1e061047b584026fb8fce5b2a8dcc18200dfc0c0bc562a5cad0a2e125436

Contents?: true

Size: 1.03 KB

Versions: 43

Compression:

Stored size: 1.03 KB

Contents

module Avo
  module Fields
    class DateTimeField < DateField
      attr_reader :format
      attr_reader :time_24hr
      attr_reader :timezone

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

        @picker_format = args[:picker_format].present? ? args[:picker_format] : "Y-m-d H:i:S"
        @time_24hr = args[:time_24hr].present? ? args[:time_24hr] : false
        @timezone = args[:timezone].present? ? args[:timezone] : Rails.application.config.time_zone
      end

      def formatted_value
        return nil if value.nil?

        if @format.is_a?(Symbol)
          value.to_time.in_time_zone(timezone).to_s(@format)
        else
          value.to_time.in_time_zone(timezone).strftime(@format)
        end
      end

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

          return model
        end

        return model if value.blank?

        model[id] = value.to_time.in_time_zone(Rails.application.config.time_zone)

        model
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
avo-1.17.1 lib/avo/fields/date_time_field.rb
avo-1.17.0 lib/avo/fields/date_time_field.rb
avo-1.16.4 lib/avo/fields/date_time_field.rb
avo-1.16.3 lib/avo/fields/date_time_field.rb
avo-1.16.2 lib/avo/fields/date_time_field.rb
avo-1.16.1 lib/avo/fields/date_time_field.rb
avo-1.16.0 lib/avo/fields/date_time_field.rb
avo-1.15.0 lib/avo/fields/date_time_field.rb
avo-1.15.0.pre.1 lib/avo/fields/date_time_field.rb
avo-1.14.0 lib/avo/fields/date_time_field.rb
avo-1.13.3 lib/avo/fields/date_time_field.rb
avo-1.13.2 lib/avo/fields/date_time_field.rb
avo-1.13.1 lib/avo/fields/date_time_field.rb
avo-1.13.0 lib/avo/fields/date_time_field.rb
avo-1.12.4 lib/avo/fields/date_time_field.rb
avo-1.12.3 lib/avo/fields/date_time_field.rb
avo-1.12.2 lib/avo/fields/date_time_field.rb
avo-1.12.1 lib/avo/fields/date_time_field.rb
avo-1.12.0 lib/avo/fields/date_time_field.rb
avo-1.11.6 lib/avo/fields/date_time_field.rb