Mercurial > hg > soundsoftware-site
comparison app/models/setting.rb @ 514:7eba09d624db live
Merge
author | Chris Cannam |
---|---|
date | Thu, 14 Jul 2011 10:50:53 +0100 |
parents | cbce1fd3b1b7 |
children | cbb26bc654de |
comparison
equal
deleted
inserted
replaced
512:b9aebdd7dd40 | 514:7eba09d624db |
---|---|
1 # redMine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2007 Jean-Philippe Lang | 2 # Copyright (C) 2006-2011 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 |
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 |
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 GB18030 | 69 GB18030 |
69 GBK | 70 GBK |
70 ISCII91 | 71 ISCII91 |
71 EUC-KR | 72 EUC-KR |
72 Big5 | 73 Big5 |
73 Big5-HKSCS | 74 Big5-HKSCS |
74 TIS-620) | 75 TIS-620) |
75 | 76 |
76 cattr_accessor :available_settings | 77 cattr_accessor :available_settings |
77 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml")) | 78 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml")) |
78 Redmine::Plugin.all.each do |plugin| | 79 Redmine::Plugin.all.each do |plugin| |
79 next unless plugin.settings | 80 next unless plugin.settings |
80 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} | 81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} |
81 end | 82 end |
82 | 83 |
83 validates_uniqueness_of :name | 84 validates_uniqueness_of :name |
84 validates_inclusion_of :name, :in => @@available_settings.keys | 85 validates_inclusion_of :name, :in => @@available_settings.keys |
85 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } | 86 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } |
86 | 87 |
87 # Hash used to cache setting values | 88 # Hash used to cache setting values |
88 @cached_settings = {} | 89 @cached_settings = {} |
89 @cached_cleared_on = Time.now | 90 @cached_cleared_on = Time.now |
90 | 91 |
91 def value | 92 def value |
92 v = read_attribute(:value) | 93 v = read_attribute(:value) |
93 # Unserialize serialized settings | 94 # Unserialize serialized settings |
94 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) | 95 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? | 96 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank? |
96 v | 97 v |
97 end | 98 end |
98 | 99 |
99 def value=(v) | 100 def value=(v) |
100 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] | 101 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] |
101 write_attribute(:value, v.to_s) | 102 write_attribute(:value, v.to_s) |
102 end | 103 end |
103 | 104 |
104 # Returns the value of the setting named name | 105 # Returns the value of the setting named name |
105 def self.[](name) | 106 def self.[](name) |
106 v = @cached_settings[name] | 107 v = @cached_settings[name] |
107 v ? v : (@cached_settings[name] = find_or_default(name).value) | 108 v ? v : (@cached_settings[name] = find_or_default(name).value) |
108 end | 109 end |
109 | 110 |
110 def self.[]=(name, v) | 111 def self.[]=(name, v) |
111 setting = find_or_default(name) | 112 setting = find_or_default(name) |
112 setting.value = (v ? v : "") | 113 setting.value = (v ? v : "") |
113 @cached_settings[name] = nil | 114 @cached_settings[name] = nil |
114 setting.save | 115 setting.save |
115 setting.value | 116 setting.value |
116 end | 117 end |
117 | 118 |
118 # Defines getter and setter for each setting | 119 # Defines getter and setter for each setting |
119 # Then setting values can be read using: Setting.some_setting_name | 120 # Then setting values can be read using: Setting.some_setting_name |
120 # or set using Setting.some_setting_name = "some value" | 121 # or set using Setting.some_setting_name = "some value" |
121 @@available_settings.each do |name, params| | 122 @@available_settings.each do |name, params| |
122 src = <<-END_SRC | 123 src = <<-END_SRC |
132 self[:#{name}] = value | 133 self[:#{name}] = value |
133 end | 134 end |
134 END_SRC | 135 END_SRC |
135 class_eval src, __FILE__, __LINE__ | 136 class_eval src, __FILE__, __LINE__ |
136 end | 137 end |
137 | 138 |
138 # Helper that returns an array based on per_page_options setting | 139 # Helper that returns an array based on per_page_options setting |
139 def self.per_page_options_array | 140 def self.per_page_options_array |
140 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort | 141 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort |
141 end | 142 end |
142 | 143 |
143 def self.openid? | 144 def self.openid? |
144 Object.const_defined?(:OpenID) && self[:openid].to_i > 0 | 145 Object.const_defined?(:OpenID) && self[:openid].to_i > 0 |
145 end | 146 end |
146 | 147 |
147 # Checks if settings have changed since the values were read | 148 # Checks if settings have changed since the values were read |
148 # and clears the cache hash if it's the case | 149 # and clears the cache hash if it's the case |
149 # Called once per request | 150 # Called once per request |
150 def self.check_cache | 151 def self.check_cache |
151 settings_updated_on = Setting.maximum(:updated_on) | 152 settings_updated_on = Setting.maximum(:updated_on) |
153 @cached_settings.clear | 154 @cached_settings.clear |
154 @cached_cleared_on = Time.now | 155 @cached_cleared_on = Time.now |
155 logger.info "Settings cache cleared." if logger | 156 logger.info "Settings cache cleared." if logger |
156 end | 157 end |
157 end | 158 end |
158 | 159 |
159 private | 160 private |
160 # Returns the Setting instance for the setting named name | 161 # Returns the Setting instance for the setting named name |
161 # (record found in database or new record with default value) | 162 # (record found in database or new record with default value) |
162 def self.find_or_default(name) | 163 def self.find_or_default(name) |
163 name = name.to_s | 164 name = name.to_s |
164 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) | 165 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) |
165 setting = find_by_name(name) | 166 setting = find_by_name(name) |
166 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name | 167 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name |
167 end | 168 end |
168 end | 169 end |