Class: RailsAdmin::Config::Fields::Types::Datetime

Inherits:
Base
  • Object
show all
Extended by:
I18nSupport
Defined in:
lib/rails_admin/config/fields/types/datetime.rb

Direct Known Subclasses

Date, Time, Timestamp

Class Attribute Summary collapse

Attributes inherited from Base

#abstract_model, #defined, #name, #order, #parent, #properties, #root, #section

Attributes included from Proxyable

#bindings

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18nSupport

abbr_day_names, abbr_month_names, date_format, day_names, month_names

Methods inherited from Base

#association?, #editable?, #errors, #form_default_value, #form_value, #generic_field_help, #generic_help, #initialize, #inspect, #inverse_of, #method_name, #optional, #optional=, #optional?, #type, #type_css_class, #value, #virtual?

Methods included from Groupable

#group

Methods included from Hideable

#hidden?, #hide, included, #show

Methods included from Configurable

#has_option?, #register_deprecated_instance_option, #register_instance_option

Methods included from Proxyable

#with

Constructor Details

This class inherits a constructor from RailsAdmin::Config::Fields::Base

Class Attribute Details

.formatObject (readonly)

Returns the value of attribute format


19
20
21
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 19

def format
  @format
end

.i18n_scopeObject (readonly)

Returns the value of attribute i18n_scope


19
20
21
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 19

def i18n_scope
  @i18n_scope
end

.js_plugin_optionsObject (readonly)

Returns the value of attribute js_plugin_options


19
20
21
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 19

def js_plugin_options
  @js_plugin_options
end

Class Method Details

.normalize(date_string, format) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 21

def normalize(date_string, format)
  unless I18n.locale == 'en'
    format.to_s.scan(/%[AaBbp]/) do |match|
      case match
      when '%A'
        english = I18n.t('date.day_names', locale: :en)
        day_names.each_with_index { |d, i| date_string = date_string.gsub(/#{d}/, english[i]) }
      when '%a'
        english = I18n.t('date.abbr_day_names', locale: :en)
        abbr_day_names.each_with_index { |d, i| date_string = date_string.gsub(/#{d}/, english[i]) }
      when '%B'
        english = I18n.t('date.month_names', locale: :en)[1..-1]
        month_names.each_with_index { |m, i| date_string = date_string.gsub(/#{m}/, english[i]) }
      when '%b'
        english = I18n.t('date.abbr_month_names', locale: :en)[1..-1]
        abbr_month_names.each_with_index { |m, i| date_string = date_string.gsub(/#{m}/, english[i]) }
      when '%p'
        date_string = date_string.gsub(/#{I18n.t('date.time.am', default: "am")}/, 'am')
        date_string = date_string.gsub(/#{I18n.t('date.time.pm', default: "pm")}/, 'pm')
      end
    end
  end
  parse_date_string(date_string)
end

.parse_date_string(date_string) ⇒ Object

Parse normalized date strings using time zone


47
48
49
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 47

def parse_date_string(date_string)
  ::Time.zone.parse(date_string)
end

Instance Method Details

#formatted_date_valueObject


52
53
54
55
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 52

def formatted_date_value
  value = bindings[:object].new_record? && self.value.nil? ? default_value : self.value
  value.nil? ? '' : I18n.l(value, format: localized_date_format).strip
end

#formatted_time_valueObject


57
58
59
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 57

def formatted_time_value
  value.nil? ? '' : I18n.l(value, format: localized_time_format)
end

#js_date_formatObject

Ruby to javascript formatting options translator


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 62

def js_date_format
  # Ruby format options as a key and javascript format options
  # as a value
  translations = {
    '%a' => 'D',          # The abbreviated weekday name ("Sun")
    '%A' => 'DD',         # The  full  weekday  name ("Sunday")
    '%b' => 'M',          # The abbreviated month name ("Jan")
    '%B' => 'MM',         # The  full  month  name ("January")
    '%d' => 'dd',         # Day of the month (01..31)
    '%D' => 'mm/dd/y',    # American date format mm/dd/yy
    '%e' => 'd',          # Day of the month (1..31)
    '%F' => 'yy-mm-dd',   # ISO 8601 date format
    # "%H" => "??",         # Hour of the day, 24-hour clock (00..23)
    # "%I" => "??",         # Hour of the day, 12-hour clock (01..12)
    '%m' => 'mm',         # Month of the year (01..12)
    '%-m' => 'm',         # Month of the year (1..12)
    # "%M" => "??",         # Minute of the hour (00..59)
    # "%p" => "??",         # Meridian indicator ("AM" or "PM")
    # "%S" => "??",         # Second of the minute (00..60)
    '%Y' => 'yy',         # Year with century
    '%y' => 'y',          # Year without a century (00..99)
  }
  localized_date_format.gsub(/%\w/) { |match| translations[match] }
end

#js_plugin_optionsObject


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 87

def js_plugin_options
  options = {
    'datepicker' => {
      'dateFormat' => js_date_format,
      'dayNames' => self.class.day_names,
      'dayNamesShort' => self.class.abbr_day_names,
      'dayNamesMin' => self.class.abbr_day_names,
      'firstDay' => 1,
      'monthNames' => self.class.month_names,
      'monthNamesShort' => self.class.abbr_month_names,
      'value' => formatted_date_value,
    },
    'timepicker' => {
      'amPmText' => meridian_indicator? ? %w(Am Pm) : ['', ''],
      'hourText' => I18n.t('datetime.prompts.hour', default: I18n.t('datetime.prompts.hour', locale: :en)),
      'minuteText' => I18n.t('datetime.prompts.minute', default: I18n.t('datetime.prompts.minute', locale: :en)),
      'showPeriod' => meridian_indicator?,
      'value' => formatted_time_value,
    },
  }

  options.merge(self.class.js_plugin_options)
end

#localized_date_formatObject


119
120
121
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 119

def localized_date_format
  localized_format([:date, :formats])
end

#localized_format(scope = [:time, :formats]) ⇒ Object


111
112
113
114
115
116
117
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 111

def localized_format(scope = [:time, :formats])
  format = date_format.to_sym
  I18n.t(format, scope: scope, default: [
    I18n.t(format, scope: scope, locale: :en),
    I18n.t(self.class.format, scope: scope, locale: :en),
  ]).to_s
end

#localized_time_formatObject


123
124
125
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 123

def localized_time_format
  meridian_indicator? ? '%I:%M %p' : '%H:%M'
end

#meridian_indicator?Boolean


127
128
129
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 127

def meridian_indicator?
  strftime_format.include? '%p'
end

#parse_input(params) ⇒ Object


131
132
133
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 131

def parse_input(params)
  params[name] = self.class.normalize(params[name], "#{localized_date_format} #{localized_time_format}") if params[name].present?
end