annotate app/models/setting.rb @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents e248c7af89ec
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@441 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@441 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@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@0 30 ]
Chris@441 31
Chris@0 32 TIME_FORMATS = [
Chris@0 33 '%H:%M',
Chris@0 34 '%I:%M %p'
Chris@0 35 ]
Chris@441 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@245 68 CP932
Chris@0 69 GB18030
Chris@0 70 GBK
Chris@0 71 ISCII91
Chris@0 72 EUC-KR
Chris@0 73 Big5
Chris@0 74 Big5-HKSCS
Chris@0 75 TIS-620)
Chris@441 76
Chris@0 77 cattr_accessor :available_settings
Chris@909 78 @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
Chris@0 79 Redmine::Plugin.all.each do |plugin|
Chris@0 80 next unless plugin.settings
Chris@441 81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
Chris@0 82 end
Chris@441 83
Chris@0 84 validates_uniqueness_of :name
Chris@0 85 validates_inclusion_of :name, :in => @@available_settings.keys
Chris@1517 86 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
Chris@1517 87 (s = @@available_settings[setting.name]) && s['format'] == 'int'
Chris@1517 88 }
Chris@0 89
Chris@0 90 # Hash used to cache setting values
Chris@0 91 @cached_settings = {}
Chris@0 92 @cached_cleared_on = Time.now
Chris@441 93
Chris@0 94 def value
Chris@0 95 v = read_attribute(:value)
Chris@0 96 # Unserialize serialized settings
Chris@0 97 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
Chris@0 98 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
Chris@0 99 v
Chris@0 100 end
Chris@441 101
Chris@0 102 def value=(v)
Chris@0 103 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
Chris@0 104 write_attribute(:value, v.to_s)
Chris@0 105 end
Chris@441 106
Chris@0 107 # Returns the value of the setting named name
Chris@0 108 def self.[](name)
Chris@0 109 v = @cached_settings[name]
Chris@0 110 v ? v : (@cached_settings[name] = find_or_default(name).value)
Chris@0 111 end
Chris@441 112
Chris@0 113 def self.[]=(name, v)
Chris@0 114 setting = find_or_default(name)
Chris@0 115 setting.value = (v ? v : "")
Chris@0 116 @cached_settings[name] = nil
Chris@0 117 setting.save
Chris@0 118 setting.value
Chris@0 119 end
Chris@441 120
Chris@0 121 # Defines getter and setter for each setting
Chris@0 122 # Then setting values can be read using: Setting.some_setting_name
Chris@0 123 # or set using Setting.some_setting_name = "some value"
Chris@0 124 @@available_settings.each do |name, params|
Chris@0 125 src = <<-END_SRC
Chris@0 126 def self.#{name}
Chris@0 127 self[:#{name}]
Chris@0 128 end
Chris@0 129
Chris@0 130 def self.#{name}?
Chris@0 131 self[:#{name}].to_i > 0
Chris@0 132 end
Chris@0 133
Chris@0 134 def self.#{name}=(value)
Chris@0 135 self[:#{name}] = value
Chris@0 136 end
Chris@1464 137 END_SRC
Chris@0 138 class_eval src, __FILE__, __LINE__
Chris@0 139 end
Chris@441 140
Chris@1464 141 # Sets a setting value from params
Chris@1464 142 def self.set_from_params(name, params)
Chris@1464 143 params = params.dup
Chris@1464 144 params.delete_if {|v| v.blank? } if params.is_a?(Array)
Chris@1464 145
Chris@1464 146 m = "#{name}_from_params"
Chris@1464 147 if respond_to? m
Chris@1464 148 self[name.to_sym] = send m, params
Chris@1464 149 else
Chris@1464 150 self[name.to_sym] = params
Chris@1464 151 end
Chris@1464 152 end
Chris@1464 153
Chris@1464 154 # Returns a hash suitable for commit_update_keywords setting
Chris@1464 155 #
Chris@1464 156 # Example:
Chris@1464 157 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
Chris@1464 158 # Setting.commit_update_keywords_from_params(params)
Chris@1464 159 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
Chris@1464 160 def self.commit_update_keywords_from_params(params)
Chris@1464 161 s = []
Chris@1464 162 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
Chris@1464 163 attributes = params.except(:keywords).keys
Chris@1464 164 params[:keywords].each_with_index do |keywords, i|
Chris@1464 165 next if keywords.blank?
Chris@1464 166 s << attributes.inject({}) {|h, a|
Chris@1464 167 value = params[a][i].to_s
Chris@1464 168 h[a.to_s] = value if value.present?
Chris@1464 169 h
Chris@1464 170 }.merge('keywords' => keywords)
Chris@1464 171 end
Chris@1464 172 end
Chris@1464 173 s
Chris@1464 174 end
Chris@1464 175
Chris@0 176 # Helper that returns an array based on per_page_options setting
Chris@0 177 def self.per_page_options_array
Chris@0 178 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
Chris@0 179 end
Chris@441 180
Chris@1464 181 # Helper that returns a Hash with single update keywords as keys
Chris@1464 182 def self.commit_update_keywords_array
Chris@1464 183 a = []
Chris@1464 184 if commit_update_keywords.is_a?(Array)
Chris@1464 185 commit_update_keywords.each do |rule|
Chris@1464 186 next unless rule.is_a?(Hash)
Chris@1464 187 rule = rule.dup
Chris@1464 188 rule.delete_if {|k, v| v.blank?}
Chris@1464 189 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
Chris@1464 190 next if keywords.empty?
Chris@1464 191 a << rule.merge('keywords' => keywords)
Chris@1464 192 end
Chris@1464 193 end
Chris@1464 194 a
Chris@1464 195 end
Chris@1464 196
Chris@1464 197 def self.commit_fix_keywords
Chris@1464 198 ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
Chris@1464 199 if commit_update_keywords.is_a?(Array)
Chris@1464 200 commit_update_keywords.first && commit_update_keywords.first['keywords']
Chris@1464 201 end
Chris@1464 202 end
Chris@1464 203
Chris@1464 204 def self.commit_fix_status_id
Chris@1464 205 ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
Chris@1464 206 if commit_update_keywords.is_a?(Array)
Chris@1464 207 commit_update_keywords.first && commit_update_keywords.first['status_id']
Chris@1464 208 end
Chris@1464 209 end
Chris@1464 210
Chris@1464 211 def self.commit_fix_done_ratio
Chris@1464 212 ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
Chris@1464 213 if commit_update_keywords.is_a?(Array)
Chris@1464 214 commit_update_keywords.first && commit_update_keywords.first['done_ratio']
Chris@1464 215 end
Chris@1464 216 end
Chris@1464 217
Chris@0 218 def self.openid?
Chris@0 219 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
Chris@0 220 end
Chris@441 221
Chris@0 222 # Checks if settings have changed since the values were read
Chris@0 223 # and clears the cache hash if it's the case
Chris@0 224 # Called once per request
Chris@0 225 def self.check_cache
Chris@0 226 settings_updated_on = Setting.maximum(:updated_on)
Chris@0 227 if settings_updated_on && @cached_cleared_on <= settings_updated_on
Chris@909 228 clear_cache
Chris@0 229 end
Chris@0 230 end
Chris@1464 231
Chris@909 232 # Clears the settings cache
Chris@909 233 def self.clear_cache
Chris@909 234 @cached_settings.clear
Chris@909 235 @cached_cleared_on = Time.now
Chris@909 236 logger.info "Settings cache cleared." if logger
Chris@909 237 end
Chris@441 238
Chris@0 239 private
Chris@0 240 # Returns the Setting instance for the setting named name
Chris@0 241 # (record found in database or new record with default value)
Chris@0 242 def self.find_or_default(name)
Chris@0 243 name = name.to_s
Chris@441 244 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
Chris@1517 245 setting = where(:name => name).first
Chris@909 246 unless setting
Chris@1517 247 setting = new
Chris@1517 248 setting.name = name
Chris@909 249 setting.value = @@available_settings[name]['default']
Chris@909 250 end
Chris@909 251 setting
Chris@0 252 end
Chris@0 253 end