comparison app/models/setting.rb @ 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 433d4f72a19b
children e248c7af89ec
comparison
equal deleted inserted replaced
1296:038ba2d95de8 1464:261b3d9a4903
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 class Setting < ActiveRecord::Base 18 class Setting < ActiveRecord::Base
19 19
20 DATE_FORMATS = [ 20 DATE_FORMATS = [
21 '%Y-%m-%d', 21 '%Y-%m-%d',
22 '%d/%m/%Y', 22 '%d/%m/%Y',
23 '%d.%m.%Y', 23 '%d.%m.%Y',
24 '%d-%m-%Y', 24 '%d-%m-%Y',
25 '%m/%d/%Y', 25 '%m/%d/%Y',
26 '%d %b %Y', 26 '%d %b %Y',
27 '%d %B %Y', 27 '%d %B %Y',
28 '%b %d, %Y', 28 '%b %d, %Y',
29 '%B %d, %Y' 29 '%B %d, %Y'
30 ] 30 ]
31 31
32 TIME_FORMATS = [ 32 TIME_FORMATS = [
33 '%H:%M', 33 '%H:%M',
34 '%I:%M %p' 34 '%I:%M %p'
130 end 130 end
131 131
132 def self.#{name}=(value) 132 def self.#{name}=(value)
133 self[:#{name}] = value 133 self[:#{name}] = value
134 end 134 end
135 END_SRC 135 END_SRC
136 class_eval src, __FILE__, __LINE__ 136 class_eval src, __FILE__, __LINE__
137 end
138
139 # Sets a setting value from params
140 def self.set_from_params(name, params)
141 params = params.dup
142 params.delete_if {|v| v.blank? } if params.is_a?(Array)
143
144 m = "#{name}_from_params"
145 if respond_to? m
146 self[name.to_sym] = send m, params
147 else
148 self[name.to_sym] = params
149 end
150 end
151
152 # Returns a hash suitable for commit_update_keywords setting
153 #
154 # Example:
155 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
156 # Setting.commit_update_keywords_from_params(params)
157 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
158 def self.commit_update_keywords_from_params(params)
159 s = []
160 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
161 attributes = params.except(:keywords).keys
162 params[:keywords].each_with_index do |keywords, i|
163 next if keywords.blank?
164 s << attributes.inject({}) {|h, a|
165 value = params[a][i].to_s
166 h[a.to_s] = value if value.present?
167 h
168 }.merge('keywords' => keywords)
169 end
170 end
171 s
137 end 172 end
138 173
139 # Helper that returns an array based on per_page_options setting 174 # Helper that returns an array based on per_page_options setting
140 def self.per_page_options_array 175 def self.per_page_options_array
141 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort 176 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
177 end
178
179 # Helper that returns a Hash with single update keywords as keys
180 def self.commit_update_keywords_array
181 a = []
182 if commit_update_keywords.is_a?(Array)
183 commit_update_keywords.each do |rule|
184 next unless rule.is_a?(Hash)
185 rule = rule.dup
186 rule.delete_if {|k, v| v.blank?}
187 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
188 next if keywords.empty?
189 a << rule.merge('keywords' => keywords)
190 end
191 end
192 a
193 end
194
195 def self.commit_fix_keywords
196 ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
197 if commit_update_keywords.is_a?(Array)
198 commit_update_keywords.first && commit_update_keywords.first['keywords']
199 end
200 end
201
202 def self.commit_fix_status_id
203 ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
204 if commit_update_keywords.is_a?(Array)
205 commit_update_keywords.first && commit_update_keywords.first['status_id']
206 end
207 end
208
209 def self.commit_fix_done_ratio
210 ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
211 if commit_update_keywords.is_a?(Array)
212 commit_update_keywords.first && commit_update_keywords.first['done_ratio']
213 end
142 end 214 end
143 215
144 def self.openid? 216 def self.openid?
145 Object.const_defined?(:OpenID) && self[:openid].to_i > 0 217 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
146 end 218 end
152 settings_updated_on = Setting.maximum(:updated_on) 224 settings_updated_on = Setting.maximum(:updated_on)
153 if settings_updated_on && @cached_cleared_on <= settings_updated_on 225 if settings_updated_on && @cached_cleared_on <= settings_updated_on
154 clear_cache 226 clear_cache
155 end 227 end
156 end 228 end
157 229
158 # Clears the settings cache 230 # Clears the settings cache
159 def self.clear_cache 231 def self.clear_cache
160 @cached_settings.clear 232 @cached_settings.clear
161 @cached_cleared_on = Time.now 233 @cached_cleared_on = Time.now
162 logger.info "Settings cache cleared." if logger 234 logger.info "Settings cache cleared." if logger