Module: ActiveScaffold::Bridges::DatePicker::Helper

Defined in:
lib/active_scaffold/bridges/date_picker/helper.rb

Defined Under Namespace

Modules: DatepickerColumnHelpers, FormColumnHelpers, SearchColumnHelpers

Constant Summary collapse

DATE_FORMAT_CONVERSION =
{
  /%a/ => 'D',
  /%A/ => 'DD',
  /%b/ => 'M',
  /%B/ => 'MM',
  /%d/ => 'dd',
  /%e/ => 'd',
  /%j/ => 'oo',
  /%m/ => 'mm',
  /%y/ => 'y',
  /%Y/ => 'yy',
  /%H/ => 'hh', # options ampm => false
  /%I/ => 'hh', # options ampm => true
  /%M/ => 'mm',
  /%p/ => 'tt',
  /%S/ => 'ss',
  /%[cUWwxXZz]/ => ''
}

Class Method Summary collapse

Class Method Details

.date_options(locale) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 34

def self.date_options(locale)
  begin
    date_picker_options = { :closeText => as_(:close),
      :prevText => as_(:previous),
      :nextText => as_(:next),
      :currentText => as_(:today),
      :monthNames => I18n.translate!('date.month_names', :locale => locale)[1..-1],
      :monthNamesShort => I18n.translate!('date.abbr_month_names', :locale => locale)[1..-1],
      :dayNames => I18n.translate!('date.day_names', :locale => locale),
      :dayNamesShort => I18n.translate!('date.abbr_day_names', :locale => locale),
      :dayNamesMin => I18n.translate!('date.abbr_day_names', :locale => locale),
      :changeYear => true,
      :changeMonth => true,
    }

    as_date_picker_options = I18n.translate! :date_picker_options, :scope => :active_scaffold, :locale => locale, :default => ''
    date_picker_options.merge!(as_date_picker_options) if as_date_picker_options.is_a? Hash
    Rails.logger.warn "ActiveScaffold: Missing date picker localization for your locale: #{locale}" if as_date_picker_options.blank?

    js_format = self.to_datepicker_format(I18n.translate!('date.formats.default', :locale => locale, :default => ''))
    date_picker_options[:dateFormat] = js_format unless js_format.blank?
    date_picker_options
  rescue
    raise if locale == I18n.locale
  end
end

.date_options_for_localesObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 23

def self.date_options_for_locales
  I18n.available_locales.collect do |locale|
    locale_date_options = date_options(locale)
    if locale_date_options
      "$.datepicker.regional['#{locale}'] = #{locale_date_options.to_json};"
    else
      nil
    end
  end.compact.join('')
end

.datetime_options(locale) ⇒ Object



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 'lib/active_scaffold/bridges/date_picker/helper.rb', line 72

def self.datetime_options(locale)
  begin
    rails_time_format = I18n.translate! 'time.formats.picker', :locale => locale
    datetime_picker_options = {:ampm => false,
      :hourText => I18n.translate!('datetime.prompts.hour', :locale => locale),
      :minuteText => I18n.translate!('datetime.prompts.minute', :locale => locale),
      :secondText => I18n.translate!('datetime.prompts.second', :locale => locale)
    }

    as_datetime_picker_options = I18n.translate! :datetime_picker_options, :scope => :active_scaffold, :locale => locale, :default => ''
    datetime_picker_options.merge!(as_datetime_picker_options) if as_datetime_picker_options.is_a? Hash
    Rails.logger.warn "ActiveScaffold: Missing datetime picker localization for your locale: #{locale}" if as_datetime_picker_options.blank?

    date_format, time_format = self.split_datetime_format(self.to_datepicker_format(rails_time_format))
    datetime_picker_options[:dateFormat] = date_format unless date_format.nil?
    unless time_format.nil?
      datetime_picker_options[:timeFormat] = time_format
      datetime_picker_options[:ampm] = true if rails_time_format.include?('%I')
    end
    datetime_picker_options
  rescue
    raise if locale == I18n.locale
  end
end

.datetime_options_for_localesObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 61

def self.datetime_options_for_locales
  I18n.available_locales.collect do |locale|
    locale_datetime_options = datetime_options(locale)
    if locale_datetime_options
      "$.timepicker.regional['#{locale}'] = #{locale_datetime_options.to_json};"
    else
      nil
    end
  end.compact.join('')
end

.split_datetime_format(datetime_format) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 110

def self.split_datetime_format(datetime_format)
  date_format = datetime_format
  time_format = nil
  time_start_indicators = %w{hh mm tt ss}
  unless datetime_format.nil?
    start_indicator = time_start_indicators.detect {|indicator| datetime_format.include?(indicator)}
    unless start_indicator.nil?
      pos_time_format = datetime_format.index(start_indicator)
      date_format = datetime_format.to(pos_time_format - 1)
      time_format = datetime_format.from(pos_time_format)
    end
  end
  return date_format, time_format
end

.to_datepicker_format(rails_format) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 97

def self.to_datepicker_format(rails_format)
  return nil if rails_format.nil?
  if rails_format =~ /%[cUWwxXZz]/
    Rails.logger.warn("AS DatePicker::Helper: rails date format #{rails_format} includes options which can't be converted to jquery datepicker format. Options %c, %U, %W, %w, %x %X, %z, %Z are not supported by datepicker and will be removed")
    nil
  end
  js_format = rails_format.dup
  DATE_FORMAT_CONVERSION.each do |key, value|
    js_format.gsub!(key, value)
  end
  js_format
end