setting.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # This file is derived from a fork of the `rails-settings-cached` gem available at
  15. # https://github.com/mastodon/rails-settings-cached/tree/v0.6.6-aliases-true, with
  16. # the original available at:
  17. # https://github.com/huacnlee/rails-settings-cached/tree/0.x
  18. # It is licensed as follows:
  19. # Copyright (c) 2006 Alex Wayne
  20. # Some additional features added 2009 by Georg Ledermann
  21. # Permission is hereby granted, free of charge, to any person obtaining
  22. # a copy of this software and associated documentation files (the
  23. # "Software"), to deal in the Software without restriction, including
  24. # without limitation the rights to use, copy, modify, merge, publish,
  25. # distribute, sublicense, and/or sell copies of the Software, and to
  26. # permit persons to whom the Software is furnished to do so, subject to
  27. # the following conditions:
  28. # The above copyright notice and this permission notice shall be
  29. # included in all copies or substantial portions of the Software.
  30. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
  33. # NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. class Setting < ApplicationRecord
  38. after_commit :rewrite_cache, on: %i(create update)
  39. after_commit :expire_cache, on: %i(destroy)
  40. # Settings are server-wide settings only, but they were previously
  41. # used for users too. This can be dropped later with a database
  42. # migration dropping any scoped setting.
  43. default_scope { where(thing_type: nil, thing_id: nil) }
  44. class << self
  45. # get or set a variable with the variable as the called method
  46. # rubocop:disable Style/MissingRespondToMissing
  47. def method_missing(method, *args)
  48. # set a value for a variable
  49. if method.end_with?('=')
  50. var_name = method.to_s.chomp('=')
  51. value = args.first
  52. self[var_name] = value
  53. else
  54. # retrieve a value
  55. self[method.to_s]
  56. end
  57. end
  58. # rubocop:enable Style/MissingRespondToMissing
  59. def cache_prefix_by_startup
  60. @cache_prefix_by_startup ||= Digest::MD5.hexdigest(default_settings.to_s)
  61. end
  62. def cache_key(var_name)
  63. "rails_settings_cached/#{cache_prefix_by_startup}/#{var_name}"
  64. end
  65. def [](key)
  66. Rails.cache.fetch(cache_key(key)) do
  67. db_val = find_by(var: key)
  68. db_val ? db_val.value : default_settings[key]
  69. end
  70. end
  71. # set a setting value by [] notation
  72. def []=(var_name, value)
  73. record = find_or_initialize_by(var: var_name.to_s)
  74. record.value = value
  75. record.save!
  76. end
  77. def default_settings
  78. return @default_settings if defined?(@default_settings)
  79. content = Rails.root.join('config', 'settings.yml').read
  80. hash = content.empty? ? {} : YAML.safe_load(ERB.new(content).result, aliases: true).to_hash
  81. @default_settings = (hash[Rails.env] || {}).freeze
  82. end
  83. end
  84. # get the value field, YAML decoded
  85. def value
  86. YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
  87. end
  88. # set the value field, YAML encoded
  89. def value=(new_value)
  90. self[:value] = new_value.to_yaml
  91. end
  92. def rewrite_cache
  93. Rails.cache.write(cache_key, value)
  94. end
  95. def expire_cache
  96. Rails.cache.delete(cache_key)
  97. end
  98. def cache_key
  99. self.class.cache_key(var)
  100. end
  101. def to_param
  102. var
  103. end
  104. end