Sha256: 88bc65e1164d4ac56cce8cf933e88a7d210dc8583d5394135d03d7c2c0921f5f

Contents?: true

Size: 1.85 KB

Versions: 7

Compression:

Stored size: 1.85 KB

Contents

module AbAdmin
  module Views
    module Inputs
      class DateTimeInput < ::SimpleForm::Inputs::Base
        def input
          if options[:no_js]
            return @builder.send(:"#{input_type}_select", attribute_name, input_options, input_html_options)
          end

          input_html_options[:value] ||= formated_value

          html = [@builder.hidden_field(attribute_name, input_html_options)]

          base_dom_id = @builder.object_name.gsub(/\[/, '_').gsub(/\]/, '')
          data_target = "#{base_dom_id}_#{attribute_name}"

          if [:date, :datetime].include? input_type
            attr = "#{data_target}_date"
            html << template.text_field_tag(attr, object.send(attribute_name).try(:strftime, '%d.%m.%Y'), id: attr,
                                            class: 'datepicker input-small', data: {target: data_target})
          end

          if [:time, :datetime].include? input_type
            attr = "#{data_target}_time"
            html << template.text_field(attr, object.send(attribute_name).try(:strftime, '%H:%M'), id: attr,
                                        class: 'timepicker input-small', data: {target: data_target})
          end

          html.join.html_safe
        end

        private

        def formated_value
          object.send(attribute_name).try(:strftime, value_format)
        end

        def value_format
          case input_type
            when :date then
              '%d.%m.%Y'
            when :datetime then
              '%d.%m.%Y %H:%M'
            when :time then
              '%H:%M'
          end
        end

        def has_required?
          false
        end

        def label_target
          case input_type
            when :date, :datetime
              "#{attribute_name}_1i"
            when :time
              "#{attribute_name}_4i"
          end
        end

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ab_admin-0.3.6 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.5 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.4 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.3 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.2 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.1 lib/ab_admin/views/inputs/date_time_input.rb
ab_admin-0.3.0 lib/ab_admin/views/inputs/date_time_input.rb