add ability to set date field format
This commit is contained in:
@@ -228,7 +228,10 @@ module Submissions
|
||||
elsif field['type'] == 'checkbox'
|
||||
composer.formatted_text_box([{ text: value.to_s.titleize }], padding: [0, 0, 10, 0])
|
||||
else
|
||||
value = I18n.l(Date.parse(value), format: :long, locale: account.locale) if field['type'] == 'date'
|
||||
if field['type'] == 'date'
|
||||
value = TimeUtils.format_date_string(value, field.dig('preferences', 'format'), account.locale)
|
||||
end
|
||||
|
||||
value = value.join(', ') if value.is_a?(Array)
|
||||
|
||||
composer.formatted_text_box([{ text: value.to_s.presence || 'n/a' }], padding: [0, 0, 10, 0])
|
||||
|
||||
@@ -164,7 +164,9 @@ module Submissions
|
||||
height - (area['y'] * height))
|
||||
end
|
||||
else
|
||||
value = I18n.l(Date.parse(value), format: :default, locale: account.locale) if field['type'] == 'date'
|
||||
if field['type'] == 'date'
|
||||
value = TimeUtils.format_date_string(value, field.dig('preferences', 'format'), account.locale)
|
||||
end
|
||||
|
||||
text = HexaPDF::Layout::TextFragment.create(Array.wrap(value).join(', '), font: pdf.fonts.add(FONT_NAME),
|
||||
font_size:)
|
||||
@@ -268,7 +270,7 @@ module Submissions
|
||||
Submissions::EnsureResultGenerated.call(latest_submitter) if latest_submitter
|
||||
|
||||
documents = latest_submitter&.documents&.preload(:blob).to_a.presence
|
||||
documents ||= submitter.submission.template.schema_documents.preload(:blob)
|
||||
documents ||= submitter.submission.template_schema_documents.preload(:blob)
|
||||
|
||||
documents.to_h do |attachment|
|
||||
pdf =
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module TimeUtils
|
||||
MONTH_FORMATS = {
|
||||
'M' => '%-m',
|
||||
'MM' => '%m',
|
||||
'MMM' => '%b',
|
||||
'MMMM' => '%B'
|
||||
}.freeze
|
||||
|
||||
DAY_FORMATS = {
|
||||
'D' => '%-d',
|
||||
'DD' => '%d'
|
||||
}.freeze
|
||||
|
||||
YEAR_FORMATS = {
|
||||
'YYYY' => '%Y',
|
||||
'YY' => '%y'
|
||||
}.freeze
|
||||
|
||||
DEFAULT_DATE_FORMAT_US = 'MM/DD/YYYY'
|
||||
DEFAULT_DATE_FORMAT = 'DD/MM/YYYY'
|
||||
|
||||
module_function
|
||||
|
||||
def timezone_abbr(timezone, time = Time.current)
|
||||
@@ -10,4 +30,20 @@ module TimeUtils
|
||||
|
||||
tz_info.abbreviation(time)
|
||||
end
|
||||
|
||||
def format_date_string(string, format, locale)
|
||||
date = Date.parse(string)
|
||||
|
||||
format ||= locale.to_s.ends_with?('US') ? DEFAULT_DATE_FORMAT_US : DEFAULT_DATE_FORMAT
|
||||
|
||||
i18n_format = format.sub(/D+/, DAY_FORMATS[format[/D+/]])
|
||||
.sub(/M+/, MONTH_FORMATS[format[/M+/]])
|
||||
.sub(/Y+/, YEAR_FORMATS[format[/Y+/]])
|
||||
|
||||
I18n.l(date, format: i18n_format, locale:)
|
||||
rescue Date::Error => e
|
||||
Rollbar.error(e) if defined?(Rollbar)
|
||||
|
||||
string
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user