| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  | # redMine - project management software | 
					
						
							|  |  |  | # Copyright (C) 2006-2007  Jean-Philippe Lang | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							|  |  |  | #  | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | #  | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Setting < ActiveRecord::Base | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cattr_accessor :available_settings | 
					
						
							|  |  |  |   @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   validates_uniqueness_of :name | 
					
						
							|  |  |  |   validates_inclusion_of :name, :in => @@available_settings.keys | 
					
						
							|  |  |  |   validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }   | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Hash used to cache setting values | 
					
						
							|  |  |  |   @cached_settings = {} | 
					
						
							|  |  |  |   @cached_cleared_on = Time.now | 
					
						
							| 
									
										
										
										
											2007-09-14 15:30:46 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   def value | 
					
						
							|  |  |  |     v = read_attribute(:value) | 
					
						
							|  |  |  |     # Unserialize serialized settings | 
					
						
							|  |  |  |     v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) | 
					
						
							|  |  |  |     v | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-09-18 17:50:17 +00:00
										 |  |  |   def value=(v) | 
					
						
							|  |  |  |     v = v.to_yaml if @@available_settings[name]['serialized'] && v.is_a?(String) | 
					
						
							|  |  |  |     write_attribute(:value, v) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  |   # Returns the value of the setting named name | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |   def self.[](name) | 
					
						
							| 
									
										
										
										
											2007-09-14 15:30:46 +00:00
										 |  |  |     v = @cached_settings[name] | 
					
						
							|  |  |  |     v ? v : (@cached_settings[name] = find_or_default(name).value) | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-09-14 15:30:46 +00:00
										 |  |  |   def self.[]=(name, v) | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  |     setting = find_or_default(name) | 
					
						
							| 
									
										
										
										
											2007-09-14 15:30:46 +00:00
										 |  |  |     setting.value = (v ? v : "") | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  |     @cached_settings[name] = nil | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |     setting.save | 
					
						
							|  |  |  |     setting.value | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  |   # Defines getter and setter for each setting | 
					
						
							|  |  |  |   # Then setting values can be read using: Setting.some_setting_name | 
					
						
							|  |  |  |   # or set using Setting.some_setting_name = "some value" | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |   @@available_settings.each do |name, params| | 
					
						
							|  |  |  |     src = <<-END_SRC
 | 
					
						
							|  |  |  |     def self.#{name} | 
					
						
							|  |  |  |       self[:#{name}] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.#{name}? | 
					
						
							| 
									
										
										
										
											2007-05-06 12:49:32 +00:00
										 |  |  |       self[:#{name}].to_i > 0 | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.#{name}=(value) | 
					
						
							| 
									
										
										
										
											2007-02-02 14:28:54 +00:00
										 |  |  |       self[:#{name}] = value | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |     END_SRC | 
					
						
							|  |  |  |     class_eval src, __FILE__, __LINE__ | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-08-31 17:45:32 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   # Checks if settings have changed since the values were read | 
					
						
							|  |  |  |   # and clears the cache hash if it's the case | 
					
						
							|  |  |  |   # Called once per request | 
					
						
							|  |  |  |   def self.check_cache | 
					
						
							|  |  |  |     settings_updated_on = Setting.maximum(:updated_on) | 
					
						
							|  |  |  |     if settings_updated_on && @cached_cleared_on <= settings_updated_on | 
					
						
							|  |  |  |       @cached_settings.clear | 
					
						
							|  |  |  |       @cached_cleared_on = Time.now | 
					
						
							|  |  |  |       logger.info "Settings cache cleared." if logger | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | private | 
					
						
							|  |  |  |   # Returns the Setting instance for the setting named name | 
					
						
							|  |  |  |   # (record found in database or new record with default value) | 
					
						
							|  |  |  |   def self.find_or_default(name) | 
					
						
							|  |  |  |     name = name.to_s | 
					
						
							|  |  |  |     raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)     | 
					
						
							|  |  |  |     setting = find_by_name(name) | 
					
						
							|  |  |  |     setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-01-21 11:50:22 +00:00
										 |  |  | end |