trabajo-afectivo/app/models/setting.rb

184 lines
4.9 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Setting < ApplicationModel
store :options
2015-09-25 14:37:55 +00:00
store :state_current
store :state_initial
2015-07-12 02:12:27 +00:00
store :preferences
before_create :state_check, :set_initial, :check_broadcast
before_update :state_check, :check_broadcast
after_create :reset_change_id
after_update :reset_change_id
2012-04-10 14:06:46 +00:00
2015-09-25 14:37:55 +00:00
attr_accessor :state
@@current = {} # rubocop:disable Style/ClassVars
@@raw = {} # rubocop:disable Style/ClassVars
@@change_id = nil # rubocop:disable Style/ClassVars
@@last_changed_at = nil # rubocop:disable Style/ClassVars
@@lookup_at = nil # rubocop:disable Style/ClassVars
@@lookup_timeout = if ENV['ZAMMAD_SETTING_TTL'] # rubocop:disable Style/ClassVars
ENV['ZAMMAD_SETTING_TTL'].to_i.seconds
else
15.seconds
end
=begin
2012-07-28 16:37:18 +00:00
set config setting
2012-07-28 16:37:18 +00:00
Setting.set('some_config_name', some_value)
=end
2012-04-10 14:06:46 +00:00
def self.set(name, value)
2016-03-25 09:27:52 +00:00
setting = Setting.find_by(name: name)
if !setting
2016-03-01 14:26:46 +00:00
raise "Can't find config setting '#{name}'"
end
2015-09-25 18:35:36 +00:00
setting.state_current = { value: value }
setting.save!
logger.info "Setting.set(#{name}, #{value.inspect})"
end
=begin
get config setting
value = Setting.get('some_config_name')
=end
2012-04-10 14:06:46 +00:00
def self.get(name)
load
@@current[name]
2012-04-10 14:06:46 +00:00
end
2012-07-28 16:37:18 +00:00
=begin
reset config setting to default
Setting.reset('some_config_name')
2017-06-14 15:25:45 +00:00
Setting.reset('some_config_name', force) # true|false - force it false per default
=end
2017-06-14 15:25:45 +00:00
def self.reset(name, force = false)
2016-03-25 09:27:52 +00:00
setting = Setting.find_by(name: name)
if !setting
2016-03-01 14:26:46 +00:00
raise "Can't find config setting '#{name}'"
end
2017-06-14 15:25:45 +00:00
return true if !force && setting.state_current == setting.state_initial
2015-09-25 18:35:36 +00:00
setting.state_current = setting.state_initial
setting.save!
2015-09-25 18:35:36 +00:00
logger.info "Setting.reset(#{name}, #{setting.state_current.inspect})"
end
2016-01-13 19:45:51 +00:00
=begin
reload config settings
Setting.reload
=end
def self.reload
@@last_changed_at = nil # rubocop:disable Style/ClassVars
2016-01-13 19:45:51 +00:00
load(true)
end
2012-04-10 14:06:46 +00:00
private
# load values and cache them
2016-01-13 19:45:51 +00:00
def self.load(force = false)
# check if config is already generated
return false if !force && @@current.present? && cache_valid?
# read all or only changed since last read
latest = Setting.order(updated_at: :desc).limit(1).pluck(:updated_at)
settings = if @@last_changed_at && @@current.present?
Setting.where('updated_at > ?', @@last_changed_at).order(:id).pluck(:name, :state_current)
else
Setting.order(:id).pluck(:name, :state_current)
end
if latest
@@last_changed_at = latest[0] # rubocop:disable Style/ClassVars
end
if settings.present?
settings.each { |setting|
@@raw[setting[0]] = setting[1]['value']
}
@@raw.each { |key, value|
if value.class != String
@@current[key] = value
next
end
@@current[key] = value.gsub(/\#\{config\.(.+?)\}/) {
@@raw[$1].to_s
}
}
end
@@change_id = Cache.get('Setting::ChangeId') # rubocop:disable Style/ClassVars
@@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
true
end
private_class_method :load
# set initial value in state_initial
def set_initial
2015-09-25 18:35:36 +00:00
self.state_initial = state_current
end
def reset_change_id
@@current[name] = state_current[:value]
change_id = rand(999_999_999).to_s
logger.debug "Setting.reset_change_id: set new cache, #{change_id}"
Cache.write('Setting::ChangeId', change_id, { expires_in: 24.hours })
@@lookup_at = nil # rubocop:disable Style/ClassVars
end
# check if cache is still valid
def self.cache_valid?
if @@lookup_at && @@lookup_at > Time.zone.now - @@lookup_timeout
#logger.debug "Setting.cache_valid?: cache_id has been set within last #{@@lookup_timeout} seconds"
return true
end
change_id = Cache.get('Setting::ChangeId')
if change_id == @@change_id
@@lookup_at = Time.zone.now # rubocop:disable Style/ClassVars
#logger.debug "Setting.cache_valid?: cache still valid, #{@@change_id}/#{change_id}"
return true
end
#logger.debug "Setting.cache_valid?: cache has changed, #{@@change_id}/#{change_id}"
false
end
private_class_method :cache_valid?
2015-09-25 21:35:17 +00:00
# convert state into hash to be able to store it as store
def state_check
2015-09-25 21:35:17 +00:00
return if !state
2015-09-25 14:37:55 +00:00
return if state && state.respond_to?('has_key?') && state.key?(:value)
2015-09-25 18:35:36 +00:00
self.state_current = { value: state }
end
# notify clients about public config changes
def check_broadcast
return if frontend != true
value = state_current
if state_current.key?(:value)
value = state_current[:value]
end
Sessions.broadcast(
{
event: 'config_update',
data: { name: name, value: value }
},
'public'
)
end
2012-04-10 14:06:46 +00:00
end