2011-05-18 06:19:08 +04:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2007-01-21 14:50:22 +03:00
|
|
|
#
|
|
|
|
# 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.
|
2011-05-18 06:19:08 +04:00
|
|
|
#
|
2007-01-21 14:50:22 +03:00
|
|
|
# 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.
|
2011-05-18 06:19:08 +04:00
|
|
|
#
|
2007-01-21 14:50:22 +03:00
|
|
|
# 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
|
|
|
|
|
2007-11-16 23:26:36 +03:00
|
|
|
DATE_FORMATS = [
|
2013-02-17 14:16:46 +04:00
|
|
|
'%Y-%m-%d',
|
|
|
|
'%d/%m/%Y',
|
|
|
|
'%d.%m.%Y',
|
|
|
|
'%d-%m-%Y',
|
|
|
|
'%m/%d/%Y',
|
|
|
|
'%d %b %Y',
|
|
|
|
'%d %B %Y',
|
|
|
|
'%b %d, %Y',
|
|
|
|
'%B %d, %Y'
|
2007-11-16 23:26:36 +03:00
|
|
|
]
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-11-16 23:26:36 +03:00
|
|
|
TIME_FORMATS = [
|
|
|
|
'%H:%M',
|
|
|
|
'%I:%M %p'
|
|
|
|
]
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2008-08-26 16:13:15 +04:00
|
|
|
ENCODINGS = %w(US-ASCII
|
|
|
|
windows-1250
|
|
|
|
windows-1251
|
|
|
|
windows-1252
|
|
|
|
windows-1253
|
|
|
|
windows-1254
|
|
|
|
windows-1255
|
|
|
|
windows-1256
|
|
|
|
windows-1257
|
|
|
|
windows-1258
|
|
|
|
windows-31j
|
|
|
|
ISO-2022-JP
|
|
|
|
ISO-2022-KR
|
|
|
|
ISO-8859-1
|
|
|
|
ISO-8859-2
|
|
|
|
ISO-8859-3
|
|
|
|
ISO-8859-4
|
|
|
|
ISO-8859-5
|
|
|
|
ISO-8859-6
|
|
|
|
ISO-8859-7
|
|
|
|
ISO-8859-8
|
|
|
|
ISO-8859-9
|
|
|
|
ISO-8859-13
|
|
|
|
ISO-8859-15
|
|
|
|
KOI8-R
|
|
|
|
UTF-8
|
|
|
|
UTF-16
|
|
|
|
UTF-16BE
|
|
|
|
UTF-16LE
|
|
|
|
EUC-JP
|
|
|
|
Shift_JIS
|
2011-02-21 12:41:34 +03:00
|
|
|
CP932
|
2008-08-26 16:13:15 +04:00
|
|
|
GB18030
|
|
|
|
GBK
|
|
|
|
ISCII91
|
|
|
|
EUC-KR
|
|
|
|
Big5
|
|
|
|
Big5-HKSCS
|
|
|
|
TIS-620)
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-01-21 14:50:22 +03:00
|
|
|
cattr_accessor :available_settings
|
2011-06-16 05:52:45 +04:00
|
|
|
@@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
|
2008-11-16 18:38:37 +03:00
|
|
|
Redmine::Plugin.all.each do |plugin|
|
2007-09-23 21:19:27 +04:00
|
|
|
next unless plugin.settings
|
2011-05-18 06:19:08 +04:00
|
|
|
@@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
|
2007-09-23 21:19:27 +04:00
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-01-21 14:50:22 +03:00
|
|
|
validates_uniqueness_of :name
|
|
|
|
validates_inclusion_of :name, :in => @@available_settings.keys
|
2013-11-30 01:18:35 +04:00
|
|
|
validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
|
|
|
|
(s = @@available_settings[setting.name]) && s['format'] == 'int'
|
|
|
|
}
|
2007-08-31 21:45:32 +04:00
|
|
|
|
|
|
|
# Hash used to cache setting values
|
|
|
|
@cached_settings = {}
|
|
|
|
@cached_cleared_on = Time.now
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-09-14 19:30:46 +04:00
|
|
|
def value
|
|
|
|
v = read_attribute(:value)
|
|
|
|
# Unserialize serialized settings
|
|
|
|
v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
|
2008-01-25 13:31:06 +03:00
|
|
|
v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
|
2007-09-14 19:30:46 +04:00
|
|
|
v
|
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-09-18 21:50:17 +04:00
|
|
|
def value=(v)
|
2010-02-13 01:08:02 +03:00
|
|
|
v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
|
2008-01-25 13:31:06 +03:00
|
|
|
write_attribute(:value, v.to_s)
|
2007-09-18 21:50:17 +04:00
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-08-31 21:45:32 +04:00
|
|
|
# Returns the value of the setting named name
|
2007-01-21 14:50:22 +03:00
|
|
|
def self.[](name)
|
2007-09-14 19:30:46 +04:00
|
|
|
v = @cached_settings[name]
|
|
|
|
v ? v : (@cached_settings[name] = find_or_default(name).value)
|
2007-01-21 14:50:22 +03:00
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-09-14 19:30:46 +04:00
|
|
|
def self.[]=(name, v)
|
2007-08-31 21:45:32 +04:00
|
|
|
setting = find_or_default(name)
|
2007-09-14 19:30:46 +04:00
|
|
|
setting.value = (v ? v : "")
|
2007-08-31 21:45:32 +04:00
|
|
|
@cached_settings[name] = nil
|
2007-01-21 14:50:22 +03:00
|
|
|
setting.save
|
|
|
|
setting.value
|
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-08-31 21:45:32 +04: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 14:50:22 +03:00
|
|
|
@@available_settings.each do |name, params|
|
|
|
|
src = <<-END_SRC
|
|
|
|
def self.#{name}
|
|
|
|
self[:#{name}]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.#{name}?
|
2007-05-06 16:49:32 +04:00
|
|
|
self[:#{name}].to_i > 0
|
2007-01-21 14:50:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.#{name}=(value)
|
2007-02-02 17:28:54 +03:00
|
|
|
self[:#{name}] = value
|
2007-01-21 14:50:22 +03:00
|
|
|
end
|
2013-10-05 13:41:11 +04:00
|
|
|
END_SRC
|
2007-01-21 14:50:22 +03:00
|
|
|
class_eval src, __FILE__, __LINE__
|
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2013-10-05 13:41:11 +04:00
|
|
|
# Sets a setting value from params
|
|
|
|
def self.set_from_params(name, params)
|
|
|
|
params = params.dup
|
|
|
|
params.delete_if {|v| v.blank? } if params.is_a?(Array)
|
|
|
|
|
|
|
|
m = "#{name}_from_params"
|
|
|
|
if respond_to? m
|
|
|
|
self[name.to_sym] = send m, params
|
|
|
|
else
|
|
|
|
self[name.to_sym] = params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a hash suitable for commit_update_keywords setting
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
|
|
|
|
# Setting.commit_update_keywords_from_params(params)
|
2013-10-13 11:37:49 +04:00
|
|
|
# # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
|
2013-10-05 13:41:11 +04:00
|
|
|
def self.commit_update_keywords_from_params(params)
|
2013-10-13 11:37:49 +04:00
|
|
|
s = []
|
2013-10-05 13:41:11 +04:00
|
|
|
if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
|
|
|
|
attributes = params.except(:keywords).keys
|
|
|
|
params[:keywords].each_with_index do |keywords, i|
|
|
|
|
next if keywords.blank?
|
2013-10-13 11:37:49 +04:00
|
|
|
s << attributes.inject({}) {|h, a|
|
2013-10-05 13:41:11 +04:00
|
|
|
value = params[a][i].to_s
|
|
|
|
h[a.to_s] = value if value.present?
|
|
|
|
h
|
2013-10-13 11:37:49 +04:00
|
|
|
}.merge('keywords' => keywords)
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2007-12-29 14:36:30 +03:00
|
|
|
# Helper that returns an array based on per_page_options setting
|
|
|
|
def self.per_page_options_array
|
|
|
|
per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
|
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2013-10-05 13:41:11 +04:00
|
|
|
# Helper that returns a Hash with single update keywords as keys
|
2013-10-13 11:37:49 +04:00
|
|
|
def self.commit_update_keywords_array
|
|
|
|
a = []
|
|
|
|
if commit_update_keywords.is_a?(Array)
|
|
|
|
commit_update_keywords.each do |rule|
|
|
|
|
next unless rule.is_a?(Hash)
|
|
|
|
rule = rule.dup
|
|
|
|
rule.delete_if {|k, v| v.blank?}
|
|
|
|
keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
|
|
|
|
next if keywords.empty?
|
|
|
|
a << rule.merge('keywords' => keywords)
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
end
|
2013-10-13 11:37:49 +04:00
|
|
|
a
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.commit_fix_keywords
|
2013-10-13 11:37:49 +04:00
|
|
|
ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
|
|
|
|
if commit_update_keywords.is_a?(Array)
|
|
|
|
commit_update_keywords.first && commit_update_keywords.first['keywords']
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.commit_fix_status_id
|
2013-10-13 11:37:49 +04:00
|
|
|
ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
|
|
|
|
if commit_update_keywords.is_a?(Array)
|
|
|
|
commit_update_keywords.first && commit_update_keywords.first['status_id']
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.commit_fix_done_ratio
|
2013-10-13 11:37:49 +04:00
|
|
|
ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
|
|
|
|
if commit_update_keywords.is_a?(Array)
|
|
|
|
commit_update_keywords.first && commit_update_keywords.first['done_ratio']
|
2013-10-05 13:41:11 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-13 00:25:50 +03:00
|
|
|
def self.openid?
|
2009-02-20 00:03:08 +03:00
|
|
|
Object.const_defined?(:OpenID) && self[:openid].to_i > 0
|
2009-02-13 00:25:50 +03:00
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-08-31 21:45:32 +04: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
|
2011-11-13 19:20:31 +04:00
|
|
|
clear_cache
|
2007-08-31 21:45:32 +04:00
|
|
|
end
|
|
|
|
end
|
2013-03-25 17:39:56 +04:00
|
|
|
|
2011-11-13 19:20:31 +04:00
|
|
|
# Clears the settings cache
|
|
|
|
def self.clear_cache
|
|
|
|
@cached_settings.clear
|
|
|
|
@cached_cleared_on = Time.now
|
|
|
|
logger.info "Settings cache cleared." if logger
|
|
|
|
end
|
2011-05-18 06:19:08 +04:00
|
|
|
|
2007-08-31 21:45:32 +04:00
|
|
|
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
|
2011-05-18 06:19:08 +04:00
|
|
|
raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
|
2013-12-20 15:54:23 +04:00
|
|
|
setting = where(:name => name).first
|
2012-02-19 19:34:06 +04:00
|
|
|
unless setting
|
2013-12-20 15:54:23 +04:00
|
|
|
setting = new
|
|
|
|
setting.name = name
|
2012-02-19 19:34:06 +04:00
|
|
|
setting.value = @@available_settings[name]['default']
|
|
|
|
end
|
|
|
|
setting
|
2007-08-31 21:45:32 +04:00
|
|
|
end
|
2007-01-21 14:50:22 +03:00
|
|
|
end
|