trabajo-afectivo/app/models/setting.rb

86 lines
1.9 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Setting < ApplicationModel
store :options
store :state
store :state_initial
2015-07-12 02:12:27 +00:00
store :preferences
2013-01-24 00:37:27 +00:00
before_create :state_check, :set_initial
before_update :state_check
after_create :delete_cache
after_update :delete_cache
2012-04-10 14:06:46 +00:00
@@current = {} # rubocop:disable Style/ClassVars
2012-04-10 14:06:46 +00:00
def self.load
2012-07-28 16:37:18 +00:00
# check if config is already generated
return @@current[:settings_config] if @@current[:settings_config]
2012-07-28 16:37:18 +00:00
# read all config settings
2012-04-10 14:06:46 +00:00
config = {}
Setting.select('name, state').order(:id).each { |setting|
config[setting.name] = setting.state[:value]
}
2012-07-28 16:37:18 +00:00
# config lookups
config.each { |key, value|
next if value.class.to_s != 'String'
config[key].gsub!( /\#\{config\.(.+?)\}/ ) {
config[$1].to_s
}
}
2012-07-28 16:37:18 +00:00
# store for class requests
@@current[:settings_config] = config
config
2012-04-10 14:06:46 +00:00
end
def self.set(name, value)
setting = Setting.find_by( name: name )
if !setting
fail "Can't find config setting '#{name}'"
end
setting.state = { value: value }
setting.save
2014-10-26 12:17:00 +00:00
logger.info "Setting.set() name:#{name}, value:#{value.inspect}"
end
2012-04-10 14:06:46 +00:00
def self.get(name)
load
@@current[:settings_config][name]
2012-04-10 14:06:46 +00:00
end
2012-07-28 16:37:18 +00:00
def self.reset(name)
setting = Setting.find_by( name: name )
if !setting
fail "Can't find config setting '#{name}'"
end
setting.state = setting.state_initial
setting.save
logger.info "Setting.reset() name:#{name}, value:#{setting.state.inspect}"
load
@@current[:settings_config][name]
end
2012-04-10 14:06:46 +00:00
private
def delete_cache
@@current[:settings_config] = nil
end
def set_initial
self.state_initial = state
end
def state_check
return if !(state || state == false)
return if !( !state.respond_to?('has_key?') || !state.key?(:value) )
self.state = { value: state }
end
2012-04-10 14:06:46 +00:00
end