Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Setting < ActiveRecord::Base Chris@0: Chris@0: DATE_FORMATS = [ Chris@1464: '%Y-%m-%d', Chris@1464: '%d/%m/%Y', Chris@1464: '%d.%m.%Y', Chris@1464: '%d-%m-%Y', Chris@1464: '%m/%d/%Y', Chris@1464: '%d %b %Y', Chris@1464: '%d %B %Y', Chris@1464: '%b %d, %Y', Chris@1464: '%B %d, %Y' Chris@0: ] Chris@441: Chris@0: TIME_FORMATS = [ Chris@0: '%H:%M', Chris@0: '%I:%M %p' Chris@0: ] Chris@441: Chris@0: ENCODINGS = %w(US-ASCII Chris@0: windows-1250 Chris@0: windows-1251 Chris@0: windows-1252 Chris@0: windows-1253 Chris@0: windows-1254 Chris@0: windows-1255 Chris@0: windows-1256 Chris@0: windows-1257 Chris@0: windows-1258 Chris@0: windows-31j Chris@0: ISO-2022-JP Chris@0: ISO-2022-KR Chris@0: ISO-8859-1 Chris@0: ISO-8859-2 Chris@0: ISO-8859-3 Chris@0: ISO-8859-4 Chris@0: ISO-8859-5 Chris@0: ISO-8859-6 Chris@0: ISO-8859-7 Chris@0: ISO-8859-8 Chris@0: ISO-8859-9 Chris@0: ISO-8859-13 Chris@0: ISO-8859-15 Chris@0: KOI8-R Chris@0: UTF-8 Chris@0: UTF-16 Chris@0: UTF-16BE Chris@0: UTF-16LE Chris@0: EUC-JP Chris@0: Shift_JIS Chris@245: CP932 Chris@0: GB18030 Chris@0: GBK Chris@0: ISCII91 Chris@0: EUC-KR Chris@0: Big5 Chris@0: Big5-HKSCS Chris@0: TIS-620) Chris@441: Chris@0: cattr_accessor :available_settings Chris@909: @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml")) Chris@0: Redmine::Plugin.all.each do |plugin| Chris@0: next unless plugin.settings Chris@441: @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} Chris@0: end Chris@441: Chris@0: validates_uniqueness_of :name Chris@0: validates_inclusion_of :name, :in => @@available_settings.keys Chris@441: validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } Chris@0: Chris@0: # Hash used to cache setting values Chris@0: @cached_settings = {} Chris@0: @cached_cleared_on = Time.now Chris@441: Chris@0: def value Chris@0: v = read_attribute(:value) Chris@0: # Unserialize serialized settings Chris@0: v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) Chris@0: v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank? Chris@0: v Chris@0: end Chris@441: Chris@0: def value=(v) Chris@0: v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] Chris@0: write_attribute(:value, v.to_s) Chris@0: end Chris@441: Chris@0: # Returns the value of the setting named name Chris@0: def self.[](name) Chris@0: v = @cached_settings[name] Chris@0: v ? v : (@cached_settings[name] = find_or_default(name).value) Chris@0: end Chris@441: Chris@0: def self.[]=(name, v) Chris@0: setting = find_or_default(name) Chris@0: setting.value = (v ? v : "") Chris@0: @cached_settings[name] = nil Chris@0: setting.save Chris@0: setting.value Chris@0: end Chris@441: Chris@0: # Defines getter and setter for each setting Chris@0: # Then setting values can be read using: Setting.some_setting_name Chris@0: # or set using Setting.some_setting_name = "some value" Chris@0: @@available_settings.each do |name, params| Chris@0: src = <<-END_SRC Chris@0: def self.#{name} Chris@0: self[:#{name}] Chris@0: end Chris@0: Chris@0: def self.#{name}? Chris@0: self[:#{name}].to_i > 0 Chris@0: end Chris@0: Chris@0: def self.#{name}=(value) Chris@0: self[:#{name}] = value Chris@0: end Chris@1464: END_SRC Chris@0: class_eval src, __FILE__, __LINE__ Chris@0: end Chris@441: Chris@1464: # Sets a setting value from params Chris@1464: def self.set_from_params(name, params) Chris@1464: params = params.dup Chris@1464: params.delete_if {|v| v.blank? } if params.is_a?(Array) Chris@1464: Chris@1464: m = "#{name}_from_params" Chris@1464: if respond_to? m Chris@1464: self[name.to_sym] = send m, params Chris@1464: else Chris@1464: self[name.to_sym] = params Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: # Returns a hash suitable for commit_update_keywords setting Chris@1464: # Chris@1464: # Example: Chris@1464: # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]} Chris@1464: # Setting.commit_update_keywords_from_params(params) Chris@1464: # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}] Chris@1464: def self.commit_update_keywords_from_params(params) Chris@1464: s = [] Chris@1464: if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array} Chris@1464: attributes = params.except(:keywords).keys Chris@1464: params[:keywords].each_with_index do |keywords, i| Chris@1464: next if keywords.blank? Chris@1464: s << attributes.inject({}) {|h, a| Chris@1464: value = params[a][i].to_s Chris@1464: h[a.to_s] = value if value.present? Chris@1464: h Chris@1464: }.merge('keywords' => keywords) Chris@1464: end Chris@1464: end Chris@1464: s Chris@1464: end Chris@1464: Chris@0: # Helper that returns an array based on per_page_options setting Chris@0: def self.per_page_options_array Chris@0: per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort Chris@0: end Chris@441: Chris@1464: # Helper that returns a Hash with single update keywords as keys Chris@1464: def self.commit_update_keywords_array Chris@1464: a = [] Chris@1464: if commit_update_keywords.is_a?(Array) Chris@1464: commit_update_keywords.each do |rule| Chris@1464: next unless rule.is_a?(Hash) Chris@1464: rule = rule.dup Chris@1464: rule.delete_if {|k, v| v.blank?} Chris@1464: keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?) Chris@1464: next if keywords.empty? Chris@1464: a << rule.merge('keywords' => keywords) Chris@1464: end Chris@1464: end Chris@1464: a Chris@1464: end Chris@1464: Chris@1464: def self.commit_fix_keywords Chris@1464: ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3" Chris@1464: if commit_update_keywords.is_a?(Array) Chris@1464: commit_update_keywords.first && commit_update_keywords.first['keywords'] Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def self.commit_fix_status_id Chris@1464: ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3" Chris@1464: if commit_update_keywords.is_a?(Array) Chris@1464: commit_update_keywords.first && commit_update_keywords.first['status_id'] Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def self.commit_fix_done_ratio Chris@1464: ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3" Chris@1464: if commit_update_keywords.is_a?(Array) Chris@1464: commit_update_keywords.first && commit_update_keywords.first['done_ratio'] Chris@1464: end Chris@1464: end Chris@1464: Chris@0: def self.openid? Chris@0: Object.const_defined?(:OpenID) && self[:openid].to_i > 0 Chris@0: end Chris@441: Chris@0: # Checks if settings have changed since the values were read Chris@0: # and clears the cache hash if it's the case Chris@0: # Called once per request Chris@0: def self.check_cache Chris@0: settings_updated_on = Setting.maximum(:updated_on) Chris@0: if settings_updated_on && @cached_cleared_on <= settings_updated_on Chris@909: clear_cache Chris@0: end Chris@0: end Chris@1464: Chris@909: # Clears the settings cache Chris@909: def self.clear_cache Chris@909: @cached_settings.clear Chris@909: @cached_cleared_on = Time.now Chris@909: logger.info "Settings cache cleared." if logger Chris@909: end Chris@441: Chris@0: private Chris@0: # Returns the Setting instance for the setting named name Chris@0: # (record found in database or new record with default value) Chris@0: def self.find_or_default(name) Chris@0: name = name.to_s Chris@441: raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) Chris@0: setting = find_by_name(name) Chris@909: unless setting Chris@909: setting = new(:name => name) Chris@909: setting.value = @@available_settings[name]['default'] Chris@909: end Chris@909: setting Chris@0: end Chris@0: end