Defect #38250
closed
config/settings.yml not closed in Setting.load_available_settings
Added by Thomas Löber almost 2 years ago.
Updated almost 2 years ago.
Category:
Code cleanup/refactoring
Description
The file config/settings.yml
is not closed after it is read in Setting.load_available_settings
.
This can cause problems during development, for example when changing Git branches while the Rails server is running (error: unable to unlink old 'config/settings.yml').
The solution is to pass a block to File.open
, as it will automatically close the file after the block is executed.
def self.load_available_settings
File.open("#{Rails.root}/config/settings.yml") do |f|
YAML.load(f).each do |name, options|
define_setting name, options
end
end
end
- Subject changed from settings.yml not closed to config/settings.yml not closed in Setting.load_available_settings
- Status changed from New to Confirmed
- Target version set to 4.2.10
- Status changed from Confirmed to Resolved
- Assignee set to Go MAEDA
- Resolution set to Fixed
Committed the fix. Thank you for reporting and fixing the issue.
Rather than explicitly opening the file ourselves, we may also use YAML.load_file
.
Until today, I didn't know that this method even existed. You never stop learning :-)
Here is a new patch using YAML.load_file
:
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -320,7 +320,7 @@
end
def self.load_available_settings
- YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
+ YAML.load_file(Rails.root + "config/settings.yml").each do |name, options|
define_setting name, options
end
end
Holger Just wrote:
Rather than explicitly opening the file ourselves, we may also use YAML.load_file
.
Thank you, updated the code in r22101.
- Target version changed from 4.2.10 to 5.1.0
- Status changed from Resolved to Closed
Also available in: Atom
PDF