Project

General

Profile

Actions

Defect #38250

closed

config/settings.yml not closed in Setting.load_available_settings

Added by Thomas Löber over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Code cleanup/refactoring
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

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
Actions #1

Updated by Go MAEDA over 1 year ago

  • 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
Actions #2

Updated by Go MAEDA over 1 year ago

  • 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.

Actions #3

Updated by Holger Just over 1 year ago

Rather than explicitly opening the file ourselves, we may also use YAML.load_file.

Actions #4

Updated by Thomas Löber over 1 year ago

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

Actions #5

Updated by Go MAEDA over 1 year ago

Holger Just wrote:

Rather than explicitly opening the file ourselves, we may also use YAML.load_file.

Thank you, updated the code in r22101.

Actions #6

Updated by Go MAEDA over 1 year ago

  • Target version changed from 4.2.10 to 5.1.0
Actions #7

Updated by Go MAEDA over 1 year ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF