Class: Setting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/setting.rb

Overview

Redmine - project management software Copyright (C) 2006-2016 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.

Since:

  • 0.4.2

Constant Summary collapse

DATE_FORMATS =
[
    '%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'
]
TIME_FORMATS =
[
'%H:%M',
'%I:%M %p'
]
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
CP932
GB18030
GBK
ISCII91
EUC-KR
Big5
Big5-HKSCS
TIS-620)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Returns the value of the setting named name



108
109
110
111
# File 'app/models/setting.rb', line 108

def self.[](name)
  v = @cached_settings[name]
  v ? v : (@cached_settings[name] = find_or_default(name).value)
end

.[]=(name, v) ⇒ Object



113
114
115
116
117
118
119
# File 'app/models/setting.rb', line 113

def self.[]=(name, v)
  setting = find_or_default(name)
  setting.value = (v ? v : "")
  @cached_settings[name] = nil
  setting.save
  setting.value
end

.check_cacheObject

Checks if settings have changed since the values were read and clears the cache hash if it's the case Called once per request

Since:

  • 0.6.0



202
203
204
205
206
207
# File 'app/models/setting.rb', line 202

def self.check_cache
  settings_updated_on = Setting.maximum(:updated_on)
  if settings_updated_on && @cached_cleared_on <= settings_updated_on
    clear_cache
  end
end

.clear_cacheObject

Clears the settings cache

Since:

  • 1.3.0



210
211
212
213
214
# File 'app/models/setting.rb', line 210

def self.clear_cache
  @cached_settings.clear
  @cached_cleared_on = Time.now
  logger.info "Settings cache cleared." if logger
end

.commit_update_keywords_arrayObject

Helper that returns a Hash with single update keywords as keys

Since:

  • 2.4.0



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/setting.rb', line 180

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)
    end
  end
  a
end

.commit_update_keywords_from_params(params) ⇒ Object

Returns a hash suitable for commit_update_keywords setting

Example: params = => ['fixes', 'closes'], :status_id => [“3”, “5”], :done_ratio => [“”, “100”] Setting.commit_update_keywords_from_params(params) # => [=> 'fixes', 'status_id' => “3”, => 'closes', 'status_id' => “5”, 'done_ratio' => “100”]

Since:

  • 2.4.0



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/setting.rb', line 158

def self.commit_update_keywords_from_params(params)
  s = []
  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?
      s << attributes.inject({}) {|h, a|
        value = params[a][i].to_s
        h[a.to_s] = value if value.present?
        h
      }.merge('keywords' => keywords)
    end
  end
  s
end

.define_plugin_setting(plugin) ⇒ Object

Since:

  • 3.0.0



216
217
218
219
220
221
# File 'app/models/setting.rb', line 216

def self.define_plugin_setting(plugin)
  if plugin.settings
    name = "plugin_#{plugin.id}"
    define_setting name, {'default' => plugin.settings[:default], 'serialized' => true}
  end
end

.define_setting(name, options = {}) ⇒ Object

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”

Since:

  • 3.0.0



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/models/setting.rb', line 226

def self.define_setting(name, options={})
  available_settings[name.to_s] = options

  src = <<-END_SRC
  def self.#{name}
    self[:#{name}]
  end

  def self.#{name}?
    self[:#{name}].to_i > 0
  end

  def self.#{name}=(value)
    self[:#{name}] = value
  end
END_SRC
  class_eval src, __FILE__, __LINE__
end

.load_available_settingsObject

Since:

  • 3.0.0



245
246
247
248
249
# File 'app/models/setting.rb', line 245

def self.load_available_settings
  YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
    define_setting name, options
  end
end

.load_plugin_settingsObject

Since:

  • 3.0.0



251
252
253
254
255
# File 'app/models/setting.rb', line 251

def self.load_plugin_settings
  Redmine::Plugin.all.each do |plugin|
    define_plugin_setting(plugin)
  end
end

.openid?Boolean

Returns:

  • (Boolean)

Since:

  • 0.9.0



195
196
197
# File 'app/models/setting.rb', line 195

def self.openid?
  Object.const_defined?(:OpenID) && self[:openid].to_i > 0
end

.per_page_options_arrayObject

Helper that returns an array based on per_page_options setting

Since:

  • 0.7.0



175
176
177
# File 'app/models/setting.rb', line 175

def self.per_page_options_array
  per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
end

.set_all_from_params(settings) ⇒ Object

Updates multiple settings from params and sends a security notification if needed

Since:

  • 3.3.0



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/setting.rb', line 122

def self.set_all_from_params(settings)
  settings = (settings || {}).dup.symbolize_keys
  changes = []
  settings.each do |name, value|
    previous_value = Setting[name]
    set_from_params name, value
    if available_settings[name.to_s]['security_notifications'] && Setting[name] != previous_value
      changes << name
    end
  end
  if changes.any?
    Mailer.security_settings_updated(changes)
  end
  true
end

.set_from_params(name, params) ⇒ Object

Sets a setting value from params

Since:

  • 2.4.0



139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/setting.rb', line 139

def self.set_from_params(name, params)
  params = params.dup
  params.delete_if {|v| v.blank? } if params.is_a?(Array)
  params.symbolize_keys! if params.is_a?(Hash)

  m = "#{name}_from_params"
  if respond_to? m
    self[name.to_sym] = send m, params
  else
    self[name.to_sym] = params
  end
end

Instance Method Details

#valueObject

Since:

  • 0.6.0



91
92
93
94
95
96
97
98
99
100
# File 'app/models/setting.rb', line 91

def value
  v = read_attribute(:value)
  # Unserialize serialized settings
  if available_settings[name]['serialized'] && v.is_a?(String)
    v = YAML::load(v)
    v = force_utf8_strings(v)
  end
  v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
  v
end

#value=(v) ⇒ Object

Since:

  • 0.6.0



102
103
104
105
# File 'app/models/setting.rb', line 102

def value=(v)
  v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized']
  write_attribute(:value, v.to_s)
end