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

#defined, #name, #order, #properties

Attributes inherited from Base

#abstract_model, #bindings, #parent, #root

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?, #column_css_class, #dom_id, #dom_name, #errors, #has_errors?, inherited, #initialize, #method_name, #optional, #optional=, #optional?, #type, #value, #virtual?

Methods included from Hideable

#hidden?, #hide, included, #show

Methods inherited from Base

#has_option?, #initialize, register_class_option, #register_deprecated_instance_option, register_deprecated_instance_option, #register_instance_option, register_instance_option, #with

Constructor Details

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

Class Attribute Details

.formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

.i18n_scopeObject (readonly)

Returns the value of attribute i18n_scope.



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

def i18n_scope
  @i18n_scope
end

.js_plugin_optionsObject (readonly)

Returns the value of attribute js_plugin_options.



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

def js_plugin_options
  @js_plugin_options
end

Class Method Details

.normalize(date_string, format) ⇒ Object



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

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
  ::Time.zone.parse(date_string, format)
end

Instance Method Details

#formatted_date_valueObject



49
50
51
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 49

def formatted_date_value
  value.nil? ? "" : I18n.l(value, :format => localized_date_format).strip
end

#formatted_time_valueObject



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

def formatted_time_value
  value.nil? ? "" : I18n.l(value, :format => localized_time_format)
end

#js_date_formatObject

Ruby to javascript formatting options translator



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

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



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

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? ? ["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 = options.merge self.class.js_plugin_options

  ActiveSupport::JSON.encode(options).html_safe
end

#localized_date_formatObject



117
118
119
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 117

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

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



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

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



121
122
123
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 121

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

#meridian_indicator?Boolean

Returns:



125
126
127
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 125

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

#parse_input(params) ⇒ Object



129
130
131
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 129

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