Sha256: 6511e95afdb864b7e29e99935b0d309f165cedf6a9d8467a059e9ba91f1991aa

Contents?: true

Size: 1.93 KB

Versions: 25

Compression:

Stored size: 1.93 KB

Contents

module Inputs
  module EffectiveDatePicker
    class Input < Effective::FormInput
      delegate :content_tag, :text_field_tag, :to => :@template

      def default_input_js
        {format: 'YYYY-MM-DD', showTodayButton: true, showClear: true}
      end

      def default_input_html
        {class: 'effective_date_picker date'}
      end

      def to_html
        if options[:input_group] == false
          return text_field_tag(field_name, value, tag_options)
        end

        content_tag(:div, class: 'input-group') do
          content_tag(:span, class: 'input-group-addon') do
            content_tag(:i, '', class: 'glyphicon glyphicon-calendar').html_safe
          end +
          text_field_tag(field_name, value, tag_options)
        end
      end

      def value
        val = super
        val.kind_of?(Time) ? val.to_date : val
      end

      def js_options
        opts = super
        return opts unless opts[:disabledDates]

        opts[:disabledDates] = Array(opts[: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

        opts
      end

      def html_options
        super.tap do |html_options|
          if js_options[:format] == default_input_js[:format] # Unless someone changed from the default
            html_options[:pattern] = '\d{4}(-\d{2})?(-\d{2})?' # Match default pattern defined above
          end

          if options[:date_linked] == false
            html_options[:class] << 'not-date-linked'
          end

        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
effective_form_inputs-1.3.0 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.9 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.8 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.7 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.6 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.5 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.4 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.3 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.2 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.1 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.2.0 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.15 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.14 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.13 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.12 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.11 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.10 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.9 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.8 app/models/inputs/effective_date_picker/input.rb
effective_form_inputs-1.1.7 app/models/inputs/effective_date_picker/input.rb