Sha256: 4793f9a04fb5ba73ae9bf99ebaf2d2dd0184c55eae9f6e304eadbae8d9b7e861

Contents?: true

Size: 784 Bytes

Versions: 8

Compression:

Stored size: 784 Bytes

Contents

module RailsBase
  class ApplicationRecord < ::ApplicationRecord
    self.abstract_class = true

    def self._magically_defined_time_objects
      columns.each do |column|
        next unless [:datetime].include?(column.type)

        # This is actually pretty cool. If you set the thread corectly, you can
        define_method("#{column.name}") do
          thread_tz = Thread.current[RailsBase::ApplicationController::TIMEZONE_THREAD_NAME]
          return super() if thread_tz.nil?
          time = self[column.name].in_time_zone(thread_tz) rescue self[column.name]

          Rails.logger.debug { "#{self.class.name}.#{column.name} intercepted :datetime [#{self[column.name]}] and returned [#{time}] - tz[#{thread_tz}]" }

          time
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails_base-0.61.0 app/models/rails_base/application_record.rb
rails_base-0.60.0 app/models/rails_base/application_record.rb
rails_base-0.58.0 app/models/rails_base/application_record.rb
rails_base-0.57.0 app/models/rails_base/application_record.rb
rails_base-0.56.0 app/models/rails_base/application_record.rb
rails_base-0.55.0 app/models/rails_base/application_record.rb
rails_base-0.54.0 app/models/rails_base/application_record.rb
rails_base-0.53.1 app/models/rails_base/application_record.rb