Sha256: 62a255e8c2abfc44b9f9457a3f286bc81f471b6202507dfd0677df748c4399ee

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

module Effective
  module FormInputs
    class DatetimeField < Effective::FormInput

      def input_html_options
        {
          class: ['form-control', 'effective_date_time_picker', 'effective_datetime', ('not-date-linked' if not_date_linked?)].compact.join(' '),
          pattern: '\d{4}(-\d{2})?-(\d{2})?( \d+)?(:\d{2})?'
        }
      end

      def input_js_options
        { format: 'YYYY-MM-DD HH:mm', sideBySide: true, showTodayButton: true, showClear: true, useCurrent: 'hour', disabledDates: disabled_dates.presence }.compact
      end

      def input_group_options
        { input_group: { class: 'input-group effective_date_time_picker_input_group' }, prepend: content_tag(:span, icon('calendar'), class: 'input-group-text') }
      end

      def build_input(&block)
        @builder.super_text_field(name, options[:input].merge(value: datetime_to_s))
      end

      def datetime_to_s
        value&.strftime('%F %H:%M')
      end

      private

      def disabled_dates
        return @disabled_dates unless @disabled_dates.nil?

        @disabled_dates ||= (
          Array(options.delete(:disabledDates)).map do |obj|
            if obj.respond_to?(:strftime)
              obj.strftime('%F')
            elsif obj.kind_of?(Range) && obj.first.respond_to?(:strftime)
              [obj.first].tap do |dates|
                dates << (dates.last + 1.day) until (dates.last + 1.day) > obj.last
              end
            elsif obj.kind_of?(String)
              obj
            else
              raise 'unexpected disabledDates data. Expected a DateTime, Range of DateTimes or String'
            end
          end.flatten.compact
        )
      end

      def not_date_linked?
        return @not_date_linked unless @not_date_linked.nil?
        @not_date_linked = (options.delete(:date_linked) == false)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
effective_bootstrap-0.1.7 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.6 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.5 app/models/effective/form_inputs/datetime_field.rb