簡體   English   中英

為什么 Rails6+ 默認開始在 config/environments/* 中添加 activesupport 需求?

[英]Why Rails6+ started adding activesupport requires in config/environments/* by default?

我對 Rails 版本升級有點晚了。 令我驚訝的是 Rails 生成的 config/environment/* 文件中有一堆 active_support 要求。

它們是干什么用的? 它與 Rails6 中引入的 Zeitwerk 有關嗎? 我不記得它們出現在舊版本的 Rails 中。

ag ^require config/environments
config/environments/development.rb
1:require "active_support/core_ext/integer/time"

config/environments/test.rb
1:require "active_support/core_ext/integer/time"

config/environments/production.rb
1:require "active_support/core_ext/integer/time"

重現步驟:

rails new myapp

cat Gemfile  | grep "^gem 'rails'"
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'

我試圖在 rails/rails CHANGELOG 和一些 git 責備中找到此更新,但這並沒有幫助。

在每個環境文件的后面一點,使用 require 語句加載的代碼(或者在生產文件的情況下在注釋中引用)。 從默認development.rb

# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp/caching-dev.txt').exist?
  config.cache_store = :memory_store
  config.public_file_server.headers = {
    'Cache-Control' => "public, max-age=#{2.days.to_i}" # <- NOTE THIS LINE
  }
else
  config.action_controller.perform_caching = false

  config.cache_store = :null_store
end

require語句增加了對諸如2.days.to_i - core_ext/integer/time加載一些函數的表達式的支持,但也需要core_ext/numeric/time

因此, require語句是一個很好的 Ruby 公民,並確保保證加載其代碼所依賴的 Rails 的特定部分,以便能夠正確解析該行。

我不知道為什么以前不需要它(這可能是與 Zeitwerk 相關的問題,正如您所建議的,這是我還不太熟悉的 Rails 6+ 的一部分)。

但歸根結底,如果在評估此文件之前加載了整個 Rails 堆棧,則 require 不會產生任何額外的影響 - 如果不是,此文件將加載它需要的內容,然后 Rails 的 rest 將根據需要加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM