闭社主体 forked from https://github.com/tootsuite/mastodon
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.

50 lines
1.1 KiB

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