setting.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = {}
  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\.(.+?)\}/ ) { |s|
  23. s = config[$1].to_s
  24. }
  25. }
  26. # store for class requests
  27. @@current[:settings_config] = config
  28. return config
  29. end
  30. def self.set(name, value)
  31. setting = Setting.where( :name => name ).first
  32. if !setting
  33. raise "Can't find config setting '#{name}'"
  34. end
  35. setting.state = { :value => value }
  36. setting.save
  37. end
  38. def self.get(name)
  39. self.load
  40. return @@current[:settings_config][name]
  41. end
  42. private
  43. def delete_cache
  44. @@current[:settings_config] = nil
  45. end
  46. def set_initial
  47. self.state_initial = self.state
  48. end
  49. def state_check
  50. if self.state || self.state == false
  51. if !self.state.respond_to?('has_key?') || !self.state.has_key?(:value)
  52. self.state = { :value => self.state }
  53. end
  54. end
  55. end
  56. end