setting.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Setting < ApplicationModel
  3. store :options
  4. store :state
  5. store :state_initial
  6. before_create :state_check, :set_initial
  7. before_update :state_check
  8. after_create :delete_cache
  9. after_update :delete_cache
  10. @@current = {} # rubocop:disable Style/ClassVars
  11. def self.load
  12. # check if config is already generated
  13. return @@current[:settings_config] if @@current[:settings_config]
  14. # read all config settings
  15. config = {}
  16. Setting.select('name, state').order(:id).each { |setting|
  17. config[setting.name] = setting.state[:value]
  18. }
  19. # config lookups
  20. config.each { |key, value|
  21. next if value.class.to_s != 'String'
  22. config[key].gsub!( /\#\{config\.(.+?)\}/ ) {
  23. config[$1].to_s
  24. }
  25. }
  26. # store for class requests
  27. @@current[:settings_config] = config
  28. config
  29. end
  30. def self.set(name, value)
  31. setting = Setting.find_by( name: name )
  32. if !setting
  33. fail "Can't find config setting '#{name}'"
  34. end
  35. setting.state = { value: value }
  36. setting.save
  37. logger.info "Setting.set() name:#{name}, value:#{value.inspect}"
  38. end
  39. def self.get(name)
  40. load
  41. @@current[:settings_config][name]
  42. end
  43. private
  44. def delete_cache
  45. @@current[:settings_config] = nil
  46. end
  47. def set_initial
  48. self.state_initial = state
  49. end
  50. def state_check
  51. return if !(state || state == false)
  52. return if !( !state.respond_to?('has_key?') || !state.key?(:value) )
  53. self.state = { value: state }
  54. end
  55. end