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, #html_default_value, #initialize, #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?, included, #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.



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
  parse_date_string(date_string)
end

.parse_date_string(date_string) ⇒ Object

Parse normalized date strings using time zone



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

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

Instance Method Details

#formatted_date_valueObject



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

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

#formatted_time_valueObject



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

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

#js_date_formatObject

Ruby to javascript formatting options translator



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

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



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

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



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

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

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



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

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



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

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

#meridian_indicator?Boolean

Returns:



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

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

#parse_input(params) ⇒ Object



134
135
136
# File 'lib/rails_admin/config/fields/types/datetime.rb', line 134

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