Class: LeanCms::Setting

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/lean_cms/setting.rb

Class Method Summary collapse

Class Method Details

.business_hoursObject



127
128
129
# File 'app/models/lean_cms/setting.rb', line 127

def business_hours
  get_json('business_hours', { 'hours' => [], 'note' => '' })
end

.content_lock_infoObject



148
149
150
151
152
153
154
# File 'app/models/lean_cms/setting.rb', line 148

def content_lock_info
  return nil unless content_locked?
  {
    locked_at: get('content_locked_at'),
    reason: get('content_locked_reason', 'Content sync in progress')
  }
end

.content_locked?Boolean

Content lock methods for sync workflow

Returns:

  • (Boolean)


132
133
134
# File 'app/models/lean_cms/setting.rb', line 132

def content_locked?
  enabled?('content_locked')
end

.enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/lean_cms/setting.rb', line 55

def enabled?(key)
  get(key, 'false') == 'true'
end

.get(key, default = nil) ⇒ Object



10
11
12
13
14
15
# File 'app/models/lean_cms/setting.rb', line 10

def get(key, default = nil)
  Rails.cache.fetch("lean_cms_setting/#{key}", expires_in: 1.hour) do
    setting = find_by(key: key)
    setting&.value || default
  end
end

.get_json(key, default = {}) ⇒ Object

JSON storage helpers



60
61
62
63
64
65
66
# File 'app/models/lean_cms/setting.rb', line 60

def get_json(key, default = {})
  raw = get(key)
  return default if raw.blank?
  JSON.parse(raw)
rescue JSON::ParserError
  default
end

.get_uncached(key, default = nil) ⇒ Object

Bypass cache - use for settings that must take effect immediately (e.g. cookie consent)



50
51
52
53
# File 'app/models/lean_cms/setting.rb', line 50

def get_uncached(key, default = nil)
  setting = find_by(key: key)
  setting&.value || default
end

.lock_content!(reason = nil) ⇒ Object



136
137
138
139
140
# File 'app/models/lean_cms/setting.rb', line 136

def lock_content!(reason = nil)
  set('content_locked', 'true')
  set('content_locked_at', Time.current.iso8601)
  set('content_locked_reason', reason) if reason
end

.remove_site_favicon!Object



44
45
46
47
# File 'app/models/lean_cms/setting.rb', line 44

def remove_site_favicon!
  setting = find_by(key: "site_favicon")
  setting&.file&.purge if setting&.file&.attached?
end

.set(key, value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/models/lean_cms/setting.rb', line 17

def set(key, value)
  setting = find_or_initialize_by(key: key)
  setting.value = value.to_s
  PaperTrail.request(whodunnit: LeanCms::Current.user&.id&.to_s) do
    setting.save!
  end
  Rails.cache.delete("lean_cms_setting/#{key}")
  value
end

.set_json(key, value) ⇒ Object



68
69
70
# File 'app/models/lean_cms/setting.rb', line 68

def set_json(key, value)
  set(key, value.to_json)
end

.site_addressObject

Returns formatted address string for display



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/lean_cms/setting.rb', line 86

def site_address
  data = site_address_data
  parts = []
  parts << data['street1'] if data['street1'].present?
  parts << data['street2'] if data['street2'].present?

  city_state_zip = []
  city_state_zip << data['city'] if data['city'].present?
  city_state_zip << data['state'] if data['state'].present?
  city_state_zip << data['zip'] if data['zip'].present?

  parts << city_state_zip.join(', ') if city_state_zip.any?
  parts.join("\n")
end

.site_address_dataObject

Returns structured address data as hash



75
76
77
78
79
80
81
82
83
# File 'app/models/lean_cms/setting.rb', line 75

def site_address_data
  get_json('site_address', {
    'street1' => '',
    'street2' => '',
    'city' => '',
    'state' => '',
    'zip' => ''
  })
end

.site_address_single_lineObject

Returns single-line formatted address



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/lean_cms/setting.rb', line 102

def site_address_single_line
  data = site_address_data
  parts = []
  parts << data['street1'] if data['street1'].present?
  parts << data['street2'] if data['street2'].present?

  city_state = []
  city_state << data['city'] if data['city'].present?
  city_state << data['state'] if data['state'].present?

  location = city_state.join(', ')
  location += " #{data['zip']}" if data['zip'].present?

  parts << location if location.present?
  parts.join(', ')
end

.site_emailObject



123
124
125
# File 'app/models/lean_cms/setting.rb', line 123

def site_email
  get('site_email', '')
end

.site_favicon_urlObject

Returns the ActiveStorage URL for the uploaded favicon (host-configured override), or nil if no favicon has been uploaded. Callers should fall back to the gem’s default sloth favicon when this returns nil.



30
31
32
33
34
# File 'app/models/lean_cms/setting.rb', line 30

def site_favicon_url
  setting = find_by(key: "site_favicon")
  return nil unless setting&.file&.attached?
  Rails.application.routes.url_helpers.rails_blob_path(setting.file, only_path: true)
end

.site_phoneObject



119
120
121
# File 'app/models/lean_cms/setting.rb', line 119

def site_phone
  get('site_phone', '')
end

.unlock_content!Object



142
143
144
145
146
# File 'app/models/lean_cms/setting.rb', line 142

def unlock_content!
  set('content_locked', 'false')
  Rails.cache.delete("lean_cms_setting/content_locked_at")
  Rails.cache.delete("lean_cms_setting/content_locked_reason")
end

.update_site_favicon!(file_param) ⇒ Object

Attaches a new favicon file (e.g. from params).



37
38
39
40
41
42
# File 'app/models/lean_cms/setting.rb', line 37

def update_site_favicon!(file_param)
  return if file_param.blank?
  setting = find_or_create_by!(key: "site_favicon") { |s| s.value = "uploaded" }
  setting.file.purge if setting.file.attached?
  setting.file.attach(file_param)
end