闭社主体 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.

58 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: settings
  5. #
  6. # id :integer not null, primary key
  7. # var :string not null
  8. # value :text
  9. # thing_type :string
  10. # thing_id :integer
  11. # created_at :datetime
  12. # updated_at :datetime
  13. #
  14. class Setting < RailsSettings::Base
  15. source Rails.root.join('config', 'settings.yml')
  16. def to_param
  17. var
  18. end
  19. class << self
  20. def [](key)
  21. return super(key) unless rails_initialized?
  22. val = Rails.cache.fetch(cache_key(key, nil)) do
  23. db_val = object(key)
  24. if db_val
  25. default_value = default_settings[key]
  26. return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
  27. db_val.value
  28. else
  29. default_settings[key]
  30. end
  31. end
  32. val
  33. end
  34. def all_as_records
  35. vars = thing_scoped
  36. records = vars.map { |r| [r.var, r] }.to_h
  37. default_settings.each do |key, default_value|
  38. next if records.key?(key) || default_value.is_a?(Hash)
  39. records[key] = Setting.new(var: key, value: default_value)
  40. end
  41. records
  42. end
  43. def default_settings
  44. return {} unless RailsSettings::Default.enabled?
  45. RailsSettings::Default.instance
  46. end
  47. end
  48. end