Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: class Setting < ActiveRecord::Base Chris@1295: Chris@1295: DATE_FORMATS = [ Chris@1295: '%Y-%m-%d', Chris@1295: '%d/%m/%Y', Chris@1295: '%d.%m.%Y', Chris@1295: '%d-%m-%Y', Chris@1295: '%m/%d/%Y', Chris@1295: '%d %b %Y', Chris@1295: '%d %B %Y', Chris@1295: '%b %d, %Y', Chris@1295: '%B %d, %Y' Chris@1295: ] Chris@1295: Chris@1295: TIME_FORMATS = [ Chris@1295: '%H:%M', Chris@1295: '%I:%M %p' Chris@1295: ] Chris@1295: Chris@1295: ENCODINGS = %w(US-ASCII Chris@1295: windows-1250 Chris@1295: windows-1251 Chris@1295: windows-1252 Chris@1295: windows-1253 Chris@1295: windows-1254 Chris@1295: windows-1255 Chris@1295: windows-1256 Chris@1295: windows-1257 Chris@1295: windows-1258 Chris@1295: windows-31j Chris@1295: ISO-2022-JP Chris@1295: ISO-2022-KR Chris@1295: ISO-8859-1 Chris@1295: ISO-8859-2 Chris@1295: ISO-8859-3 Chris@1295: ISO-8859-4 Chris@1295: ISO-8859-5 Chris@1295: ISO-8859-6 Chris@1295: ISO-8859-7 Chris@1295: ISO-8859-8 Chris@1295: ISO-8859-9 Chris@1295: ISO-8859-13 Chris@1295: ISO-8859-15 Chris@1295: KOI8-R Chris@1295: UTF-8 Chris@1295: UTF-16 Chris@1295: UTF-16BE Chris@1295: UTF-16LE Chris@1295: EUC-JP Chris@1295: Shift_JIS Chris@1295: CP932 Chris@1295: GB18030 Chris@1295: GBK Chris@1295: ISCII91 Chris@1295: EUC-KR Chris@1295: Big5 Chris@1295: Big5-HKSCS Chris@1295: TIS-620) Chris@1295: Chris@1295: cattr_accessor :available_settings Chris@1295: @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml")) Chris@1295: Redmine::Plugin.all.each do |plugin| Chris@1295: next unless plugin.settings Chris@1295: @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} Chris@1295: end Chris@1295: Chris@1295: validates_uniqueness_of :name Chris@1295: validates_inclusion_of :name, :in => @@available_settings.keys Chris@1295: validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } Chris@1295: Chris@1295: # Hash used to cache setting values Chris@1295: @cached_settings = {} Chris@1295: @cached_cleared_on = Time.now Chris@1295: Chris@1295: def value Chris@1295: v = read_attribute(:value) Chris@1295: # Unserialize serialized settings Chris@1295: v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) Chris@1295: v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank? Chris@1295: v Chris@1295: end Chris@1295: Chris@1295: def value=(v) Chris@1295: v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] Chris@1295: write_attribute(:value, v.to_s) Chris@1295: end Chris@1295: Chris@1295: # Returns the value of the setting named name Chris@1295: def self.[](name) Chris@1295: v = @cached_settings[name] Chris@1295: v ? v : (@cached_settings[name] = find_or_default(name).value) Chris@1295: end Chris@1295: Chris@1295: def self.[]=(name, v) Chris@1295: setting = find_or_default(name) Chris@1295: setting.value = (v ? v : "") Chris@1295: @cached_settings[name] = nil Chris@1295: setting.save Chris@1295: setting.value Chris@1295: end Chris@1295: Chris@1295: # Defines getter and setter for each setting Chris@1295: # Then setting values can be read using: Setting.some_setting_name Chris@1295: # or set using Setting.some_setting_name = "some value" Chris@1295: @@available_settings.each do |name, params| Chris@1295: src = <<-END_SRC Chris@1295: def self.#{name} Chris@1295: self[:#{name}] Chris@1295: end Chris@1295: Chris@1295: def self.#{name}? Chris@1295: self[:#{name}].to_i > 0 Chris@1295: end Chris@1295: Chris@1295: def self.#{name}=(value) Chris@1295: self[:#{name}] = value Chris@1295: end Chris@1295: END_SRC Chris@1295: class_eval src, __FILE__, __LINE__ Chris@1295: end Chris@1295: Chris@1295: # Helper that returns an array based on per_page_options setting Chris@1295: def self.per_page_options_array Chris@1295: per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort Chris@1295: end Chris@1295: Chris@1295: def self.openid? Chris@1295: Object.const_defined?(:OpenID) && self[:openid].to_i > 0 Chris@1295: end Chris@1295: Chris@1295: # Checks if settings have changed since the values were read Chris@1295: # and clears the cache hash if it's the case Chris@1295: # Called once per request Chris@1295: def self.check_cache Chris@1295: settings_updated_on = Setting.maximum(:updated_on) Chris@1295: if settings_updated_on && @cached_cleared_on <= settings_updated_on Chris@1295: clear_cache Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Clears the settings cache Chris@1295: def self.clear_cache Chris@1295: @cached_settings.clear Chris@1295: @cached_cleared_on = Time.now Chris@1295: logger.info "Settings cache cleared." if logger Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: # Returns the Setting instance for the setting named name Chris@1295: # (record found in database or new record with default value) Chris@1295: def self.find_or_default(name) Chris@1295: name = name.to_s Chris@1295: raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) Chris@1295: setting = find_by_name(name) Chris@1295: unless setting Chris@1295: setting = new(:name => name) Chris@1295: setting.value = @@available_settings[name]['default'] Chris@1295: end Chris@1295: setting Chris@1295: end Chris@1295: end