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