Class: Bible270::Setting

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

Overview

Runtime state an admin can change without a deploy: enrollment and the current run's shared calendar date.

Constant Summary collapse

ENROLLMENT_CLOSED_AT =
'enrollment_closed_at'
RUN_START_DATE =
'run_start_date'

Class Method Summary collapse

Class Method Details

.clear_run_start_date!Object



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

def self.clear_run_start_date!
  delete_key(RUN_START_DATE)
  true
end

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



77
78
79
80
# File 'app/models/bible270/setting.rb', line 77

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

.delete_key(key) ⇒ Object



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

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)


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

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



68
69
70
71
72
73
74
75
# File 'app/models/bible270/setting.rb', line 68

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



82
83
84
85
# File 'app/models/bible270/setting.rb', line 82

def self.open_enrollment!
  delete_key(ENROLLMENT_CLOSED_AT)
  true
end

.read(key) ⇒ Object



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

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

.run_start_dateObject

---- current run ------------------------------------------------------



31
32
33
# File 'app/models/bible270/setting.rb', line 31

def self.run_start_date
  run_start_date_override || Bible270.config.start_date
end

.run_start_date_overridden?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/bible270/setting.rb', line 41

def self.run_start_date_overridden?
  run_start_date_override.present?
end

.run_start_date_overrideObject



35
36
37
38
39
# File 'app/models/bible270/setting.rb', line 35

def self.run_start_date_override
  Plan.to_date(read(RUN_START_DATE))
rescue ActiveRecord::StatementInvalid
  nil
end

.set_run_start_date!(value) ⇒ Object



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

def self.set_run_start_date!(value)
  date = Plan.to_date(value)
  return false if date.nil?

  write(RUN_START_DATE, date.iso8601)
  true
end

.write(key, value) ⇒ Object



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

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