Sha256: 32659b70f79004c291d502748c1fe1df77d9cdb129562725a9e479fad3d4fddc

Contents?: true

Size: 783 Bytes

Versions: 6

Compression:

Stored size: 783 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 thrad 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

6 entries across 6 versions & 1 rubygems

Version Path
rails_base-0.53.0 app/models/rails_base/application_record.rb
rails_base-0.52.3 app/models/rails_base/application_record.rb
rails_base-0.52.1 app/models/rails_base/application_record.rb
rails_base-0.52.0 app/models/rails_base/application_record.rb
rails_base-0.51.1 app/models/rails_base/application_record.rb
rails_base-0.51.0 app/models/rails_base/application_record.rb