app/models/setting.rb in fat_free_crm-0.11.1 vs app/models/setting.rb in fat_free_crm-0.11.2
- old
+ new
@@ -25,11 +25,11 @@
# created_at :datetime
# updated_at :datetime
#
# Fat Free CRM settings are stored in three places, and are loaded in the following order:
-#
+#
# 1) config/settings.default.yml
# 2) config/settings.yml (if exists)
# 3) 'settings' table in database (if exists)
#
# Any configured settings in `config/settings.yml` will override those in
@@ -42,20 +42,20 @@
# Use class variables for cache and yaml settings.
cattr_accessor :cache, :yaml_settings
@@cache = @@yaml_settings = {}.with_indifferent_access
class << self
-
+
# Cache should be cleared before each request.
def clear_cache!
@@cache = {}.with_indifferent_access
end
-
+
#-------------------------------------------------------------------
def method_missing(method, *args)
begin
- super(method, *args)
+ super
rescue NoMethodError
method_name = method.to_s
if method_name.last == "="
self[method_name.sub("=", "")] = args.first
else
@@ -68,35 +68,35 @@
#-------------------------------------------------------------------
def [](name)
# Return value if cached
return cache[name] if cache.has_key?(name)
# Check database
- if database_and_table_exists?
- if setting = self.find_by_name(name)
+ if database_and_table_exists?
+ if setting = self.find_by_name(name.to_s)
unless setting.value.nil?
return cache[name] = setting.value
end
end
end
- # Check YAML settings
+ # Check YAML settings
if yaml_settings.has_key?(name)
return cache[name] = yaml_settings[name]
end
end
-
+
# Set setting value
#-------------------------------------------------------------------
def []=(name, value)
return nil unless database_and_table_exists?
- setting = self.find_by_name(name) || self.new(:name => name)
+ setting = self.find_by_name(name.to_s) || self.new(:name => name)
setting.value = value
setting.save
cache[name] = value
end
-
+
# Unrolls [ :one, :two ] settings array into [[ "One", :one ], [ "Two", :two ]]
# picking symbol translations from locale. If setting is not a symbol but
# string it gets copied without translation.
#-------------------------------------------------------------------
def unroll(setting)
@@ -107,27 +107,27 @@
# Returns false if table or database is unavailable.
# Catches all database-related errors, so that Setting will return nil
# instead of crashing the entire application.
table_exists? rescue false
end
-
-
+
+
# Loads settings from YAML files
def load_settings_from_yaml
@@yaml_settings = {}.with_indifferent_access
-
+
setting_files = [
FatFreeCRM.root.join("config", "settings.default.yml"),
Rails.root.join("config", "settings.yml")
]
# Load default settings, then override with custom settings
setting_files.each do |file|
if File.exist?(file)
begin
settings = YAML.load_file(file)
- # Merge settings into current settings hash
- @@yaml_settings.merge!(settings)
+ # Merge settings into current settings hash (recursively)
+ @@yaml_settings.deep_merge!(settings)
rescue Exception => ex
puts "Settings couldn't be loaded from #{file}: #{ex.message}"
end
end
end