Class: SiteSetting

Inherits:
ApplicationRecord show all
Defined in:
/var/apps/qpixel/app/models/site_setting.rb

Overview

Represents a site setting. Site settings control the operation and display of most aspects of the site, such as reputation awards, additional content, and site constants such as name and logo.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules

Class Method Details

.[](name, community: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File '/var/apps/qpixel/app/models/site_setting.rb', line 14

def self.[](name, community: nil)
  key = "SiteSettings/#{community.present? ? community.id : RequestContext.community_id}/#{name}"
  cached = Rails.cache.fetch key, include_community: false do
    SiteSetting.applied_setting(name, community: community)&.typed
  end

  if cached.nil?
    Rails.cache.delete key, include_community: false
    value = SiteSetting.applied_setting(name, community: community)&.typed
    Rails.cache.write key, value, include_community: false
    value
  else
    cached
  end
end

.[]=(name, value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File '/var/apps/qpixel/app/models/site_setting.rb', line 30

def self.[]=(name, value)
  key = "SiteSettings/#{RequestContext.community_id}/#{name}"

  setting = applied_setting(name)

  typed_value = SettingConverter.new(value).send("as_#{setting.value_type.downcase}")

  setting.update(value: typed_value)

  Rails.cache.write key, typed_value, include_community: false
end

.all_communities(name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File '/var/apps/qpixel/app/models/site_setting.rb', line 79

def self.all_communities(name)
  communities = Community.all
  keys = (communities.map { |c| [c.id, "SiteSetting/#{c.id}/#{name}"] } + [[nil, "SiteSetting//#{name}"]]).to_h
  cached = Rails.cache.read_multi(*keys.values, include_community: false)
  missing = keys.reject { |_k, v| cached.include?(v) }.map { |k, _v| k }
  settings = if missing.empty?
               {}
             else
               SiteSetting.where(name: name, community_id: missing).to_h { |s| [s.community_id, s] }
             end
  Rails.cache.write_multi(missing.to_h { |cid| [keys[cid], settings[cid]&.typed] }, include_community: false)
  communities.to_h do |c|
    [
      c.id,
      if cached.include?(keys[c.id])
        cached[keys[c.id]]
      elsif settings.include?(c.id)
        settings[c.id]&.typed
      elsif cached.include?(keys[nil])
        cached[keys[nil]]
      elsif settings.include?(nil)
        settings[nil]&.typed
      end
    ]
  end
end

.applied_setting(name, community: nil) ⇒ Object



74
75
76
77
# File '/var/apps/qpixel/app/models/site_setting.rb', line 74

def self.applied_setting(name, community: nil)
  SiteSetting.for_community_id(community.present? ? community.id : RequestContext.community_id)
             .or(global).where(name: name).priority_order.first
end

.exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File '/var/apps/qpixel/app/models/site_setting.rb', line 42

def self.exist?(name)
  Rails.cache.exist?("SiteSettings/#{RequestContext.community_id}/#{name}", include_community: false) ||
    SiteSetting.where(name: name).any?
end

.typed(setting) ⇒ Object



70
71
72
# File '/var/apps/qpixel/app/models/site_setting.rb', line 70

def self.typed(setting)
  SettingConverter.new(setting.value).send("as_#{setting.value_type.downcase}")
end

Instance Method Details

#global?Boolean

Is the setting global?

Returns:

  • (Boolean)

    check result



49
50
51
# File '/var/apps/qpixel/app/models/site_setting.rb', line 49

def global?
  community_id.nil?
end

#numeric?Boolean

Is the setting numeric-valued?

Returns:

  • (Boolean)

    check result



62
63
64
# File '/var/apps/qpixel/app/models/site_setting.rb', line 62

def numeric?
  float? || integer?
end

#typedObject



66
67
68
# File '/var/apps/qpixel/app/models/site_setting.rb', line 66

def typed
  SettingConverter.new(value).send("as_#{value_type.downcase}")
end