Module: TimeZoneAware
- Extended by:
- ActiveSupport::Concern
- Included in:
- PushMessage
- Defined in:
- app/models/concerns/time_zone_aware.rb
Class Method Summary collapse
-
.localize(value, zone_name) ⇒ Object
NULL-safe: localizes
value(a UTC datetime) tozone_name, or returns it unshifted when there is no zone to localize to (nil value, blank/invalid zone name). -
.server_time_zone ⇒ Object
The deployment-wide IANA time zone identifier (ThecoreSettings ns: :main, key: :time_zone), or nil when unset — callers treat a nil zone as "leave the UTC value unshifted".
- .valid_zone?(value) ⇒ Boolean
- .validate_zone(record, attr) ⇒ Object
Class Method Details
.localize(value, zone_name) ⇒ Object
NULL-safe: localizes value (a UTC datetime) to zone_name, or returns it
unshifted when there is no zone to localize to (nil value, blank/invalid zone name).
42 43 44 45 46 47 |
# File 'app/models/concerns/time_zone_aware.rb', line 42 def self.localize(value, zone_name) return nil if value.nil? zone = zone_name.present? ? ActiveSupport::TimeZone[zone_name] : nil zone ? value.in_time_zone(zone) : value end |
.server_time_zone ⇒ Object
The deployment-wide IANA time zone identifier (ThecoreSettings ns: :main, key: :time_zone), or nil when unset — callers treat a nil zone as "leave the UTC value unshifted".
36 37 38 |
# File 'app/models/concerns/time_zone_aware.rb', line 36 def self.server_time_zone ::Settings.ns(:main).time_zone.presence end |
.valid_zone?(value) ⇒ Boolean
49 50 51 |
# File 'app/models/concerns/time_zone_aware.rb', line 49 def self.valid_zone?(value) value.blank? || ActiveSupport::TimeZone[value].present? end |
.validate_zone(record, attr) ⇒ Object
53 54 55 |
# File 'app/models/concerns/time_zone_aware.rb', line 53 def self.validate_zone(record, attr) record.errors.add(attr, :invalid) unless valid_zone?(record.read_attribute(attr)) end |