Sha256: de3317623e69f5e1079c9da0e4eff991f84382f751ba4d4d9b5b686f2a8b63cb
Contents?: true
Size: 1.76 KB
Versions: 44
Compression:
Stored size: 1.76 KB
Contents
# frozen_string_literal: true # # Objects that can localize time to it, kicking out safe uses of a date format. # module TimeZoneAble extend ActiveSupport::Concern def self.included(base) base.class_eval do # # Fields # field :time_zone, type: String # # Validations # validates :time_zone, inclusion: TZInfo::Timezone.all_identifiers, presence: true # # Callbacks # before_validation :default_time_zone end end def time_zone_options TZInfo::Timezone.all_identifiers end # # Return the given time in the localized time for this object # def local_date(date, format = :medium, default = 'N/A') case date when Time tz = TZInfo::Timezone.get(time_zone.presence || SystemConfiguration.default_time_zone) date.present? ? I18n.l(date.in_time_zone(tz).to_date, format: format) : default when Date I18n.l(date, format: format) else default end rescue StandardError default end # # Return the given time in the localized time for this object # def local_time(time, format = :medium, default = 'N/A') tz = TZInfo::Timezone.get(time_zone.presence || SystemConfiguration.default_time_zone) time.present? ? I18n.l(time.in_time_zone(tz), format: format) : default rescue StandardError default end # # Return the updated_at if found, otherwise return the created_at. If neither are there # then return unknown # def local_updated_time(obj, format = :medium, default = 'N/A') obj.updated_at.present? ? local_time(obj.updated_at, format, default) : local_time(created_at, format, default) rescue StandardError default end def default_time_zone self.time_zone ||= SystemConfiguration.default_time_zone end end
Version data entries
44 entries across 44 versions & 1 rubygems