@@ -1,254 +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 |
|
|
94 | if available_settings[name]['serialized'] && v.is_a?(String) | |
|
95 | v = YAML::load(v) | |||
|
96 | v = force_utf8_strings(v) | |||
|
97 | end | |||
95 | 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? | |
96 | v |
|
99 | v | |
97 | end |
|
100 | end | |
98 |
|
101 | |||
99 | def value=(v) |
|
102 | def value=(v) | |
100 | 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'] | |
101 | write_attribute(:value, v.to_s) |
|
104 | write_attribute(:value, v.to_s) | |
102 | end |
|
105 | end | |
103 |
|
106 | |||
104 | # Returns the value of the setting named name |
|
107 | # Returns the value of the setting named name | |
105 | def self.[](name) |
|
108 | def self.[](name) | |
106 | v = @cached_settings[name] |
|
109 | v = @cached_settings[name] | |
107 | v ? v : (@cached_settings[name] = find_or_default(name).value) |
|
110 | v ? v : (@cached_settings[name] = find_or_default(name).value) | |
108 | end |
|
111 | end | |
109 |
|
112 | |||
110 | def self.[]=(name, v) |
|
113 | def self.[]=(name, v) | |
111 | setting = find_or_default(name) |
|
114 | setting = find_or_default(name) | |
112 | setting.value = (v ? v : "") |
|
115 | setting.value = (v ? v : "") | |
113 | @cached_settings[name] = nil |
|
116 | @cached_settings[name] = nil | |
114 | setting.save |
|
117 | setting.save | |
115 | setting.value |
|
118 | setting.value | |
116 | end |
|
119 | end | |
117 |
|
120 | |||
118 | # Sets a setting value from params |
|
121 | # Sets a setting value from params | |
119 | def self.set_from_params(name, params) |
|
122 | def self.set_from_params(name, params) | |
120 | params = params.dup |
|
123 | params = params.dup | |
121 | params.delete_if {|v| v.blank? } if params.is_a?(Array) |
|
124 | params.delete_if {|v| v.blank? } if params.is_a?(Array) | |
122 | params.symbolize_keys! if params.is_a?(Hash) |
|
125 | params.symbolize_keys! if params.is_a?(Hash) | |
123 |
|
126 | |||
124 | m = "#{name}_from_params" |
|
127 | m = "#{name}_from_params" | |
125 | if respond_to? m |
|
128 | if respond_to? m | |
126 | self[name.to_sym] = send m, params |
|
129 | self[name.to_sym] = send m, params | |
127 | else |
|
130 | else | |
128 | self[name.to_sym] = params |
|
131 | self[name.to_sym] = params | |
129 | end |
|
132 | end | |
130 | end |
|
133 | end | |
131 |
|
134 | |||
132 | # Returns a hash suitable for commit_update_keywords setting |
|
135 | # Returns a hash suitable for commit_update_keywords setting | |
133 | # |
|
136 | # | |
134 | # Example: |
|
137 | # Example: | |
135 | # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]} |
|
138 | # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]} | |
136 | # Setting.commit_update_keywords_from_params(params) |
|
139 | # Setting.commit_update_keywords_from_params(params) | |
137 | # # => [{'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"}] | |
138 | def self.commit_update_keywords_from_params(params) |
|
141 | def self.commit_update_keywords_from_params(params) | |
139 | s = [] |
|
142 | s = [] | |
140 | 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} | |
141 | attributes = params.except(:keywords).keys |
|
144 | attributes = params.except(:keywords).keys | |
142 | params[:keywords].each_with_index do |keywords, i| |
|
145 | params[:keywords].each_with_index do |keywords, i| | |
143 | next if keywords.blank? |
|
146 | next if keywords.blank? | |
144 | s << attributes.inject({}) {|h, a| |
|
147 | s << attributes.inject({}) {|h, a| | |
145 | value = params[a][i].to_s |
|
148 | value = params[a][i].to_s | |
146 | h[a.to_s] = value if value.present? |
|
149 | h[a.to_s] = value if value.present? | |
147 | h |
|
150 | h | |
148 | }.merge('keywords' => keywords) |
|
151 | }.merge('keywords' => keywords) | |
149 | end |
|
152 | end | |
150 | end |
|
153 | end | |
151 | s |
|
154 | s | |
152 | end |
|
155 | end | |
153 |
|
156 | |||
154 | # Helper that returns an array based on per_page_options setting |
|
157 | # Helper that returns an array based on per_page_options setting | |
155 | def self.per_page_options_array |
|
158 | def self.per_page_options_array | |
156 | 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 | |
157 | end |
|
160 | end | |
158 |
|
161 | |||
159 | # Helper that returns a Hash with single update keywords as keys |
|
162 | # Helper that returns a Hash with single update keywords as keys | |
160 | def self.commit_update_keywords_array |
|
163 | def self.commit_update_keywords_array | |
161 | a = [] |
|
164 | a = [] | |
162 | if commit_update_keywords.is_a?(Array) |
|
165 | if commit_update_keywords.is_a?(Array) | |
163 | commit_update_keywords.each do |rule| |
|
166 | commit_update_keywords.each do |rule| | |
164 | next unless rule.is_a?(Hash) |
|
167 | next unless rule.is_a?(Hash) | |
165 | rule = rule.dup |
|
168 | rule = rule.dup | |
166 | rule.delete_if {|k, v| v.blank?} |
|
169 | rule.delete_if {|k, v| v.blank?} | |
167 | keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?) |
|
170 | keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?) | |
168 | next if keywords.empty? |
|
171 | next if keywords.empty? | |
169 | a << rule.merge('keywords' => keywords) |
|
172 | a << rule.merge('keywords' => keywords) | |
170 | end |
|
173 | end | |
171 | end |
|
174 | end | |
172 | a |
|
175 | a | |
173 | end |
|
176 | end | |
174 |
|
177 | |||
175 | def self.openid? |
|
178 | def self.openid? | |
176 | Object.const_defined?(:OpenID) && self[:openid].to_i > 0 |
|
179 | Object.const_defined?(:OpenID) && self[:openid].to_i > 0 | |
177 | end |
|
180 | end | |
178 |
|
181 | |||
179 | # Checks if settings have changed since the values were read |
|
182 | # Checks if settings have changed since the values were read | |
180 | # and clears the cache hash if it's the case |
|
183 | # and clears the cache hash if it's the case | |
181 | # Called once per request |
|
184 | # Called once per request | |
182 | def self.check_cache |
|
185 | def self.check_cache | |
183 | settings_updated_on = Setting.maximum(:updated_on) |
|
186 | settings_updated_on = Setting.maximum(:updated_on) | |
184 | if settings_updated_on && @cached_cleared_on <= settings_updated_on |
|
187 | if settings_updated_on && @cached_cleared_on <= settings_updated_on | |
185 | clear_cache |
|
188 | clear_cache | |
186 | end |
|
189 | end | |
187 | end |
|
190 | end | |
188 |
|
191 | |||
189 | # Clears the settings cache |
|
192 | # Clears the settings cache | |
190 | def self.clear_cache |
|
193 | def self.clear_cache | |
191 | @cached_settings.clear |
|
194 | @cached_settings.clear | |
192 | @cached_cleared_on = Time.now |
|
195 | @cached_cleared_on = Time.now | |
193 | logger.info "Settings cache cleared." if logger |
|
196 | logger.info "Settings cache cleared." if logger | |
194 | end |
|
197 | end | |
195 |
|
198 | |||
196 | def self.define_plugin_setting(plugin) |
|
199 | def self.define_plugin_setting(plugin) | |
197 | if plugin.settings |
|
200 | if plugin.settings | |
198 | name = "plugin_#{plugin.id}" |
|
201 | name = "plugin_#{plugin.id}" | |
199 | define_setting name, {'default' => plugin.settings[:default], 'serialized' => true} |
|
202 | define_setting name, {'default' => plugin.settings[:default], 'serialized' => true} | |
200 | end |
|
203 | end | |
201 | end |
|
204 | end | |
202 |
|
205 | |||
203 | # Defines getter and setter for each setting |
|
206 | # Defines getter and setter for each setting | |
204 | # Then setting values can be read using: Setting.some_setting_name |
|
207 | # Then setting values can be read using: Setting.some_setting_name | |
205 | # or set using Setting.some_setting_name = "some value" |
|
208 | # or set using Setting.some_setting_name = "some value" | |
206 | def self.define_setting(name, options={}) |
|
209 | def self.define_setting(name, options={}) | |
207 | available_settings[name.to_s] = options |
|
210 | available_settings[name.to_s] = options | |
208 |
|
211 | |||
209 | src = <<-END_SRC |
|
212 | src = <<-END_SRC | |
210 | def self.#{name} |
|
213 | def self.#{name} | |
211 | self[:#{name}] |
|
214 | self[:#{name}] | |
212 | end |
|
215 | end | |
213 |
|
216 | |||
214 | def self.#{name}? |
|
217 | def self.#{name}? | |
215 | self[:#{name}].to_i > 0 |
|
218 | self[:#{name}].to_i > 0 | |
216 | end |
|
219 | end | |
217 |
|
220 | |||
218 | def self.#{name}=(value) |
|
221 | def self.#{name}=(value) | |
219 | self[:#{name}] = value |
|
222 | self[:#{name}] = value | |
220 | end |
|
223 | end | |
221 | END_SRC |
|
224 | END_SRC | |
222 | class_eval src, __FILE__, __LINE__ |
|
225 | class_eval src, __FILE__, __LINE__ | |
223 | end |
|
226 | end | |
224 |
|
227 | |||
225 | def self.load_available_settings |
|
228 | def self.load_available_settings | |
226 | 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| | |
227 | define_setting name, options |
|
230 | define_setting name, options | |
228 | end |
|
231 | end | |
229 | end |
|
232 | end | |
230 |
|
233 | |||
231 | def self.load_plugin_settings |
|
234 | def self.load_plugin_settings | |
232 | Redmine::Plugin.all.each do |plugin| |
|
235 | Redmine::Plugin.all.each do |plugin| | |
233 | define_plugin_setting(plugin) |
|
236 | define_plugin_setting(plugin) | |
234 | end |
|
237 | end | |
235 | end |
|
238 | end | |
236 |
|
239 | |||
237 | load_available_settings |
|
240 | load_available_settings | |
238 | load_plugin_settings |
|
241 | load_plugin_settings | |
239 |
|
242 | |||
240 | private |
|
243 | private | |
|
244 | ||||
|
245 | def force_utf8_strings(arg) | |||
|
246 | if arg.is_a?(String) | |||
|
247 | arg.dup.force_encoding('UTF-8') | |||
|
248 | elsif arg.is_a?(Array) | |||
|
249 | arg.map do |a| | |||
|
250 | force_utf8_strings(a) | |||
|
251 | end | |||
|
252 | elsif arg.is_a?(Hash) | |||
|
253 | arg = arg.dup | |||
|
254 | arg.each do |k,v| | |||
|
255 | arg[k] = force_utf8_strings(v) | |||
|
256 | end | |||
|
257 | arg | |||
|
258 | else | |||
|
259 | arg | |||
|
260 | end | |||
|
261 | end | |||
|
262 | ||||
241 | # Returns the Setting instance for the setting named name |
|
263 | # Returns the Setting instance for the setting named name | |
242 | # (record found in database or new record with default value) |
|
264 | # (record found in database or new record with default value) | |
243 | def self.find_or_default(name) |
|
265 | def self.find_or_default(name) | |
244 | name = name.to_s |
|
266 | name = name.to_s | |
245 | 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) | |
246 | setting = where(:name => name).order(:id => :desc).first |
|
268 | setting = where(:name => name).order(:id => :desc).first | |
247 | unless setting |
|
269 | unless setting | |
248 | setting = new |
|
270 | setting = new | |
249 | setting.name = name |
|
271 | setting.name = name | |
250 | setting.value = available_settings[name]['default'] |
|
272 | setting.value = available_settings[name]['default'] | |
251 | end |
|
273 | end | |
252 | setting |
|
274 | setting | |
253 | end |
|
275 | end | |
254 | end |
|
276 | end |
@@ -1,104 +1,127 | |||||
|
1 | # encoding: utf-8 | |||
|
2 | # | |||
1 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
3 | # |
|
5 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
8 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
9 | # of the License, or (at your option) any later version. | |
8 | # |
|
10 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
13 | # |
|
15 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
19 | |||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
20 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
21 | |||
20 | class SettingTest < ActiveSupport::TestCase |
|
22 | class SettingTest < ActiveSupport::TestCase | |
21 |
|
23 | |||
22 | def teardown |
|
24 | def teardown | |
23 | Setting.clear_cache |
|
25 | Setting.clear_cache | |
24 | end |
|
26 | end | |
25 |
|
27 | |||
26 | def test_read_default |
|
28 | def test_read_default | |
27 | assert_equal "Redmine", Setting.app_title |
|
29 | assert_equal "Redmine", Setting.app_title | |
28 | assert Setting.self_registration? |
|
30 | assert Setting.self_registration? | |
29 | assert !Setting.login_required? |
|
31 | assert !Setting.login_required? | |
30 | end |
|
32 | end | |
31 |
|
33 | |||
32 | def test_update |
|
34 | def test_update | |
33 | Setting.app_title = "My title" |
|
35 | Setting.app_title = "My title" | |
34 | assert_equal "My title", Setting.app_title |
|
36 | assert_equal "My title", Setting.app_title | |
35 | # make sure db has been updated (INSERT) |
|
37 | # make sure db has been updated (INSERT) | |
36 | assert_equal "My title", Setting.find_by_name('app_title').value |
|
38 | assert_equal "My title", Setting.find_by_name('app_title').value | |
37 |
|
39 | |||
38 | Setting.app_title = "My other title" |
|
40 | Setting.app_title = "My other title" | |
39 | assert_equal "My other title", Setting.app_title |
|
41 | assert_equal "My other title", Setting.app_title | |
40 | # make sure db has been updated (UPDATE) |
|
42 | # make sure db has been updated (UPDATE) | |
41 | assert_equal "My other title", Setting.find_by_name('app_title').value |
|
43 | assert_equal "My other title", Setting.find_by_name('app_title').value | |
42 | end |
|
44 | end | |
43 |
|
45 | |||
44 | def test_setting_with_int_format_should_accept_numeric_only |
|
46 | def test_setting_with_int_format_should_accept_numeric_only | |
45 | with_settings :session_timeout => 30 do |
|
47 | with_settings :session_timeout => 30 do | |
46 | Setting.session_timeout = 'foo' |
|
48 | Setting.session_timeout = 'foo' | |
47 | assert_equal "30", Setting.session_timeout |
|
49 | assert_equal "30", Setting.session_timeout | |
48 | Setting.session_timeout = 40 |
|
50 | Setting.session_timeout = 40 | |
49 | assert_equal "40", Setting.session_timeout |
|
51 | assert_equal "40", Setting.session_timeout | |
50 | end |
|
52 | end | |
51 | end |
|
53 | end | |
52 |
|
54 | |||
53 | def test_setting_with_invalid_name_should_be_valid |
|
55 | def test_setting_with_invalid_name_should_be_valid | |
54 | setting = Setting.new(:name => "does_not_exist", :value => "should_not_be_allowed") |
|
56 | setting = Setting.new(:name => "does_not_exist", :value => "should_not_be_allowed") | |
55 | assert !setting.save |
|
57 | assert !setting.save | |
56 | end |
|
58 | end | |
57 |
|
59 | |||
58 | def test_serialized_setting |
|
60 | def test_serialized_setting | |
59 | Setting.notified_events = ['issue_added', 'issue_updated', 'news_added'] |
|
61 | Setting.notified_events = ['issue_added', 'issue_updated', 'news_added'] | |
60 | assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events |
|
62 | assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events | |
61 | assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value |
|
63 | assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value | |
62 | end |
|
64 | end | |
63 |
|
65 | |||
64 | def test_setting_should_be_reloaded_after_clear_cache |
|
66 | def test_setting_should_be_reloaded_after_clear_cache | |
65 | Setting.app_title = "My title" |
|
67 | Setting.app_title = "My title" | |
66 | assert_equal "My title", Setting.app_title |
|
68 | assert_equal "My title", Setting.app_title | |
67 |
|
69 | |||
68 | s = Setting.find_by_name("app_title") |
|
70 | s = Setting.find_by_name("app_title") | |
69 | s.value = 'New title' |
|
71 | s.value = 'New title' | |
70 | s.save! |
|
72 | s.save! | |
71 | assert_equal "My title", Setting.app_title |
|
73 | assert_equal "My title", Setting.app_title | |
72 |
|
74 | |||
73 | Setting.clear_cache |
|
75 | Setting.clear_cache | |
74 | assert_equal "New title", Setting.app_title |
|
76 | assert_equal "New title", Setting.app_title | |
75 | end |
|
77 | end | |
76 |
|
78 | |||
77 | def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank |
|
79 | def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank | |
78 | with_settings :per_page_options => nil do |
|
80 | with_settings :per_page_options => nil do | |
79 | assert_equal [], Setting.per_page_options_array |
|
81 | assert_equal [], Setting.per_page_options_array | |
80 | end |
|
82 | end | |
81 |
|
83 | |||
82 | with_settings :per_page_options => '' do |
|
84 | with_settings :per_page_options => '' do | |
83 | assert_equal [], Setting.per_page_options_array |
|
85 | assert_equal [], Setting.per_page_options_array | |
84 | end |
|
86 | end | |
85 | end |
|
87 | end | |
86 |
|
88 | |||
87 | def test_per_page_options_array_should_be_an_array_of_integers |
|
89 | def test_per_page_options_array_should_be_an_array_of_integers | |
88 | with_settings :per_page_options => '10, 25, 50' do |
|
90 | with_settings :per_page_options => '10, 25, 50' do | |
89 | assert_equal [10, 25, 50], Setting.per_page_options_array |
|
91 | assert_equal [10, 25, 50], Setting.per_page_options_array | |
90 | end |
|
92 | end | |
91 | end |
|
93 | end | |
92 |
|
94 | |||
93 | def test_per_page_options_array_should_omit_non_numerial_values |
|
95 | def test_per_page_options_array_should_omit_non_numerial_values | |
94 | with_settings :per_page_options => 'a, 25, 50' do |
|
96 | with_settings :per_page_options => 'a, 25, 50' do | |
95 | assert_equal [25, 50], Setting.per_page_options_array |
|
97 | assert_equal [25, 50], Setting.per_page_options_array | |
96 | end |
|
98 | end | |
97 | end |
|
99 | end | |
98 |
|
100 | |||
99 | def test_per_page_options_array_should_be_sorted |
|
101 | def test_per_page_options_array_should_be_sorted | |
100 | with_settings :per_page_options => '25, 10, 50' do |
|
102 | with_settings :per_page_options => '25, 10, 50' do | |
101 | assert_equal [10, 25, 50], Setting.per_page_options_array |
|
103 | assert_equal [10, 25, 50], Setting.per_page_options_array | |
102 | end |
|
104 | end | |
103 | end |
|
105 | end | |
|
106 | ||||
|
107 | def test_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings | |||
|
108 | yaml = <<-YAML | |||
|
109 | --- | |||
|
110 | - keywords: !binary | | |||
|
111 | Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ | |||
|
112 | vizRgdC00LXQu9Cw0L3QvixmaXhlZA== | |||
|
113 | ||||
|
114 | done_ratio: "100" | |||
|
115 | status_id: "5" | |||
|
116 | YAML | |||
|
117 | ||||
|
118 | Setting.commit_update_keywords = {} | |||
|
119 | assert_equal 1, Setting.where(:name => 'commit_update_keywords').update_all(:value => yaml) | |||
|
120 | Setting.clear_cache | |||
|
121 | ||||
|
122 | assert_equal 'UTF-8', Setting.commit_update_keywords.first['keywords'].encoding.name | |||
|
123 | ensure | |||
|
124 | Setting.where(:name => 'commit_update_keywords').delete_all | |||
|
125 | Setting.clear_cache | |||
|
126 | end | |||
104 | end |
|
127 | end |
General Comments 0
You need to be logged in to leave comments.
Login now