Class: Bible270::Setting

Inherits:
ApplicationRecord show all
Defined in:
app/models/bible270/setting.rb

Overview

Runtime state an admin can change without a deploy. Currently just whether this run of the plan is open to new readers.

Constant Summary collapse

ENROLLMENT_CLOSED_AT =
'enrollment_closed_at'

Class Method Summary collapse

Class Method Details

.close_enrollment!(at: Time.current) ⇒ Object



47
48
49
50
# File 'app/models/bible270/setting.rb', line 47

def self.close_enrollment!(at: Time.current)
  write(ENROLLMENT_CLOSED_AT, at.iso8601)
  true
end

.delete_key(key) ⇒ Object



24
25
26
# File 'app/models/bible270/setting.rb', line 24

def self.delete_key(key)
  where(key: key.to_s).delete_all
end

.enrollment_closed?Boolean

Closed either because an admin closed it, or because the app was configured to launch closed.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'app/models/bible270/setting.rb', line 32

def self.enrollment_closed?
  return true if read(ENROLLMENT_CLOSED_AT).present?

  Bible270.config.respond_to?(:enrollment_open) && Bible270.config.enrollment_open == false
end

.enrollment_closed_atObject



38
39
40
41
42
43
44
45
# File 'app/models/bible270/setting.rb', line 38

def self.enrollment_closed_at
  raw = read(ENROLLMENT_CLOSED_AT)
  return nil if raw.blank?

  Time.zone ? Time.zone.parse(raw) : Time.parse(raw)
rescue ArgumentError
  nil
end

.open_enrollment!Object



52
53
54
55
# File 'app/models/bible270/setting.rb', line 52

def self.open_enrollment!
  delete_key(ENROLLMENT_CLOSED_AT)
  true
end

.read(key) ⇒ Object



13
14
15
# File 'app/models/bible270/setting.rb', line 13

def self.read(key)
  find_by(key: key.to_s)&.value
end

.write(key, value) ⇒ Object



17
18
19
20
21
22
# File 'app/models/bible270/setting.rb', line 17

def self.write(key, value)
  record = find_or_initialize_by(key: key.to_s)
  record.value = value
  record.save!
  value
end