Class: Setting

Inherits:
ActiveRecord::Base
  • Object
show all
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

Class Method Details

.[](name) ⇒ Object

Get setting value (from database or loaded YAML files)




69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/setting.rb', line 69

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.to_s)
      unless setting.value.nil?
        return cache[name] = setting.value
      end
    end
  end
  # Check YAML settings
  if yaml_settings.has_key?(name)
    return cache[name] = yaml_settings[name]
  end
end

.[]=(name, value) ⇒ Object

Set setting value




89
90
91
92
93
94
95
# File 'app/models/setting.rb', line 89

def []=(name, value)
  return nil unless database_and_table_exists?
  setting = self.find_by_name(name.to_s) || self.new(:name => name)
  setting.value = value
  setting.save
  cache[name] = value
end

.clear_cache!Object

Cache should be cleared before each request.



49
50
51
# File 'app/models/setting.rb', line 49

def clear_cache!
  @@cache = {}.with_indifferent_access
end

.database_and_table_exists?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'app/models/setting.rb', line 106

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 false
end

.load_settings_from_yamlObject

Loads settings from YAML files



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/setting.rb', line 115

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 (recursively)
        @@yaml_settings.deep_merge!(settings)
      rescue Exception => ex
        puts "Settings couldn't be loaded from #{file}: #{ex.message}"
      end
    end
  end
  yaml_settings
end

.method_missing(method, *args) ⇒ Object




54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/setting.rb', line 54

def method_missing(method, *args)
  begin
    super
  rescue NoMethodError
    method_name = method.to_s
    if method_name.last == "="
      self[method_name.sub("=", "")] = args.first
    else
      self[method_name]
    end
  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.




102
103
104
# File 'app/models/setting.rb', line 102

def unroll(setting)
  send(setting).map { |key| [ key.is_a?(Symbol) ? I18n.t(key) : key, key.to_sym ] }
end