@@ -1,252 +1,253 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 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. | |
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 | @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml")) |
|
78 | @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml")) | |
79 | Redmine::Plugin.all.each do |plugin| |
|
79 | Redmine::Plugin.all.each do |plugin| | |
80 | next unless plugin.settings |
|
80 | next unless plugin.settings | |
81 | @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} |
|
81 | @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | validates_uniqueness_of :name |
|
84 | validates_uniqueness_of :name | |
85 | validates_inclusion_of :name, :in => @@available_settings.keys |
|
85 | validates_inclusion_of :name, :in => @@available_settings.keys | |
86 | validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| |
|
86 | validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| | |
87 | (s = @@available_settings[setting.name]) && s['format'] == 'int' |
|
87 | (s = @@available_settings[setting.name]) && s['format'] == 'int' | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | # Hash used to cache setting values |
|
90 | # Hash used to cache setting values | |
91 | @cached_settings = {} |
|
91 | @cached_settings = {} | |
92 | @cached_cleared_on = Time.now |
|
92 | @cached_cleared_on = Time.now | |
93 |
|
93 | |||
94 | def value |
|
94 | def value | |
95 | v = read_attribute(:value) |
|
95 | v = read_attribute(:value) | |
96 | # Unserialize serialized settings |
|
96 | # Unserialize serialized settings | |
97 | v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) |
|
97 | v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) | |
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 | # Defines getter and setter for each setting |
|
121 | # Defines getter and setter for each setting | |
122 | # Then setting values can be read using: Setting.some_setting_name |
|
122 | # Then setting values can be read using: Setting.some_setting_name | |
123 | # or set using Setting.some_setting_name = "some value" |
|
123 | # or set using Setting.some_setting_name = "some value" | |
124 | @@available_settings.each do |name, params| |
|
124 | @@available_settings.each do |name, params| | |
125 | src = <<-END_SRC |
|
125 | src = <<-END_SRC | |
126 | def self.#{name} |
|
126 | def self.#{name} | |
127 | self[:#{name}] |
|
127 | self[:#{name}] | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | def self.#{name}? |
|
130 | def self.#{name}? | |
131 | self[:#{name}].to_i > 0 |
|
131 | self[:#{name}].to_i > 0 | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def self.#{name}=(value) |
|
134 | def self.#{name}=(value) | |
135 | self[:#{name}] = value |
|
135 | self[:#{name}] = value | |
136 | end |
|
136 | end | |
137 | END_SRC |
|
137 | END_SRC | |
138 | class_eval src, __FILE__, __LINE__ |
|
138 | class_eval src, __FILE__, __LINE__ | |
139 | end |
|
139 | end | |
140 |
|
140 | |||
141 | # Sets a setting value from params |
|
141 | # Sets a setting value from params | |
142 | def self.set_from_params(name, params) |
|
142 | def self.set_from_params(name, params) | |
143 | params = params.dup |
|
143 | params = params.dup | |
144 | params.delete_if {|v| v.blank? } if params.is_a?(Array) |
|
144 | params.delete_if {|v| v.blank? } if params.is_a?(Array) | |
145 |
|
145 | |||
146 | m = "#{name}_from_params" |
|
146 | m = "#{name}_from_params" | |
147 | if respond_to? m |
|
147 | if respond_to? m | |
148 | self[name.to_sym] = send m, params |
|
148 | self[name.to_sym] = send m, params | |
149 | else |
|
149 | else | |
150 | self[name.to_sym] = params |
|
150 | self[name.to_sym] = params | |
151 | end |
|
151 | end | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | # Returns a hash suitable for commit_update_keywords setting |
|
154 | # Returns a hash suitable for commit_update_keywords setting | |
155 | # |
|
155 | # | |
156 | # Example: |
|
156 | # Example: | |
157 | # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]} |
|
157 | # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]} | |
158 | # Setting.commit_update_keywords_from_params(params) |
|
158 | # Setting.commit_update_keywords_from_params(params) | |
159 | # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}] |
|
159 | # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}] | |
160 | def self.commit_update_keywords_from_params(params) |
|
160 | def self.commit_update_keywords_from_params(params) | |
161 | s = [] |
|
161 | s = [] | |
162 | if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array} |
|
162 | if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array} | |
163 | attributes = params.except(:keywords).keys |
|
163 | attributes = params.except(:keywords).keys | |
164 | params[:keywords].each_with_index do |keywords, i| |
|
164 | params[:keywords].each_with_index do |keywords, i| | |
165 | next if keywords.blank? |
|
165 | next if keywords.blank? | |
166 | s << attributes.inject({}) {|h, a| |
|
166 | s << attributes.inject({}) {|h, a| | |
167 | value = params[a][i].to_s |
|
167 | value = params[a][i].to_s | |
168 | h[a.to_s] = value if value.present? |
|
168 | h[a.to_s] = value if value.present? | |
169 | h |
|
169 | h | |
170 | }.merge('keywords' => keywords) |
|
170 | }.merge('keywords' => keywords) | |
171 | end |
|
171 | end | |
172 | end |
|
172 | end | |
173 | s |
|
173 | s | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | # Helper that returns an array based on per_page_options setting |
|
176 | # Helper that returns an array based on per_page_options setting | |
177 | def self.per_page_options_array |
|
177 | def self.per_page_options_array | |
178 | per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort |
|
178 | per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort | |
179 | end |
|
179 | end | |
180 |
|
180 | |||
181 | # Helper that returns a Hash with single update keywords as keys |
|
181 | # Helper that returns a Hash with single update keywords as keys | |
182 | def self.commit_update_keywords_array |
|
182 | def self.commit_update_keywords_array | |
183 | a = [] |
|
183 | a = [] | |
184 | if commit_update_keywords.is_a?(Array) |
|
184 | if commit_update_keywords.is_a?(Array) | |
185 | commit_update_keywords.each do |rule| |
|
185 | commit_update_keywords.each do |rule| | |
186 | next unless rule.is_a?(Hash) |
|
186 | next unless rule.is_a?(Hash) | |
187 | rule = rule.dup |
|
187 | rule = rule.dup | |
188 | rule.delete_if {|k, v| v.blank?} |
|
188 | rule.delete_if {|k, v| v.blank?} | |
189 | keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?) |
|
189 | keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?) | |
190 | next if keywords.empty? |
|
190 | next if keywords.empty? | |
191 | a << rule.merge('keywords' => keywords) |
|
191 | a << rule.merge('keywords' => keywords) | |
192 | end |
|
192 | end | |
193 | end |
|
193 | end | |
194 | a |
|
194 | a | |
195 | end |
|
195 | end | |
196 |
|
196 | |||
197 | def self.commit_fix_keywords |
|
197 | def self.commit_fix_keywords | |
198 | ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3" |
|
198 | ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3" | |
199 | if commit_update_keywords.is_a?(Array) |
|
199 | if commit_update_keywords.is_a?(Array) | |
200 | commit_update_keywords.first && commit_update_keywords.first['keywords'] |
|
200 | commit_update_keywords.first && commit_update_keywords.first['keywords'] | |
201 | end |
|
201 | end | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | def self.commit_fix_status_id |
|
204 | def self.commit_fix_status_id | |
205 | ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3" |
|
205 | ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3" | |
206 | if commit_update_keywords.is_a?(Array) |
|
206 | if commit_update_keywords.is_a?(Array) | |
207 | commit_update_keywords.first && commit_update_keywords.first['status_id'] |
|
207 | commit_update_keywords.first && commit_update_keywords.first['status_id'] | |
208 | end |
|
208 | end | |
209 | end |
|
209 | end | |
210 |
|
210 | |||
211 | def self.commit_fix_done_ratio |
|
211 | def self.commit_fix_done_ratio | |
212 | ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3" |
|
212 | ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3" | |
213 | if commit_update_keywords.is_a?(Array) |
|
213 | if commit_update_keywords.is_a?(Array) | |
214 | commit_update_keywords.first && commit_update_keywords.first['done_ratio'] |
|
214 | commit_update_keywords.first && commit_update_keywords.first['done_ratio'] | |
215 | end |
|
215 | end | |
216 | end |
|
216 | end | |
217 |
|
217 | |||
218 | def self.openid? |
|
218 | def self.openid? | |
219 | Object.const_defined?(:OpenID) && self[:openid].to_i > 0 |
|
219 | Object.const_defined?(:OpenID) && self[:openid].to_i > 0 | |
220 | end |
|
220 | end | |
221 |
|
221 | |||
222 | # Checks if settings have changed since the values were read |
|
222 | # Checks if settings have changed since the values were read | |
223 | # and clears the cache hash if it's the case |
|
223 | # and clears the cache hash if it's the case | |
224 | # Called once per request |
|
224 | # Called once per request | |
225 | def self.check_cache |
|
225 | def self.check_cache | |
226 | settings_updated_on = Setting.maximum(:updated_on) |
|
226 | settings_updated_on = Setting.maximum(:updated_on) | |
227 | if settings_updated_on && @cached_cleared_on <= settings_updated_on |
|
227 | if settings_updated_on && @cached_cleared_on <= settings_updated_on | |
228 | clear_cache |
|
228 | clear_cache | |
229 | end |
|
229 | end | |
230 | end |
|
230 | end | |
231 |
|
231 | |||
232 | # Clears the settings cache |
|
232 | # Clears the settings cache | |
233 | def self.clear_cache |
|
233 | def self.clear_cache | |
234 | @cached_settings.clear |
|
234 | @cached_settings.clear | |
235 | @cached_cleared_on = Time.now |
|
235 | @cached_cleared_on = Time.now | |
236 | logger.info "Settings cache cleared." if logger |
|
236 | logger.info "Settings cache cleared." if logger | |
237 | end |
|
237 | end | |
238 |
|
238 | |||
239 | private |
|
239 | private | |
240 | # Returns the Setting instance for the setting named name |
|
240 | # Returns the Setting instance for the setting named name | |
241 | # (record found in database or new record with default value) |
|
241 | # (record found in database or new record with default value) | |
242 | def self.find_or_default(name) |
|
242 | def self.find_or_default(name) | |
243 | name = name.to_s |
|
243 | name = name.to_s | |
244 | raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) |
|
244 | raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) | |
245 |
setting = |
|
245 | setting = where(:name => name).first | |
246 | unless setting |
|
246 | unless setting | |
247 |
setting = new |
|
247 | setting = new | |
|
248 | setting.name = name | |||
248 | setting.value = @@available_settings[name]['default'] |
|
249 | setting.value = @@available_settings[name]['default'] | |
249 | end |
|
250 | end | |
250 | setting |
|
251 | setting | |
251 | end |
|
252 | end | |
252 | end |
|
253 | end |
General Comments 0
You need to be logged in to leave comments.
Login now