@@ -1,16 +1,16 | |||
|
1 |
# |
|
|
2 |
# Copyright (C) 2006-20 |
|
|
1 | # Redmine - project management software | |
|
2 | # Copyright (C) 2006-2011 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. |
@@ -28,12 +28,12 class Setting < ActiveRecord::Base | |||
|
28 | 28 | '%b %d, %Y', |
|
29 | 29 | '%B %d, %Y' |
|
30 | 30 | ] |
|
31 | ||
|
31 | ||
|
32 | 32 | TIME_FORMATS = [ |
|
33 | 33 | '%H:%M', |
|
34 | 34 | '%I:%M %p' |
|
35 | 35 | ] |
|
36 | ||
|
36 | ||
|
37 | 37 | ENCODINGS = %w(US-ASCII |
|
38 | 38 | windows-1250 |
|
39 | 39 | windows-1251 |
@@ -73,22 +73,22 class Setting < ActiveRecord::Base | |||
|
73 | 73 | Big5 |
|
74 | 74 | Big5-HKSCS |
|
75 | 75 | TIS-620) |
|
76 | ||
|
76 | ||
|
77 | 77 | cattr_accessor :available_settings |
|
78 | 78 | @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml")) |
|
79 | 79 | Redmine::Plugin.all.each do |plugin| |
|
80 | 80 | next unless plugin.settings |
|
81 |
@@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} |
|
|
81 | @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} | |
|
82 | 82 | end |
|
83 | ||
|
83 | ||
|
84 | 84 | validates_uniqueness_of :name |
|
85 | 85 | validates_inclusion_of :name, :in => @@available_settings.keys |
|
86 |
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' } | |
|
87 | 87 | |
|
88 | 88 | # Hash used to cache setting values |
|
89 | 89 | @cached_settings = {} |
|
90 | 90 | @cached_cleared_on = Time.now |
|
91 | ||
|
91 | ||
|
92 | 92 | def value |
|
93 | 93 | v = read_attribute(:value) |
|
94 | 94 | # Unserialize serialized settings |
@@ -96,18 +96,18 class Setting < ActiveRecord::Base | |||
|
96 | 96 | v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank? |
|
97 | 97 | v |
|
98 | 98 | end |
|
99 | ||
|
99 | ||
|
100 | 100 | def value=(v) |
|
101 | 101 | v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] |
|
102 | 102 | write_attribute(:value, v.to_s) |
|
103 | 103 | end |
|
104 | ||
|
104 | ||
|
105 | 105 | # Returns the value of the setting named name |
|
106 | 106 | def self.[](name) |
|
107 | 107 | v = @cached_settings[name] |
|
108 | 108 | v ? v : (@cached_settings[name] = find_or_default(name).value) |
|
109 | 109 | end |
|
110 | ||
|
110 | ||
|
111 | 111 | def self.[]=(name, v) |
|
112 | 112 | setting = find_or_default(name) |
|
113 | 113 | setting.value = (v ? v : "") |
@@ -115,7 +115,7 class Setting < ActiveRecord::Base | |||
|
115 | 115 | setting.save |
|
116 | 116 | setting.value |
|
117 | 117 | end |
|
118 | ||
|
118 | ||
|
119 | 119 | # Defines getter and setter for each setting |
|
120 | 120 | # Then setting values can be read using: Setting.some_setting_name |
|
121 | 121 | # or set using Setting.some_setting_name = "some value" |
@@ -135,16 +135,16 class Setting < ActiveRecord::Base | |||
|
135 | 135 | END_SRC |
|
136 | 136 | class_eval src, __FILE__, __LINE__ |
|
137 | 137 | end |
|
138 | ||
|
138 | ||
|
139 | 139 | # Helper that returns an array based on per_page_options setting |
|
140 | 140 | def self.per_page_options_array |
|
141 | 141 | per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort |
|
142 | 142 | end |
|
143 | ||
|
143 | ||
|
144 | 144 | def self.openid? |
|
145 | 145 | Object.const_defined?(:OpenID) && self[:openid].to_i > 0 |
|
146 | 146 | end |
|
147 | ||
|
147 | ||
|
148 | 148 | # Checks if settings have changed since the values were read |
|
149 | 149 | # and clears the cache hash if it's the case |
|
150 | 150 | # Called once per request |
@@ -156,13 +156,13 class Setting < ActiveRecord::Base | |||
|
156 | 156 | logger.info "Settings cache cleared." if logger |
|
157 | 157 | end |
|
158 | 158 | end |
|
159 | ||
|
159 | ||
|
160 | 160 | private |
|
161 | 161 | # Returns the Setting instance for the setting named name |
|
162 | 162 | # (record found in database or new record with default value) |
|
163 | 163 | def self.find_or_default(name) |
|
164 | 164 | name = name.to_s |
|
165 |
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) | |
|
166 | 166 | setting = find_by_name(name) |
|
167 | 167 | setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name |
|
168 | 168 | end |
General Comments 0
You need to be logged in to leave comments.
Login now