annotate .svn/pristine/77/77edc6851d3d16ead6a951e08fc89080d2584af3.svn-base @ 1464:261b3d9a4903 redmine-2.4

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