You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class Setting < RailsSettings::Base
  3. source Rails.root.join('config/settings.yml')
  4. def to_param
  5. var
  6. end
  7. class << self
  8. def [](key)
  9. return super(key) unless rails_initialized?
  10. val = Rails.cache.fetch(cache_key(key, @object)) do
  11. db_val = object(key)
  12. if db_val
  13. default_value = default_settings[key]
  14. return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
  15. db_val.value
  16. else
  17. default_settings[key]
  18. end
  19. end
  20. val
  21. end
  22. def all_as_records
  23. vars = thing_scoped
  24. records = vars.map { |r| [r.var, r] }.to_h
  25. default_settings.each do |key, default_value|
  26. next if records.key?(key) || default_value.is_a?(Hash)
  27. records[key] = Setting.new(var: key, value: default_value)
  28. end
  29. records
  30. end
  31. private
  32. def default_settings
  33. return {} unless RailsSettings::Default.enabled?
  34. RailsSettings::Default.instance
  35. end
  36. end
  37. end