annotate app/models/setting.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Setting < ActiveRecord::Base
Chris@0 19
Chris@0 20 DATE_FORMATS = [
Chris@0 21 '%Y-%m-%d',
Chris@0 22 '%d/%m/%Y',
Chris@0 23 '%d.%m.%Y',
Chris@0 24 '%d-%m-%Y',
Chris@0 25 '%m/%d/%Y',
Chris@0 26 '%d %b %Y',
Chris@0 27 '%d %B %Y',
Chris@0 28 '%b %d, %Y',
Chris@0 29 '%B %d, %Y'
Chris@0 30 ]
Chris@441 31
Chris@0 32 TIME_FORMATS = [
Chris@0 33 '%H:%M',
Chris@0 34 '%I:%M %p'
Chris@0 35 ]
Chris@441 36
Chris@0 37 ENCODINGS = %w(US-ASCII
Chris@0 38 windows-1250
Chris@0 39 windows-1251
Chris@0 40 windows-1252
Chris@0 41 windows-1253
Chris@0 42 windows-1254
Chris@0 43 windows-1255
Chris@0 44 windows-1256
Chris@0 45 windows-1257
Chris@0 46 windows-1258
Chris@0 47 windows-31j
Chris@0 48 ISO-2022-JP
Chris@0 49 ISO-2022-KR
Chris@0 50 ISO-8859-1
Chris@0 51 ISO-8859-2
Chris@0 52 ISO-8859-3
Chris@0 53 ISO-8859-4
Chris@0 54 ISO-8859-5
Chris@0 55 ISO-8859-6
Chris@0 56 ISO-8859-7
Chris@0 57 ISO-8859-8
Chris@0 58 ISO-8859-9
Chris@0 59 ISO-8859-13
Chris@0 60 ISO-8859-15
Chris@0 61 KOI8-R
Chris@0 62 UTF-8
Chris@0 63 UTF-16
Chris@0 64 UTF-16BE
Chris@0 65 UTF-16LE
Chris@0 66 EUC-JP
Chris@0 67 Shift_JIS
Chris@245 68 CP932
Chris@0 69 GB18030
Chris@0 70 GBK
Chris@0 71 ISCII91
Chris@0 72 EUC-KR
Chris@0 73 Big5
Chris@0 74 Big5-HKSCS
Chris@0 75 TIS-620)
Chris@441 76
Chris@0 77 cattr_accessor :available_settings
Chris@909 78 @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
Chris@0 79 Redmine::Plugin.all.each do |plugin|
Chris@0 80 next unless plugin.settings
Chris@441 81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
Chris@0 82 end
Chris@441 83
Chris@0 84 validates_uniqueness_of :name
Chris@0 85 validates_inclusion_of :name, :in => @@available_settings.keys
Chris@441 86 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
Chris@0 87
Chris@0 88 # Hash used to cache setting values
Chris@0 89 @cached_settings = {}
Chris@0 90 @cached_cleared_on = Time.now
Chris@441 91
Chris@0 92 def value
Chris@0 93 v = read_attribute(:value)
Chris@0 94 # Unserialize serialized settings
Chris@0 95 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
Chris@0 96 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
Chris@0 97 v
Chris@0 98 end
Chris@441 99
Chris@0 100 def value=(v)
Chris@0 101 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
Chris@0 102 write_attribute(:value, v.to_s)
Chris@0 103 end
Chris@441 104
Chris@0 105 # Returns the value of the setting named name
Chris@0 106 def self.[](name)
Chris@0 107 v = @cached_settings[name]
Chris@0 108 v ? v : (@cached_settings[name] = find_or_default(name).value)
Chris@0 109 end
Chris@441 110
Chris@0 111 def self.[]=(name, v)
Chris@0 112 setting = find_or_default(name)
Chris@0 113 setting.value = (v ? v : "")
Chris@0 114 @cached_settings[name] = nil
Chris@0 115 setting.save
Chris@0 116 setting.value
Chris@0 117 end
Chris@441 118
Chris@0 119 # Defines getter and setter for each setting
Chris@0 120 # Then setting values can be read using: Setting.some_setting_name
Chris@0 121 # or set using Setting.some_setting_name = "some value"
Chris@0 122 @@available_settings.each do |name, params|
Chris@0 123 src = <<-END_SRC
Chris@0 124 def self.#{name}
Chris@0 125 self[:#{name}]
Chris@0 126 end
Chris@0 127
Chris@0 128 def self.#{name}?
Chris@0 129 self[:#{name}].to_i > 0
Chris@0 130 end
Chris@0 131
Chris@0 132 def self.#{name}=(value)
Chris@0 133 self[:#{name}] = value
Chris@0 134 end
Chris@0 135 END_SRC
Chris@0 136 class_eval src, __FILE__, __LINE__
Chris@0 137 end
Chris@441 138
Chris@0 139 # Helper that returns an array based on per_page_options setting
Chris@0 140 def self.per_page_options_array
Chris@0 141 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
Chris@0 142 end
Chris@441 143
Chris@0 144 def self.openid?
Chris@0 145 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
Chris@0 146 end
Chris@441 147
Chris@0 148 # Checks if settings have changed since the values were read
Chris@0 149 # and clears the cache hash if it's the case
Chris@0 150 # Called once per request
Chris@0 151 def self.check_cache
Chris@0 152 settings_updated_on = Setting.maximum(:updated_on)
Chris@0 153 if settings_updated_on && @cached_cleared_on <= settings_updated_on
Chris@909 154 clear_cache
Chris@0 155 end
Chris@0 156 end
Chris@909 157
Chris@909 158 # Clears the settings cache
Chris@909 159 def self.clear_cache
Chris@909 160 @cached_settings.clear
Chris@909 161 @cached_cleared_on = Time.now
Chris@909 162 logger.info "Settings cache cleared." if logger
Chris@909 163 end
Chris@441 164
Chris@0 165 private
Chris@0 166 # Returns the Setting instance for the setting named name
Chris@0 167 # (record found in database or new record with default value)
Chris@0 168 def self.find_or_default(name)
Chris@0 169 name = name.to_s
Chris@441 170 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
Chris@0 171 setting = find_by_name(name)
Chris@909 172 unless setting
Chris@909 173 setting = new(:name => name)
Chris@909 174 setting.value = @@available_settings[name]['default']
Chris@909 175 end
Chris@909 176 setting
Chris@0 177 end
Chris@0 178 end