##// END OF EJS Templates
Fixed setting value serialization....
Jean-Philippe Lang -
r733:31eda0fcb2d8
parent child
Show More
@@ -1,98 +1,98
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Setting < ActiveRecord::Base
19 19
20 20 cattr_accessor :available_settings
21 21 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
22 22
23 23 validates_uniqueness_of :name
24 24 validates_inclusion_of :name, :in => @@available_settings.keys
25 25 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
26 26
27 27 # Hash used to cache setting values
28 28 @cached_settings = {}
29 29 @cached_cleared_on = Time.now
30 30
31 31 def value
32 32 v = read_attribute(:value)
33 33 # Unserialize serialized settings
34 34 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
35 35 v
36 36 end
37 37
38 38 def value=(v)
39 v = v.to_yaml if @@available_settings[name]['serialized'] && v.is_a?(String)
39 v = v.to_yaml if v && @@available_settings[name]['serialized']
40 40 write_attribute(:value, v)
41 41 end
42 42
43 43 # Returns the value of the setting named name
44 44 def self.[](name)
45 45 v = @cached_settings[name]
46 46 v ? v : (@cached_settings[name] = find_or_default(name).value)
47 47 end
48 48
49 49 def self.[]=(name, v)
50 50 setting = find_or_default(name)
51 51 setting.value = (v ? v : "")
52 52 @cached_settings[name] = nil
53 53 setting.save
54 54 setting.value
55 55 end
56 56
57 57 # Defines getter and setter for each setting
58 58 # Then setting values can be read using: Setting.some_setting_name
59 59 # or set using Setting.some_setting_name = "some value"
60 60 @@available_settings.each do |name, params|
61 61 src = <<-END_SRC
62 62 def self.#{name}
63 63 self[:#{name}]
64 64 end
65 65
66 66 def self.#{name}?
67 67 self[:#{name}].to_i > 0
68 68 end
69 69
70 70 def self.#{name}=(value)
71 71 self[:#{name}] = value
72 72 end
73 73 END_SRC
74 74 class_eval src, __FILE__, __LINE__
75 75 end
76 76
77 77 # Checks if settings have changed since the values were read
78 78 # and clears the cache hash if it's the case
79 79 # Called once per request
80 80 def self.check_cache
81 81 settings_updated_on = Setting.maximum(:updated_on)
82 82 if settings_updated_on && @cached_cleared_on <= settings_updated_on
83 83 @cached_settings.clear
84 84 @cached_cleared_on = Time.now
85 85 logger.info "Settings cache cleared." if logger
86 86 end
87 87 end
88 88
89 89 private
90 90 # Returns the Setting instance for the setting named name
91 91 # (record found in database or new record with default value)
92 92 def self.find_or_default(name)
93 93 name = name.to_s
94 94 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
95 95 setting = find_by_name(name)
96 96 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
97 97 end
98 98 end
General Comments 0
You need to be logged in to leave comments. Login now