To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / .svn / text-base / setting.rb.svn-base @ 442:753f1380d6bc

History | View | Annotate | Download (5.03 KB)

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