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
- UNSUPPORTED_FORMAT_OPTIONS =
/%[cUWwxXZ]/
- DATE_FORMAT_CONVERSION =
{ /%a/ => 'D', /%A/ => 'DD', /%b/ => 'M', /%B/ => 'MM', /%d/ => 'dd', /%e|%-d/ => 'd', /%j/ => 'oo', /%m/ => 'mm', /%-m|%-m/ => 'm', /%y/ => 'y', /%Y/ => 'yy', /%H/ => 'HH', # options ampm => false /%I/ => 'hh', # options ampm => true /%M/ => 'mm', /%p/ => 'tt', /%S/ => 'ss', /%z/ => 'z', UNSUPPORTED_FORMAT_OPTIONS => '' }.freeze
Class Method Summary collapse
- .date_options(locale) ⇒ Object
- .date_options_for_locales ⇒ Object
- .datetime_options(locale) ⇒ Object
- .datetime_options_for_locales ⇒ Object
- .format_to_datetime_picker(rails_time_format) ⇒ Object
- .split_datetime_format(datetime_format) ⇒ Object
- .to_datepicker_format(rails_format) ⇒ Object
Class Method Details
.date_options(locale) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 33 def self.(locale) = { :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 } = I18n.translate! :date_picker_options, :scope => :active_scaffold, :locale => locale, :default => '' .merge!() if .is_a? Hash Rails.logger.warn "ActiveScaffold: Missing date picker localization for your locale: #{locale}" if .blank? js_format = to_datepicker_format(I18n.translate!('date.formats.default', :locale => locale, :default => '')) [:dateFormat] = js_format if js_format.present? rescue StandardError raise if locale == I18n.locale end |
.date_options_for_locales ⇒ Object
26 27 28 29 30 31 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 26 def self. I18n.available_locales.collect do |locale| = (locale) "$.datepicker.regional['#{locale}'] = #{.to_json};" if end.compact.join('') end |
.datetime_options(locale) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 66 def self.(locale) rails_time_format = I18n.translate! 'time.formats.picker', :locale => locale, :default => '%a, %d %b %Y %H:%M:%S' = { :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) } = I18n.translate! :datetime_picker_options, :scope => :active_scaffold, :locale => locale, :default => '' .merge!() if .is_a? Hash Rails.logger.warn "ActiveScaffold: Missing datetime picker localization for your locale: #{locale}" if .blank? .merge! format_to_datetime_picker(rails_time_format) rescue StandardError raise if locale == I18n.locale end |
.datetime_options_for_locales ⇒ Object
59 60 61 62 63 64 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 59 def self. I18n.available_locales.collect do |locale| = (locale) "$.timepicker.regional['#{locale}'] = #{.to_json};" if end.compact.join('') end |
.format_to_datetime_picker(rails_time_format) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 103 def self.format_to_datetime_picker(rails_time_format) date_format, time_format = split_datetime_format(to_datepicker_format(rails_time_format)) = {} [:dateFormat] = date_format unless date_format.nil? unless time_format.nil? [:timeFormat] = time_format [:ampm] = true if rails_time_format.include?('%I') end end |
.split_datetime_format(datetime_format) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 114 def self.split_datetime_format(datetime_format) date_format = datetime_format time_format = nil time_start_indicators = %w[HH 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).strip time_format = datetime_format.from(pos_time_format).strip end end [date_format, time_format] end |
.to_datepicker_format(rails_format) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_scaffold/bridges/date_picker/helper.rb', line 85 def self.to_datepicker_format(rails_format) return nil if rails_format.nil? if rails_format.match?(UNSUPPORTED_FORMAT_OPTIONS) = UNSUPPORTED_FORMAT_OPTIONS.to_s.scan(/\[(.*)\]/).dig(0, 0)&.each_char&.map { |c| "%#{c}" } Rails.logger.warn( "AS DatePicker::Helper: rails date format #{rails_format} includes options "\ "which can't be converted to jquery datepicker format. "\ "Options #{.join(', ')} are not supported by datepicker and will be removed" ) end js_format = rails_format.dup js_format.gsub!(/([ ]|^)([^% ]\S*)/, " '\\2'") DATE_FORMAT_CONVERSION.each do |key, value| js_format.gsub!(key, value) end js_format end |