annotate .svn/pristine/2a/2a8a6f95df9c9b5ea1480ac33b5f365836080244.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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