Sha256: 9d602b8885f1d404afd381a2a10f26b2405829a9473c17d905adfff824a28958

Contents?: true

Size: 1.79 KB

Versions: 32

Compression:

Stored size: 1.79 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' }, 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

32 entries across 32 versions & 1 rubygems

Version Path
effective_bootstrap-0.1.4 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.3 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.2 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.1 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.1.0 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.29 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.28 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.27 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.26 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.25 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.24 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.23 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.22 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.21 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.20 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.19 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.18 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.17 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.16 app/models/effective/form_inputs/datetime_field.rb
effective_bootstrap-0.0.15 app/models/effective/form_inputs/datetime_field.rb