Class: Setting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Setting
- Defined in:
- app/models/setting.rb
Overview
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 `config/settings.default.yml`, and settings in the database table have the highest priority.
Constant Summary collapse
- @@cache =
@@yaml_settings = {}.with_indifferent_access
Class Method Summary collapse
-
.[](name) ⇒ Object
Get setting value (from database or loaded YAML files) ——————————————————————-.
-
.[]=(name, value) ⇒ Object
Set setting value ——————————————————————-.
-
.clear_cache! ⇒ Object
Cache should be cleared before each request.
- .database_and_table_exists? ⇒ Boolean
-
.dig(key, *rest) ⇒ Object
Retrieves the value object corresponding to the each key objects repeatedly.
-
.load_settings_from_yaml(file) ⇒ Object
Loads settings from YAML files.
-
.method_missing(method, *args) ⇒ Object
——————————————————————-.
-
.unroll(setting) ⇒ Object
Unrolls [ :one, :two ] settings array into [[ “One”, :one ], [ “Two”, :two ]] picking symbol translations from locale.
Class Method Details
.[](name) ⇒ Object
Get setting value (from database or loaded YAML files)
56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/setting.rb', line 56 def [](name) # Return value if cached return cache[name] if cache.key?(name) # Check database if database_and_table_exists? && (setting = find_by_name(name.to_s))&.value.present? return cache[name] = setting.value end # Check YAML settings cache[name] = yaml_settings[name] if yaml_settings.key?(name) end |
.[]=(name, value) ⇒ Object
Set setting value
70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/setting.rb', line 70 def []=(name, value) raise ArgumentError, "name cannot be blank" if name.blank? return nil unless database_and_table_exists? setting = find_by_name(name.to_s) || new(name: name.to_s) setting.value = value setting.save cache[name] = value end |
.clear_cache! ⇒ Object
Cache should be cleared before each request.
38 39 40 |
# File 'app/models/setting.rb', line 38 def clear_cache! @@cache = {}.with_indifferent_access end |
.database_and_table_exists? ⇒ Boolean
103 104 105 106 107 108 109 110 111 |
# File 'app/models/setting.rb', line 103 def database_and_table_exists? # 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 StandardError false end |
.dig(key, *rest) ⇒ Object
Retrieves the value object corresponding to the each key objects repeatedly. Equivalent to the #dig method on a Hash.
92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/setting.rb', line 92 def dig(key, *rest) value = self[key] if value.nil? || rest.empty? value elsif value.respond_to?(:dig) value.dig(*rest) else raise TypeError, "#{value.class} does not have #dig method" end end |
.load_settings_from_yaml(file) ⇒ Object
Loads settings from YAML files
114 115 116 117 |
# File 'app/models/setting.rb', line 114 def load_settings_from_yaml(file) settings = YAML.load(ERB.new(File.read(file)).result) @@yaml_settings.deep_merge!(settings) end |
.method_missing(method, *args) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/setting.rb', line 43 def method_missing(method, *args) super rescue NoMethodError method_name = method.to_s if method_name.last == "=" self[method_name.sub("=", "")] = args.first else self[method_name] end end |
.unroll(setting) ⇒ Object
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.
85 86 87 |
# File 'app/models/setting.rb', line 85 def unroll(setting) send(setting).map { |key| [key.is_a?(Symbol) ? I18n.t(key) : key, key.to_sym] } end |