##// END OF EJS Templates
Compatibility with ruby1.8 (#19305)....
Jean-Philippe Lang -
r13733:3f46dfdee9f9
parent child
Show More
@@ -1,276 +1,276
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
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'
35 ]
35 ]
36
36
37 ENCODINGS = %w(US-ASCII
37 ENCODINGS = %w(US-ASCII
38 windows-1250
38 windows-1250
39 windows-1251
39 windows-1251
40 windows-1252
40 windows-1252
41 windows-1253
41 windows-1253
42 windows-1254
42 windows-1254
43 windows-1255
43 windows-1255
44 windows-1256
44 windows-1256
45 windows-1257
45 windows-1257
46 windows-1258
46 windows-1258
47 windows-31j
47 windows-31j
48 ISO-2022-JP
48 ISO-2022-JP
49 ISO-2022-KR
49 ISO-2022-KR
50 ISO-8859-1
50 ISO-8859-1
51 ISO-8859-2
51 ISO-8859-2
52 ISO-8859-3
52 ISO-8859-3
53 ISO-8859-4
53 ISO-8859-4
54 ISO-8859-5
54 ISO-8859-5
55 ISO-8859-6
55 ISO-8859-6
56 ISO-8859-7
56 ISO-8859-7
57 ISO-8859-8
57 ISO-8859-8
58 ISO-8859-9
58 ISO-8859-9
59 ISO-8859-13
59 ISO-8859-13
60 ISO-8859-15
60 ISO-8859-15
61 KOI8-R
61 KOI8-R
62 UTF-8
62 UTF-8
63 UTF-16
63 UTF-16
64 UTF-16BE
64 UTF-16BE
65 UTF-16LE
65 UTF-16LE
66 EUC-JP
66 EUC-JP
67 Shift_JIS
67 Shift_JIS
68 CP932
68 CP932
69 GB18030
69 GB18030
70 GBK
70 GBK
71 ISCII91
71 ISCII91
72 EUC-KR
72 EUC-KR
73 Big5
73 Big5
74 Big5-HKSCS
74 Big5-HKSCS
75 TIS-620)
75 TIS-620)
76
76
77 cattr_accessor :available_settings
77 cattr_accessor :available_settings
78 self.available_settings ||= {}
78 self.available_settings ||= {}
79
79
80 validates_uniqueness_of :name, :if => Proc.new {|setting| setting.new_record? || setting.name_changed?}
80 validates_uniqueness_of :name, :if => Proc.new {|setting| setting.new_record? || setting.name_changed?}
81 validates_inclusion_of :name, :in => Proc.new {available_settings.keys}
81 validates_inclusion_of :name, :in => Proc.new {available_settings.keys}
82 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
82 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
83 (s = available_settings[setting.name]) && s['format'] == 'int'
83 (s = available_settings[setting.name]) && s['format'] == 'int'
84 }
84 }
85 attr_protected :id
85 attr_protected :id
86
86
87 # Hash used to cache setting values
87 # Hash used to cache setting values
88 @cached_settings = {}
88 @cached_settings = {}
89 @cached_cleared_on = Time.now
89 @cached_cleared_on = Time.now
90
90
91 def value
91 def value
92 v = read_attribute(:value)
92 v = read_attribute(:value)
93 # Unserialize serialized settings
93 # Unserialize serialized settings
94 if available_settings[name]['serialized'] && v.is_a?(String)
94 if available_settings[name]['serialized'] && v.is_a?(String)
95 v = YAML::load(v)
95 v = YAML::load(v)
96 v = force_utf8_strings(v)
96 v = force_utf8_strings(v)
97 end
97 end
98 v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
98 v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
99 v
99 v
100 end
100 end
101
101
102 def value=(v)
102 def value=(v)
103 v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized']
103 v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized']
104 write_attribute(:value, v.to_s)
104 write_attribute(:value, v.to_s)
105 end
105 end
106
106
107 # Returns the value of the setting named name
107 # Returns the value of the setting named name
108 def self.[](name)
108 def self.[](name)
109 v = @cached_settings[name]
109 v = @cached_settings[name]
110 v ? v : (@cached_settings[name] = find_or_default(name).value)
110 v ? v : (@cached_settings[name] = find_or_default(name).value)
111 end
111 end
112
112
113 def self.[]=(name, v)
113 def self.[]=(name, v)
114 setting = find_or_default(name)
114 setting = find_or_default(name)
115 setting.value = (v ? v : "")
115 setting.value = (v ? v : "")
116 @cached_settings[name] = nil
116 @cached_settings[name] = nil
117 setting.save
117 setting.save
118 setting.value
118 setting.value
119 end
119 end
120
120
121 # Sets a setting value from params
121 # Sets a setting value from params
122 def self.set_from_params(name, params)
122 def self.set_from_params(name, params)
123 params = params.dup
123 params = params.dup
124 params.delete_if {|v| v.blank? } if params.is_a?(Array)
124 params.delete_if {|v| v.blank? } if params.is_a?(Array)
125 params.symbolize_keys! if params.is_a?(Hash)
125 params.symbolize_keys! if params.is_a?(Hash)
126
126
127 m = "#{name}_from_params"
127 m = "#{name}_from_params"
128 if respond_to? m
128 if respond_to? m
129 self[name.to_sym] = send m, params
129 self[name.to_sym] = send m, params
130 else
130 else
131 self[name.to_sym] = params
131 self[name.to_sym] = params
132 end
132 end
133 end
133 end
134
134
135 # Returns a hash suitable for commit_update_keywords setting
135 # Returns a hash suitable for commit_update_keywords setting
136 #
136 #
137 # Example:
137 # Example:
138 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
138 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
139 # Setting.commit_update_keywords_from_params(params)
139 # Setting.commit_update_keywords_from_params(params)
140 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
140 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
141 def self.commit_update_keywords_from_params(params)
141 def self.commit_update_keywords_from_params(params)
142 s = []
142 s = []
143 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
143 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
144 attributes = params.except(:keywords).keys
144 attributes = params.except(:keywords).keys
145 params[:keywords].each_with_index do |keywords, i|
145 params[:keywords].each_with_index do |keywords, i|
146 next if keywords.blank?
146 next if keywords.blank?
147 s << attributes.inject({}) {|h, a|
147 s << attributes.inject({}) {|h, a|
148 value = params[a][i].to_s
148 value = params[a][i].to_s
149 h[a.to_s] = value if value.present?
149 h[a.to_s] = value if value.present?
150 h
150 h
151 }.merge('keywords' => keywords)
151 }.merge('keywords' => keywords)
152 end
152 end
153 end
153 end
154 s
154 s
155 end
155 end
156
156
157 # Helper that returns an array based on per_page_options setting
157 # Helper that returns an array based on per_page_options setting
158 def self.per_page_options_array
158 def self.per_page_options_array
159 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
159 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
160 end
160 end
161
161
162 # Helper that returns a Hash with single update keywords as keys
162 # Helper that returns a Hash with single update keywords as keys
163 def self.commit_update_keywords_array
163 def self.commit_update_keywords_array
164 a = []
164 a = []
165 if commit_update_keywords.is_a?(Array)
165 if commit_update_keywords.is_a?(Array)
166 commit_update_keywords.each do |rule|
166 commit_update_keywords.each do |rule|
167 next unless rule.is_a?(Hash)
167 next unless rule.is_a?(Hash)
168 rule = rule.dup
168 rule = rule.dup
169 rule.delete_if {|k, v| v.blank?}
169 rule.delete_if {|k, v| v.blank?}
170 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
170 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
171 next if keywords.empty?
171 next if keywords.empty?
172 a << rule.merge('keywords' => keywords)
172 a << rule.merge('keywords' => keywords)
173 end
173 end
174 end
174 end
175 a
175 a
176 end
176 end
177
177
178 def self.openid?
178 def self.openid?
179 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
179 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
180 end
180 end
181
181
182 # Checks if settings have changed since the values were read
182 # Checks if settings have changed since the values were read
183 # and clears the cache hash if it's the case
183 # and clears the cache hash if it's the case
184 # Called once per request
184 # Called once per request
185 def self.check_cache
185 def self.check_cache
186 settings_updated_on = Setting.maximum(:updated_on)
186 settings_updated_on = Setting.maximum(:updated_on)
187 if settings_updated_on && @cached_cleared_on <= settings_updated_on
187 if settings_updated_on && @cached_cleared_on <= settings_updated_on
188 clear_cache
188 clear_cache
189 end
189 end
190 end
190 end
191
191
192 # Clears the settings cache
192 # Clears the settings cache
193 def self.clear_cache
193 def self.clear_cache
194 @cached_settings.clear
194 @cached_settings.clear
195 @cached_cleared_on = Time.now
195 @cached_cleared_on = Time.now
196 logger.info "Settings cache cleared." if logger
196 logger.info "Settings cache cleared." if logger
197 end
197 end
198
198
199 def self.define_plugin_setting(plugin)
199 def self.define_plugin_setting(plugin)
200 if plugin.settings
200 if plugin.settings
201 name = "plugin_#{plugin.id}"
201 name = "plugin_#{plugin.id}"
202 define_setting name, {'default' => plugin.settings[:default], 'serialized' => true}
202 define_setting name, {'default' => plugin.settings[:default], 'serialized' => true}
203 end
203 end
204 end
204 end
205
205
206 # Defines getter and setter for each setting
206 # Defines getter and setter for each setting
207 # Then setting values can be read using: Setting.some_setting_name
207 # Then setting values can be read using: Setting.some_setting_name
208 # or set using Setting.some_setting_name = "some value"
208 # or set using Setting.some_setting_name = "some value"
209 def self.define_setting(name, options={})
209 def self.define_setting(name, options={})
210 available_settings[name.to_s] = options
210 available_settings[name.to_s] = options
211
211
212 src = <<-END_SRC
212 src = <<-END_SRC
213 def self.#{name}
213 def self.#{name}
214 self[:#{name}]
214 self[:#{name}]
215 end
215 end
216
216
217 def self.#{name}?
217 def self.#{name}?
218 self[:#{name}].to_i > 0
218 self[:#{name}].to_i > 0
219 end
219 end
220
220
221 def self.#{name}=(value)
221 def self.#{name}=(value)
222 self[:#{name}] = value
222 self[:#{name}] = value
223 end
223 end
224 END_SRC
224 END_SRC
225 class_eval src, __FILE__, __LINE__
225 class_eval src, __FILE__, __LINE__
226 end
226 end
227
227
228 def self.load_available_settings
228 def self.load_available_settings
229 YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
229 YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
230 define_setting name, options
230 define_setting name, options
231 end
231 end
232 end
232 end
233
233
234 def self.load_plugin_settings
234 def self.load_plugin_settings
235 Redmine::Plugin.all.each do |plugin|
235 Redmine::Plugin.all.each do |plugin|
236 define_plugin_setting(plugin)
236 define_plugin_setting(plugin)
237 end
237 end
238 end
238 end
239
239
240 load_available_settings
240 load_available_settings
241 load_plugin_settings
241 load_plugin_settings
242
242
243 private
243 private
244
244
245 def force_utf8_strings(arg)
245 def force_utf8_strings(arg)
246 if arg.is_a?(String)
246 if arg.is_a?(String) && arg.respond_to?(:force_encoding)
247 arg.dup.force_encoding('UTF-8')
247 arg.dup.force_encoding('UTF-8')
248 elsif arg.is_a?(Array)
248 elsif arg.is_a?(Array)
249 arg.map do |a|
249 arg.map do |a|
250 force_utf8_strings(a)
250 force_utf8_strings(a)
251 end
251 end
252 elsif arg.is_a?(Hash)
252 elsif arg.is_a?(Hash)
253 arg = arg.dup
253 arg = arg.dup
254 arg.each do |k,v|
254 arg.each do |k,v|
255 arg[k] = force_utf8_strings(v)
255 arg[k] = force_utf8_strings(v)
256 end
256 end
257 arg
257 arg
258 else
258 else
259 arg
259 arg
260 end
260 end
261 end
261 end
262
262
263 # Returns the Setting instance for the setting named name
263 # Returns the Setting instance for the setting named name
264 # (record found in database or new record with default value)
264 # (record found in database or new record with default value)
265 def self.find_or_default(name)
265 def self.find_or_default(name)
266 name = name.to_s
266 name = name.to_s
267 raise "There's no setting named #{name}" unless available_settings.has_key?(name)
267 raise "There's no setting named #{name}" unless available_settings.has_key?(name)
268 setting = where(:name => name).order(:id => :desc).first
268 setting = where(:name => name).order(:id => :desc).first
269 unless setting
269 unless setting
270 setting = new
270 setting = new
271 setting.name = name
271 setting.name = name
272 setting.value = available_settings[name]['default']
272 setting.value = available_settings[name]['default']
273 end
273 end
274 setting
274 setting
275 end
275 end
276 end
276 end
General Comments 0
You need to be logged in to leave comments. Login now