base.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # frozen_string_literal: true
  2. module RailsSettings
  3. class ProcetedKeyError < RuntimeError
  4. def initialize(key)
  5. super("Can't use #{key} as setting key.")
  6. end
  7. end
  8. class Base < ActiveRecord::Base
  9. SEPARATOR_REGEXP = /[\n,;]+/
  10. PROTECTED_KEYS = %w[var value]
  11. self.table_name = table_name_prefix + "settings"
  12. # get the value field, YAML decoded
  13. def value
  14. # rubocop:disable Security/YAMLLoad
  15. YAML.load(self[:value]) if self[:value].present?
  16. end
  17. # set the value field, YAML encoded
  18. def value=(new_value)
  19. self[:value] = new_value.to_yaml
  20. end
  21. def clear_cache
  22. self.class.clear_cache
  23. end
  24. class << self
  25. def clear_cache
  26. RequestCache.reset
  27. Rails.cache.delete(cache_key)
  28. end
  29. def field(key, **opts)
  30. _define_field(key, **opts)
  31. end
  32. alias_method :_rails_scope, :scope
  33. def scope(*args, &block)
  34. name = args.shift
  35. body = args.shift
  36. if body.respond_to?(:call)
  37. return _rails_scope(name, body, &block)
  38. end
  39. @scope = name.to_sym
  40. yield block
  41. @scope = nil
  42. end
  43. def get_field(key)
  44. @defined_fields.find { |field| field[:key] == key.to_s } || {}
  45. end
  46. def cache_prefix(&block)
  47. @cache_prefix = block
  48. end
  49. def cache_key
  50. key_parts = ["rails-settings-cached"]
  51. key_parts << @cache_prefix.call if @cache_prefix
  52. key_parts.join("/")
  53. end
  54. def keys
  55. @defined_fields.map { |field| field[:key] }
  56. end
  57. def editable_keys
  58. @defined_fields.reject { |field| field[:readonly] }.map { |field| field[:key] }
  59. end
  60. def readonly_keys
  61. @defined_fields.select { |field| field[:readonly] }.map { |field| field[:key] }
  62. end
  63. attr_reader :defined_fields
  64. private
  65. def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil, **opts)
  66. key = key.to_s
  67. raise ProcetedKeyError.new(key) if PROTECTED_KEYS.include?(key)
  68. @defined_fields ||= []
  69. @defined_fields << {
  70. scope: @scope,
  71. key: key,
  72. default: default,
  73. type: type || :string,
  74. readonly: readonly.nil? ? false : readonly,
  75. options: opts
  76. }
  77. if readonly
  78. define_singleton_method(key) do
  79. result = default.is_a?(Proc) ? default.call : default
  80. send(:_convert_string_to_typeof_value, type, result, separator: separator)
  81. end
  82. else
  83. define_singleton_method(key) do
  84. val = send(:_value_of, key)
  85. result = nil
  86. if !val.nil?
  87. result = val
  88. else
  89. result = default
  90. result = default.call if default.is_a?(Proc)
  91. end
  92. result = send(:_convert_string_to_typeof_value, type, result, separator: separator)
  93. result
  94. end
  95. define_singleton_method("#{key}=") do |value|
  96. var_name = key
  97. record = find_by(var: var_name) || new(var: var_name)
  98. value = send(:_convert_string_to_typeof_value, type, value, separator: separator)
  99. record.value = value
  100. record.save!
  101. value
  102. end
  103. if validates
  104. validates[:if] = proc { |item| item.var.to_s == key }
  105. send(:validates, key, **validates)
  106. define_method(:read_attribute_for_validation) do |_key|
  107. self.value
  108. end
  109. end
  110. end
  111. if type == :boolean
  112. define_singleton_method("#{key}?") do
  113. send(key)
  114. end
  115. end
  116. # delegate instance get method to class for support:
  117. # setting = Setting.new
  118. # setting.admin_emails
  119. define_method(key) do
  120. self.class.public_send(key)
  121. end
  122. end
  123. def _convert_string_to_typeof_value(type, value, separator: nil)
  124. return value unless [String, Integer, Float, BigDecimal].include?(value.class)
  125. case type
  126. when :boolean
  127. ["true", "1", 1, true].include?(value)
  128. when :array
  129. value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
  130. when :hash
  131. value = begin
  132. YAML.safe_load(value).to_h
  133. rescue
  134. {}
  135. end
  136. value.deep_stringify_keys!
  137. ActiveSupport::HashWithIndifferentAccess.new(value)
  138. when :integer
  139. value.to_i
  140. when :float
  141. value.to_f
  142. when :big_decimal
  143. value.to_d
  144. else
  145. value
  146. end
  147. end
  148. def _value_of(var_name)
  149. unless _table_exists?
  150. # Fallback to default value if table was not ready (before migrate)
  151. puts "WARNING: table: \"#{table_name}\" does not exist or not database connection, `#{name}.#{var_name}` fallback to returns the default value."
  152. return nil
  153. end
  154. _all_settings[var_name]
  155. end
  156. def _table_exists?
  157. table_exists?
  158. rescue
  159. false
  160. end
  161. def rails_initialized?
  162. Rails.application&.initialized?
  163. end
  164. def _all_settings
  165. RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
  166. vars = unscoped.select("var, value")
  167. result = {}
  168. vars.each { |record| result[record.var] = record.value }
  169. result.with_indifferent_access
  170. end
  171. end
  172. end
  173. end
  174. end