Sha256: 89a6c2ca6567f720d4cb8653929a919d842ac8ae3ffeb324ec0fff7f5e62fe34

Contents?: true

Size: 1.07 KB

Versions: 31

Compression:

Stored size: 1.07 KB

Contents

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

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

        @partial_name = "date-time-field"
        @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)
        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

31 entries across 31 versions & 1 rubygems

Version Path
avo-1.4.0.pre.1 lib/avo/fields/date_time_field.rb
avo-1.3.5 lib/avo/fields/date_time_field.rb
avo-1.3.5.pre.1 lib/avo/fields/date_time_field.rb
avo-1.3.4 lib/avo/fields/date_time_field.rb
avo-1.3.3 lib/avo/fields/date_time_field.rb
avo-1.3.2 lib/avo/fields/date_time_field.rb
avo-1.3.1 lib/avo/fields/date_time_field.rb
avo-1.3.0 lib/avo/fields/date_time_field.rb
avo-1.2.11.pre.4 lib/avo/fields/date_time_field.rb
avo-1.2.11.pre.3 lib/avo/fields/date_time_field.rb
avo-1.2.11.pre.2 lib/avo/fields/date_time_field.rb
avo-1.2.11.pre.1 lib/avo/fields/date_time_field.rb
avo-1.2.10 lib/avo/fields/date_time_field.rb
avo-1.2.9 lib/avo/fields/date_time_field.rb
avo-1.2.8 lib/avo/fields/date_time_field.rb
avo-1.2.7 lib/avo/fields/date_time_field.rb
avo-1.2.6 lib/avo/fields/date_time_field.rb
avo-1.2.6.pre.1 lib/avo/fields/date_time_field.rb
avo-1.2.5 lib/avo/fields/date_time_field.rb
avo-1.2.4 lib/avo/fields/date_time_field.rb