Class: DrgcmsFormFields::DatePicker

Inherits:
DrgcmsField show all
Defined in:
app/models/drgcms_form_fields/date_picker.rb

Overview

Implementation of date_picker DRG CMS form field with help of jQuery DateTimePicker plugin.

Since javascript date(time) format differs from ruby date(time) format localization must be provided in order for date_picker object works as expected. For example:

en:
 datetimepicker: 
  formats:
  date: 'Y/m/d'
  datetime: 'Y/m/d H:i'

sl:
 datetimepicker: 
  formats:
   date: 'd.m.Y'
   datetime: 'd.m.Y H:i'

Form options:

  • type: date_picker (required)

  • name: Field name (required)

  • options: options which apply to date_picker field. All options can be found here xdsoft.net/jqplugins/datetimepicker/ .

Options can be defined in single line like:

  • options: ‘inline: true,lang: “sl”’ or

  • options:

    • inline: true

    • lang: ‘“sl”’

  • html: html options which apply to date_picker field (optional)

Form example:

10:
  name: created
  type: date_picker
  options: 'inline: true,lang: "sl"'

Instance Attribute Summary

Attributes inherited from DrgcmsField

#css, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

#__css_code, #hash_to_options, #html, #initialize, #record_text_for, #ro_standard, #set_css_code, #set_default_value, #set_initial_value, #set_style, #t

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Class Method Details

.get_data(params, name) ⇒ Object

DatePicker get_data method.



100
101
102
103
# File 'app/models/drgcms_form_fields/date_picker.rb', line 100

def self.get_data(params, name)
  t = params['record'][name] ? params['record'][name].to_datetime : nil
  t ? Time.zone.local(t.year, t.month, t.day) : nil
end

Instance Method Details

#renderObject

Render date_picker field html code



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/drgcms_form_fields/date_picker.rb', line 67

def render
  value = @record.try(@yaml['name']) ? I18n.localize(@record[@yaml['name']].to_date) : nil
  #return ro_standard( @parent.dc_format_value(value)) if @readonly

  @yaml['options'] ||= {}
  set_initial_value
  @yaml['html']['size'] ||= @yaml['size'] || 10
  @yaml['html']['value'] ||= value
  @yaml['html']['autocomplete'] ||= 'off'
  @yaml['html']['class'] = @yaml['html']['class'].to_s + ' date-picker'

  @yaml['options']['lang']   ||= "'#{I18n.locale}'"
  @yaml['options']['format'] ||= "'#{t('datetimepicker.formats.date')}'"
  @yaml['options']['timepicker'] = false
  @yaml['options']['scrollMonth'] ||= false
  @yaml['options']['scrollInput'] ||= false

  record = record_text_for(@yaml['name'])
  @html << @parent.text_field(record, @yaml['name'], @yaml['html'])
  @js << %Q[
$(document).ready(function() {
  $("##{record}_#{@yaml['name']}").datetimepicker( {
    #{hash_to_options(@yaml['options'])}
  });
});
] unless @readonly
  
  self
end