Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_support/date_and_time_extensions.rb

Overview

Extend standard Date class with our custom to_s overrides

Instance Method Summary collapse

Instance Method Details

#orig_to_datetimeObject

ISO date format



12
# File 'lib/ndr_support/date_and_time_extensions.rb', line 12

alias orig_to_datetime to_datetime

#orig_to_sObject



22
# File 'lib/ndr_support/date_and_time_extensions.rb', line 22

alias orig_to_s to_s

#to_datetimeObject



14
15
16
17
18
19
20
# File 'lib/ndr_support/date_and_time_extensions.rb', line 14

def to_datetime
  # Default timezone for Date is GMT, not local timezone
  default_timezone = ActiveRecord.default_timezone
  return in_time_zone.to_datetime if default_timezone == :local

  orig_to_datetime
end

#to_isoObject

to_iso output must be SQL safe for security reasons



8
9
10
# File 'lib/ndr_support/date_and_time_extensions.rb', line 8

def to_iso
  strftime('%Y-%m-%d')
end

#to_s(format = :default) ⇒ Object

Rails 7 stops overriding to_s (without a format specification) (for performance on Ruby 3.1) cf. activesupport-7.0.4/lib/active_support/core_ext/date/deprecated_conversions.rb We keep overriding this for compatibility



27
28
29
30
31
32
33
# File 'lib/ndr_support/date_and_time_extensions.rb', line 27

def to_s(format = :default)
  if format == :default
    DATE_FORMATS.key?(:default) ? to_fs(:default) : orig_to_s
  else
    orig_to_s(format)
  end
end