Skip to content

Commit

Permalink
Fix rails 7 deprecation on ActiveRecord::Base.default_timezone
Browse files Browse the repository at this point in the history
Rails 7 added ActiveRecord.default_timezone and 7.1 will remove it from ActiveRecord::Base.

Extract a memoized method to hide this logic.
  • Loading branch information
jrafanie committed Mar 6, 2024
1 parent 0667ba7 commit 400138d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/inventory_refresh/save_collection/saver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,18 @@ def assert_referential_integrity(hash)
# @return [Time] A rails friendly time getting config from ActiveRecord::Base.default_timezone (can be :local
# or :utc)
def time_now
if ActiveRecord::Base.default_timezone == :utc
if default_timezone_utc?
Time.now.utc
else
Time.zone.now
end
end

def default_timezone_utc?
# Rails 7 added ActiveRecord.default_timezone and 7.1 will remove it from ActiveRecord::Base
@default_timezone_utc ||= (ActiveRecord.respond_to?(:default_timezone) ? ActiveRecord.default_timezone : ActiveRecord::Base.default_timezone) == :utc
end

# Enriches data hash with timestamp columns
#
# @param hash [Hash] data hash
Expand Down

0 comments on commit 400138d

Please sign in to comment.