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