annotate .svn/pristine/71/71c5cddb30e880c05eb4294be088fd4484820a38.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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