setting.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: settings
  5. #
  6. # id :bigint(8) not null, primary key
  7. # var :string not null
  8. # value :text
  9. # thing_type :string
  10. # created_at :datetime
  11. # updated_at :datetime
  12. # thing_id :bigint(8)
  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. 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. end
  33. def all_as_records
  34. vars = thing_scoped
  35. records = vars.index_by(&:var)
  36. default_settings.each do |key, default_value|
  37. next if records.key?(key) || default_value.is_a?(Hash)
  38. records[key] = Setting.new(var: key, value: default_value)
  39. end
  40. records
  41. end
  42. def default_settings
  43. return {} unless RailsSettings::Default.enabled?
  44. RailsSettings::Default.instance
  45. end
  46. end
  47. end