annotate app/models/setting.rb @ 861:b8105f717bf7 bug_182

Close obsolete branch bug_182
author Chris Cannam
date Fri, 10 Jun 2011 16:49:58 +0100
parents 513646585e45
children 051f544170fe
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Setting < ActiveRecord::Base
Chris@0 19
Chris@0 20 DATE_FORMATS = [
Chris@0 21 '%Y-%m-%d',
Chris@0 22 '%d/%m/%Y',
Chris@0 23 '%d.%m.%Y',
Chris@0 24 '%d-%m-%Y',
Chris@0 25 '%m/%d/%Y',
Chris@0 26 '%d %b %Y',
Chris@0 27 '%d %B %Y',
Chris@0 28 '%b %d, %Y',
Chris@0 29 '%B %d, %Y'
Chris@0 30 ]
Chris@0 31
Chris@0 32 TIME_FORMATS = [
Chris@0 33 '%H:%M',
Chris@0 34 '%I:%M %p'
Chris@0 35 ]
Chris@0 36
Chris@0 37 ENCODINGS = %w(US-ASCII
Chris@0 38 windows-1250
Chris@0 39 windows-1251
Chris@0 40 windows-1252
Chris@0 41 windows-1253
Chris@0 42 windows-1254
Chris@0 43 windows-1255
Chris@0 44 windows-1256
Chris@0 45 windows-1257
Chris@0 46 windows-1258
Chris@0 47 windows-31j
Chris@0 48 ISO-2022-JP
Chris@0 49 ISO-2022-KR
Chris@0 50 ISO-8859-1
Chris@0 51 ISO-8859-2
Chris@0 52 ISO-8859-3
Chris@0 53 ISO-8859-4
Chris@0 54 ISO-8859-5
Chris@0 55 ISO-8859-6
Chris@0 56 ISO-8859-7
Chris@0 57 ISO-8859-8
Chris@0 58 ISO-8859-9
Chris@0 59 ISO-8859-13
Chris@0 60 ISO-8859-15
Chris@0 61 KOI8-R
Chris@0 62 UTF-8
Chris@0 63 UTF-16
Chris@0 64 UTF-16BE
Chris@0 65 UTF-16LE
Chris@0 66 EUC-JP
Chris@0 67 Shift_JIS
Chris@0 68 GB18030
Chris@0 69 GBK
Chris@0 70 ISCII91
Chris@0 71 EUC-KR
Chris@0 72 Big5
Chris@0 73 Big5-HKSCS
Chris@0 74 TIS-620)
Chris@0 75
Chris@0 76 cattr_accessor :available_settings
Chris@0 77 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
Chris@0 78 Redmine::Plugin.all.each do |plugin|
Chris@0 79 next unless plugin.settings
Chris@0 80 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
Chris@0 81 end
Chris@0 82
Chris@0 83 validates_uniqueness_of :name
Chris@0 84 validates_inclusion_of :name, :in => @@available_settings.keys
Chris@0 85 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
Chris@0 86
Chris@0 87 # Hash used to cache setting values
Chris@0 88 @cached_settings = {}
Chris@0 89 @cached_cleared_on = Time.now
Chris@0 90
Chris@0 91 def value
Chris@0 92 v = read_attribute(:value)
Chris@0 93 # Unserialize serialized settings
Chris@0 94 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
Chris@0 95 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
Chris@0 96 v
Chris@0 97 end
Chris@0 98
Chris@0 99 def value=(v)
Chris@0 100 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
Chris@0 101 write_attribute(:value, v.to_s)
Chris@0 102 end
Chris@0 103
Chris@0 104 # Returns the value of the setting named name
Chris@0 105 def self.[](name)
Chris@0 106 v = @cached_settings[name]
Chris@0 107 v ? v : (@cached_settings[name] = find_or_default(name).value)
Chris@0 108 end
Chris@0 109
Chris@0 110 def self.[]=(name, v)
Chris@0 111 setting = find_or_default(name)
Chris@0 112 setting.value = (v ? v : "")
Chris@0 113 @cached_settings[name] = nil
Chris@0 114 setting.save
Chris@0 115 setting.value
Chris@0 116 end
Chris@0 117
Chris@0 118 # Defines getter and setter for each setting
Chris@0 119 # Then setting values can be read using: Setting.some_setting_name
Chris@0 120 # or set using Setting.some_setting_name = "some value"
Chris@0 121 @@available_settings.each do |name, params|
Chris@0 122 src = <<-END_SRC
Chris@0 123 def self.#{name}
Chris@0 124 self[:#{name}]
Chris@0 125 end
Chris@0 126
Chris@0 127 def self.#{name}?
Chris@0 128 self[:#{name}].to_i > 0
Chris@0 129 end
Chris@0 130
Chris@0 131 def self.#{name}=(value)
Chris@0 132 self[:#{name}] = value
Chris@0 133 end
Chris@0 134 END_SRC
Chris@0 135 class_eval src, __FILE__, __LINE__
Chris@0 136 end
Chris@0 137
Chris@0 138 # Helper that returns an array based on per_page_options setting
Chris@0 139 def self.per_page_options_array
Chris@0 140 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
Chris@0 141 end
Chris@0 142
Chris@0 143 def self.openid?
Chris@0 144 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
Chris@0 145 end
Chris@0 146
Chris@0 147 # Checks if settings have changed since the values were read
Chris@0 148 # and clears the cache hash if it's the case
Chris@0 149 # Called once per request
Chris@0 150 def self.check_cache
Chris@0 151 settings_updated_on = Setting.maximum(:updated_on)
Chris@0 152 if settings_updated_on && @cached_cleared_on <= settings_updated_on
Chris@0 153 @cached_settings.clear
Chris@0 154 @cached_cleared_on = Time.now
Chris@0 155 logger.info "Settings cache cleared." if logger
Chris@0 156 end
Chris@0 157 end
Chris@0 158
Chris@0 159 private
Chris@0 160 # Returns the Setting instance for the setting named name
Chris@0 161 # (record found in database or new record with default value)
Chris@0 162 def self.find_or_default(name)
Chris@0 163 name = name.to_s
Chris@0 164 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
Chris@0 165 setting = find_by_name(name)
Chris@0 166 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
Chris@0 167 end
Chris@0 168 end