annotate .svn/pristine/74/740710e32980903c8cdfb79c5eb1452c04126e11.svn-base @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class Setting < ActiveRecord::Base
Chris@1517 19
Chris@1517 20 DATE_FORMATS = [
Chris@1517 21 '%Y-%m-%d',
Chris@1517 22 '%d/%m/%Y',
Chris@1517 23 '%d.%m.%Y',
Chris@1517 24 '%d-%m-%Y',
Chris@1517 25 '%m/%d/%Y',
Chris@1517 26 '%d %b %Y',
Chris@1517 27 '%d %B %Y',
Chris@1517 28 '%b %d, %Y',
Chris@1517 29 '%B %d, %Y'
Chris@1517 30 ]
Chris@1517 31
Chris@1517 32 TIME_FORMATS = [
Chris@1517 33 '%H:%M',
Chris@1517 34 '%I:%M %p'
Chris@1517 35 ]
Chris@1517 36
Chris@1517 37 ENCODINGS = %w(US-ASCII
Chris@1517 38 windows-1250
Chris@1517 39 windows-1251
Chris@1517 40 windows-1252
Chris@1517 41 windows-1253
Chris@1517 42 windows-1254
Chris@1517 43 windows-1255
Chris@1517 44 windows-1256
Chris@1517 45 windows-1257
Chris@1517 46 windows-1258
Chris@1517 47 windows-31j
Chris@1517 48 ISO-2022-JP
Chris@1517 49 ISO-2022-KR
Chris@1517 50 ISO-8859-1
Chris@1517 51 ISO-8859-2
Chris@1517 52 ISO-8859-3
Chris@1517 53 ISO-8859-4
Chris@1517 54 ISO-8859-5
Chris@1517 55 ISO-8859-6
Chris@1517 56 ISO-8859-7
Chris@1517 57 ISO-8859-8
Chris@1517 58 ISO-8859-9
Chris@1517 59 ISO-8859-13
Chris@1517 60 ISO-8859-15
Chris@1517 61 KOI8-R
Chris@1517 62 UTF-8
Chris@1517 63 UTF-16
Chris@1517 64 UTF-16BE
Chris@1517 65 UTF-16LE
Chris@1517 66 EUC-JP
Chris@1517 67 Shift_JIS
Chris@1517 68 CP932
Chris@1517 69 GB18030
Chris@1517 70 GBK
Chris@1517 71 ISCII91
Chris@1517 72 EUC-KR
Chris@1517 73 Big5
Chris@1517 74 Big5-HKSCS
Chris@1517 75 TIS-620)
Chris@1517 76
Chris@1517 77 cattr_accessor :available_settings
Chris@1517 78 @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
Chris@1517 79 Redmine::Plugin.all.each do |plugin|
Chris@1517 80 next unless plugin.settings
Chris@1517 81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
Chris@1517 82 end
Chris@1517 83
Chris@1517 84 validates_uniqueness_of :name
Chris@1517 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@1517 89
Chris@1517 90 # Hash used to cache setting values
Chris@1517 91 @cached_settings = {}
Chris@1517 92 @cached_cleared_on = Time.now
Chris@1517 93
Chris@1517 94 def value
Chris@1517 95 v = read_attribute(:value)
Chris@1517 96 # Unserialize serialized settings
Chris@1517 97 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
Chris@1517 98 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
Chris@1517 99 v
Chris@1517 100 end
Chris@1517 101
Chris@1517 102 def value=(v)
Chris@1517 103 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
Chris@1517 104 write_attribute(:value, v.to_s)
Chris@1517 105 end
Chris@1517 106
Chris@1517 107 # Returns the value of the setting named name
Chris@1517 108 def self.[](name)
Chris@1517 109 v = @cached_settings[name]
Chris@1517 110 v ? v : (@cached_settings[name] = find_or_default(name).value)
Chris@1517 111 end
Chris@1517 112
Chris@1517 113 def self.[]=(name, v)
Chris@1517 114 setting = find_or_default(name)
Chris@1517 115 setting.value = (v ? v : "")
Chris@1517 116 @cached_settings[name] = nil
Chris@1517 117 setting.save
Chris@1517 118 setting.value
Chris@1517 119 end
Chris@1517 120
Chris@1517 121 # Defines getter and setter for each setting
Chris@1517 122 # Then setting values can be read using: Setting.some_setting_name
Chris@1517 123 # or set using Setting.some_setting_name = "some value"
Chris@1517 124 @@available_settings.each do |name, params|
Chris@1517 125 src = <<-END_SRC
Chris@1517 126 def self.#{name}
Chris@1517 127 self[:#{name}]
Chris@1517 128 end
Chris@1517 129
Chris@1517 130 def self.#{name}?
Chris@1517 131 self[:#{name}].to_i > 0
Chris@1517 132 end
Chris@1517 133
Chris@1517 134 def self.#{name}=(value)
Chris@1517 135 self[:#{name}] = value
Chris@1517 136 end
Chris@1517 137 END_SRC
Chris@1517 138 class_eval src, __FILE__, __LINE__
Chris@1517 139 end
Chris@1517 140
Chris@1517 141 # Sets a setting value from params
Chris@1517 142 def self.set_from_params(name, params)
Chris@1517 143 params = params.dup
Chris@1517 144 params.delete_if {|v| v.blank? } if params.is_a?(Array)
Chris@1517 145
Chris@1517 146 m = "#{name}_from_params"
Chris@1517 147 if respond_to? m
Chris@1517 148 self[name.to_sym] = send m, params
Chris@1517 149 else
Chris@1517 150 self[name.to_sym] = params
Chris@1517 151 end
Chris@1517 152 end
Chris@1517 153
Chris@1517 154 # Returns a hash suitable for commit_update_keywords setting
Chris@1517 155 #
Chris@1517 156 # Example:
Chris@1517 157 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
Chris@1517 158 # Setting.commit_update_keywords_from_params(params)
Chris@1517 159 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
Chris@1517 160 def self.commit_update_keywords_from_params(params)
Chris@1517 161 s = []
Chris@1517 162 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
Chris@1517 163 attributes = params.except(:keywords).keys
Chris@1517 164 params[:keywords].each_with_index do |keywords, i|
Chris@1517 165 next if keywords.blank?
Chris@1517 166 s << attributes.inject({}) {|h, a|
Chris@1517 167 value = params[a][i].to_s
Chris@1517 168 h[a.to_s] = value if value.present?
Chris@1517 169 h
Chris@1517 170 }.merge('keywords' => keywords)
Chris@1517 171 end
Chris@1517 172 end
Chris@1517 173 s
Chris@1517 174 end
Chris@1517 175
Chris@1517 176 # Helper that returns an array based on per_page_options setting
Chris@1517 177 def self.per_page_options_array
Chris@1517 178 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
Chris@1517 179 end
Chris@1517 180
Chris@1517 181 # Helper that returns a Hash with single update keywords as keys
Chris@1517 182 def self.commit_update_keywords_array
Chris@1517 183 a = []
Chris@1517 184 if commit_update_keywords.is_a?(Array)
Chris@1517 185 commit_update_keywords.each do |rule|
Chris@1517 186 next unless rule.is_a?(Hash)
Chris@1517 187 rule = rule.dup
Chris@1517 188 rule.delete_if {|k, v| v.blank?}
Chris@1517 189 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
Chris@1517 190 next if keywords.empty?
Chris@1517 191 a << rule.merge('keywords' => keywords)
Chris@1517 192 end
Chris@1517 193 end
Chris@1517 194 a
Chris@1517 195 end
Chris@1517 196
Chris@1517 197 def self.commit_fix_keywords
Chris@1517 198 ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
Chris@1517 199 if commit_update_keywords.is_a?(Array)
Chris@1517 200 commit_update_keywords.first && commit_update_keywords.first['keywords']
Chris@1517 201 end
Chris@1517 202 end
Chris@1517 203
Chris@1517 204 def self.commit_fix_status_id
Chris@1517 205 ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
Chris@1517 206 if commit_update_keywords.is_a?(Array)
Chris@1517 207 commit_update_keywords.first && commit_update_keywords.first['status_id']
Chris@1517 208 end
Chris@1517 209 end
Chris@1517 210
Chris@1517 211 def self.commit_fix_done_ratio
Chris@1517 212 ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
Chris@1517 213 if commit_update_keywords.is_a?(Array)
Chris@1517 214 commit_update_keywords.first && commit_update_keywords.first['done_ratio']
Chris@1517 215 end
Chris@1517 216 end
Chris@1517 217
Chris@1517 218 def self.openid?
Chris@1517 219 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
Chris@1517 220 end
Chris@1517 221
Chris@1517 222 # Checks if settings have changed since the values were read
Chris@1517 223 # and clears the cache hash if it's the case
Chris@1517 224 # Called once per request
Chris@1517 225 def self.check_cache
Chris@1517 226 settings_updated_on = Setting.maximum(:updated_on)
Chris@1517 227 if settings_updated_on && @cached_cleared_on <= settings_updated_on
Chris@1517 228 clear_cache
Chris@1517 229 end
Chris@1517 230 end
Chris@1517 231
Chris@1517 232 # Clears the settings cache
Chris@1517 233 def self.clear_cache
Chris@1517 234 @cached_settings.clear
Chris@1517 235 @cached_cleared_on = Time.now
Chris@1517 236 logger.info "Settings cache cleared." if logger
Chris@1517 237 end
Chris@1517 238
Chris@1517 239 private
Chris@1517 240 # Returns the Setting instance for the setting named name
Chris@1517 241 # (record found in database or new record with default value)
Chris@1517 242 def self.find_or_default(name)
Chris@1517 243 name = name.to_s
Chris@1517 244 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
Chris@1517 245 setting = where(:name => name).first
Chris@1517 246 unless setting
Chris@1517 247 setting = new
Chris@1517 248 setting.name = name
Chris@1517 249 setting.value = @@available_settings[name]['default']
Chris@1517 250 end
Chris@1517 251 setting
Chris@1517 252 end
Chris@1517 253 end