Sha256: 6433010e6512f53761bfb954d769dc9032ae3e6d5f934c3bf6c172058d6a10e8

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module HasZone
  extend ActiveSupport::Concern

  included do

    # This allows the model has the ability to store TimeZone.
    # Usage:
    # has_zone with: :time_zone
    #
    # It defines the following methods:
    #
    # :zone
    # Public: Returns the Rails Time Zone.
    #
    #
    # :zone=(identifier)
    # Public: Sets the TimeZone by TZinfo identifier.
    #
    # identifier - Time Zone identifier
    #
    def self.has_zone(options = { with: :time_zone })
      define_method(:zone) do
        return nil if send(options[:with]).nil?
        @zone ||= ::ActiveSupport::TimeZone.tzids_map[send(options[:with])].try(:name)
      end

      define_method(:zone=) do |tz|
        zone = tz.is_a?(String) ? ::ActiveSupport::TimeZone.new(tz) : tz
        send "#{options[:with]}=".to_sym, zone.try(:identifier)
      end

      define_method("#{options[:with]}=".to_sym) do |tz|
        # Since we memoize the time zone, we need to reset it if the time zone has changed
        @zone = nil
        write_attribute(options[:with], tz)
      end

      define_method(:alias_time_zone) do
        if send(options[:with]) == "UTC"
          send "#{options[:with]}=".to_sym, "Etc/UTC"
        elsif ::ActiveSupport::TimeZone.tzids_map.keys.exclude? send(options[:with])
          Rails.logger.warn("Time zone #{send(options[:with])} is not a valid time zone.") if defined? Rails
        end
      end

      before_validation :alias_time_zone

    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_zone-0.0.4 lib/has_zone/has_zone.rb
has_zone-0.0.3 lib/has_zone/has_zone.rb