annotate app/models/setting.rb @ 1516:b450a9d58aed redmine-2.4

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