##// END OF EJS Templates
Fixed: Oracle error when saving serialized setting (eg. mail notifications)...
Jean-Philippe Lang -
r731:25012efc9c9b
parent child
Show More
@@ -1,93 +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)
39 v = v.to_yaml if @@available_settings[name]['serialized'] && v.is_a?(String)
40 write_attribute(:value, v)
41 end
42
38 # Returns the value of the setting named name
43 # Returns the value of the setting named name
39 def self.[](name)
44 def self.[](name)
40 v = @cached_settings[name]
45 v = @cached_settings[name]
41 v ? v : (@cached_settings[name] = find_or_default(name).value)
46 v ? v : (@cached_settings[name] = find_or_default(name).value)
42 end
47 end
43
48
44 def self.[]=(name, v)
49 def self.[]=(name, v)
45 setting = find_or_default(name)
50 setting = find_or_default(name)
46 setting.value = (v ? v : "")
51 setting.value = (v ? v : "")
47 @cached_settings[name] = nil
52 @cached_settings[name] = nil
48 setting.save
53 setting.save
49 setting.value
54 setting.value
50 end
55 end
51
56
52 # Defines getter and setter for each setting
57 # Defines getter and setter for each setting
53 # Then setting values can be read using: Setting.some_setting_name
58 # Then setting values can be read using: Setting.some_setting_name
54 # or set using Setting.some_setting_name = "some value"
59 # or set using Setting.some_setting_name = "some value"
55 @@available_settings.each do |name, params|
60 @@available_settings.each do |name, params|
56 src = <<-END_SRC
61 src = <<-END_SRC
57 def self.#{name}
62 def self.#{name}
58 self[:#{name}]
63 self[:#{name}]
59 end
64 end
60
65
61 def self.#{name}?
66 def self.#{name}?
62 self[:#{name}].to_i > 0
67 self[:#{name}].to_i > 0
63 end
68 end
64
69
65 def self.#{name}=(value)
70 def self.#{name}=(value)
66 self[:#{name}] = value
71 self[:#{name}] = value
67 end
72 end
68 END_SRC
73 END_SRC
69 class_eval src, __FILE__, __LINE__
74 class_eval src, __FILE__, __LINE__
70 end
75 end
71
76
72 # Checks if settings have changed since the values were read
77 # Checks if settings have changed since the values were read
73 # and clears the cache hash if it's the case
78 # and clears the cache hash if it's the case
74 # Called once per request
79 # Called once per request
75 def self.check_cache
80 def self.check_cache
76 settings_updated_on = Setting.maximum(:updated_on)
81 settings_updated_on = Setting.maximum(:updated_on)
77 if settings_updated_on && @cached_cleared_on <= settings_updated_on
82 if settings_updated_on && @cached_cleared_on <= settings_updated_on
78 @cached_settings.clear
83 @cached_settings.clear
79 @cached_cleared_on = Time.now
84 @cached_cleared_on = Time.now
80 logger.info "Settings cache cleared." if logger
85 logger.info "Settings cache cleared." if logger
81 end
86 end
82 end
87 end
83
88
84 private
89 private
85 # Returns the Setting instance for the setting named name
90 # Returns the Setting instance for the setting named name
86 # (record found in database or new record with default value)
91 # (record found in database or new record with default value)
87 def self.find_or_default(name)
92 def self.find_or_default(name)
88 name = name.to_s
93 name = name.to_s
89 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)
90 setting = find_by_name(name)
95 setting = find_by_name(name)
91 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
92 end
97 end
93 end
98 end
@@ -1,39 +1,45
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 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class SettingTest < Test::Unit::TestCase
20 class SettingTest < Test::Unit::TestCase
21
21
22 def test_read_default
22 def test_read_default
23 assert_equal "Redmine", Setting.app_title
23 assert_equal "Redmine", Setting.app_title
24 assert Setting.self_registration?
24 assert Setting.self_registration?
25 assert !Setting.login_required?
25 assert !Setting.login_required?
26 end
26 end
27
27
28 def test_update
28 def test_update
29 Setting.app_title = "My title"
29 Setting.app_title = "My title"
30 assert_equal "My title", Setting.app_title
30 assert_equal "My title", Setting.app_title
31 # make sure db has been updated (INSERT)
31 # make sure db has been updated (INSERT)
32 assert_equal "My title", Setting.find_by_name('app_title').value
32 assert_equal "My title", Setting.find_by_name('app_title').value
33
33
34 Setting.app_title = "My other title"
34 Setting.app_title = "My other title"
35 assert_equal "My other title", Setting.app_title
35 assert_equal "My other title", Setting.app_title
36 # make sure db has been updated (UPDATE)
36 # make sure db has been updated (UPDATE)
37 assert_equal "My other title", Setting.find_by_name('app_title').value
37 assert_equal "My other title", Setting.find_by_name('app_title').value
38 end
38 end
39
40 def test_serialized_setting
41 Setting.notified_events = ['issue_added', 'issue_updated', 'news_added']
42 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events
43 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value
44 end
39 end
45 end
General Comments 0
You need to be logged in to leave comments. Login now