Class: Forma::DateField

Inherits:
SimpleField show all
Defined in:
lib/forma/field.rb

Overview

Date feild.

Instance Attribute Summary collapse

Attributes inherited from Field

#actions, #after, #autofocus, #before, #child_model_name, #height, #hint, #i18n, #icon, #inline_hint, #label, #model, #model_name, #name, #parent, #readonly, #required, #tag, #turbolink, #url, #value, #width

Instance Method Summary collapse

Methods inherited from SimpleField

#errors, #has_errors?, #value

Methods inherited from Field

#action, #id, #localization_key, #localized_hint, #localized_label, #name_as_chain, #parameter_name, #to_html

Methods included from Html

attr, el

Methods included from Utils

extract_value, number_format, #simple_value, singular_name

Constructor Details

#initialize(h = {}) ⇒ DateField

Returns a new instance of DateField.



333
334
335
336
337
338
339
340
# File 'lib/forma/field.rb', line 333

def initialize(h = {})
  h = h.symbolize_keys
  @formatter = h[:formatter]
  @@date_counter ||= 0
  @@date_counter += 1
  @date_counter = @@date_counter
  super(h)
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



331
332
333
# File 'lib/forma/field.rb', line 331

def formatter
  @formatter
end

Instance Method Details

#edit_element(val) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/forma/field.rb', line 347

def edit_element(val)
  input_id = "ff-date-#{@date_counter}"
  val = Date.strptime(val) if (val.present? and val.is_a?(String)) rescue nil
  el('div', children: [
    el('input', attrs: {
      id: input_id,
      name: parameter_name,
      value: val.to_s,
      type: 'hidden'
    }),
    el('input', attrs: {
      class: 'ff-date',
      type: 'text',
      value: (val.strftime('%d-%b-%Y') if val.present?),
      autofocus: @autofocus,
      style: { width: "#{width || 100}px" },
      'data-altfield' => input_id,
    })
  ])
end

#view_element(val) ⇒ Object



342
343
344
345
# File 'lib/forma/field.rb', line 342

def view_element(val)
			val = val.respond_to?(:localtime) ? val.localtime : val
  el('span', text: val.strftime(formatter || Forma.config.date.formatter))
end