##// END OF EJS Templates
Fixed setting value serialization....
Jean-Philippe Lang -
r733:31eda0fcb2d8
parent child
Show More
@@ -1,98 +1,98
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 cattr_accessor :available_settings
20 cattr_accessor :available_settings
21 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
21 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
22
22
23 validates_uniqueness_of :name
23 validates_uniqueness_of :name
24 validates_inclusion_of :name, :in => @@available_settings.keys
24 validates_inclusion_of :name, :in => @@available_settings.keys
25 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
25 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
26
26
27 # Hash used to cache setting values
27 # Hash used to cache setting values
28 @cached_settings = {}
28 @cached_settings = {}
29 @cached_cleared_on = Time.now
29 @cached_cleared_on = Time.now
30
30
31 def value
31 def value
32 v = read_attribute(:value)
32 v = read_attribute(:value)
33 # Unserialize serialized settings
33 # Unserialize serialized settings
34 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
34 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
35 v
35 v
36 end
36 end
37
37
38 def value=(v)
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 write_attribute(:value, v)
40 write_attribute(:value, v)
41 end
41 end
42
42
43 # Returns the value of the setting named name
43 # Returns the value of the setting named name
44 def self.[](name)
44 def self.[](name)
45 v = @cached_settings[name]
45 v = @cached_settings[name]
46 v ? v : (@cached_settings[name] = find_or_default(name).value)
46 v ? v : (@cached_settings[name] = find_or_default(name).value)
47 end
47 end
48
48
49 def self.[]=(name, v)
49 def self.[]=(name, v)
50 setting = find_or_default(name)
50 setting = find_or_default(name)
51 setting.value = (v ? v : "")
51 setting.value = (v ? v : "")
52 @cached_settings[name] = nil
52 @cached_settings[name] = nil
53 setting.save
53 setting.save
54 setting.value
54 setting.value
55 end
55 end
56
56
57 # Defines getter and setter for each setting
57 # Defines getter and setter for each setting
58 # Then setting values can be read using: Setting.some_setting_name
58 # Then setting values can be read using: Setting.some_setting_name
59 # or set using Setting.some_setting_name = "some value"
59 # or set using Setting.some_setting_name = "some value"
60 @@available_settings.each do |name, params|
60 @@available_settings.each do |name, params|
61 src = <<-END_SRC
61 src = <<-END_SRC
62 def self.#{name}
62 def self.#{name}
63 self[:#{name}]
63 self[:#{name}]
64 end
64 end
65
65
66 def self.#{name}?
66 def self.#{name}?
67 self[:#{name}].to_i > 0
67 self[:#{name}].to_i > 0
68 end
68 end
69
69
70 def self.#{name}=(value)
70 def self.#{name}=(value)
71 self[:#{name}] = value
71 self[:#{name}] = value
72 end
72 end
73 END_SRC
73 END_SRC
74 class_eval src, __FILE__, __LINE__
74 class_eval src, __FILE__, __LINE__
75 end
75 end
76
76
77 # Checks if settings have changed since the values were read
77 # Checks if settings have changed since the values were read
78 # and clears the cache hash if it's the case
78 # and clears the cache hash if it's the case
79 # Called once per request
79 # Called once per request
80 def self.check_cache
80 def self.check_cache
81 settings_updated_on = Setting.maximum(:updated_on)
81 settings_updated_on = Setting.maximum(:updated_on)
82 if settings_updated_on && @cached_cleared_on <= settings_updated_on
82 if settings_updated_on && @cached_cleared_on <= settings_updated_on
83 @cached_settings.clear
83 @cached_settings.clear
84 @cached_cleared_on = Time.now
84 @cached_cleared_on = Time.now
85 logger.info "Settings cache cleared." if logger
85 logger.info "Settings cache cleared." if logger
86 end
86 end
87 end
87 end
88
88
89 private
89 private
90 # Returns the Setting instance for the setting named name
90 # Returns the Setting instance for the setting named name
91 # (record found in database or new record with default value)
91 # (record found in database or new record with default value)
92 def self.find_or_default(name)
92 def self.find_or_default(name)
93 name = name.to_s
93 name = name.to_s
94 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
94 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
95 setting = find_by_name(name)
95 setting = find_by_name(name)
96 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
96 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
97 end
97 end
98 end
98 end
General Comments 0
You need to be logged in to leave comments. Login now