bible270 1.16.0 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +60 -32
- data/README.md +129 -58
- data/Rakefile +3 -2
- data/app/controllers/bible270/admin_controller.rb +152 -33
- data/app/controllers/bible270/admin_roles_controller.rb +50 -0
- data/app/controllers/bible270/admin_scripture_frames_controller.rb +89 -0
- data/app/controllers/bible270/application_controller.rb +18 -2
- data/app/controllers/bible270/checkoffs_controller.rb +38 -9
- data/app/controllers/bible270/readers_controller.rb +1 -1
- data/app/controllers/bible270/scripture_frames_controller.rb +16 -0
- data/app/helpers/bible270/plan_helper.rb +4 -4
- data/app/mailers/bible270/notice_mailer.rb +2 -2
- data/app/models/bible270/checkoff.rb +69 -3
- data/app/models/bible270/participation.rb +71 -0
- data/app/models/bible270/plan_run.rb +121 -0
- data/app/models/bible270/reader.rb +142 -24
- data/app/models/bible270/scripture_frame.rb +48 -0
- data/app/models/bible270/setting.rb +13 -4
- data/app/views/bible270/admin/_reader_list.html.erb +55 -0
- data/app/views/bible270/admin/_scripture_frames.html.erb +34 -0
- data/app/views/bible270/admin/_scripture_text.html.erb +11 -0
- data/app/views/bible270/admin/index.html.erb +64 -49
- data/app/views/bible270/admin/show.html.erb +117 -54
- data/app/views/bible270/admin_scripture_frames/_form.html.erb +72 -0
- data/app/views/bible270/admin_scripture_frames/edit.html.erb +4 -0
- data/app/views/bible270/admin_scripture_frames/new.html.erb +4 -0
- data/app/views/bible270/days/_day_progress.html.erb +2 -2
- data/app/views/bible270/days/_reading.html.erb +5 -4
- data/app/views/bible270/profiles/edit.html.erb +14 -12
- data/app/views/bible270/readers/show.html.erb +1 -1
- data/app/views/bible270/shared/_admin_confirmations.html.erb +16 -0
- data/app/views/bible270/shared/_admin_reader_list_ui.html.erb +49 -0
- data/app/views/bible270/shared/_day_completion.html.erb +13 -0
- data/app/views/bible270/shared/_day_completion_ui.html.erb +152 -0
- data/app/views/bible270/shared/_day_index.html.erb +2 -1
- data/app/views/bible270/shared/_header.html.erb +12 -4
- data/app/views/bible270/shared/_interaction_ui.html.erb +2 -62
- data/app/views/bible270/shared/_scripture_frame.html.erb +14 -0
- data/app/views/bible270/shared/_scripture_frame_ui.html.erb +247 -0
- data/app/views/bible270/shared/_scripture_library_ui.html.erb +34 -0
- data/app/views/bible270/shared/_styles.html.erb +141 -10
- data/app/views/layouts/bible270/application.html.erb +8 -0
- data/config/routes.rb +9 -2
- data/config/scripture_frames.yml +134 -0
- data/db/migrate/20260101000024_create_bible270_plan_runs.rb +60 -0
- data/db/migrate/20260101000025_add_admin_to_bible270_readers.rb +7 -0
- data/db/migrate/20260101000026_add_revert_to_bible270_plan_runs.rb +8 -0
- data/db/migrate/20260101000027_create_bible270_scripture_frames.rb +16 -0
- data/lib/bible270/configuration.rb +17 -4
- data/lib/bible270/migration_reconciler.rb +91 -8
- data/lib/bible270/reader_consistency.rb +8 -4
- data/lib/bible270/reader_progress_report.rb +2 -1
- data/lib/bible270/scripture_frames.rb +226 -0
- data/lib/bible270/version.rb +1 -1
- data/lib/bible270.rb +1 -0
- data/lib/generators/bible270/install/templates/bible270.rb.tt +8 -1
- metadata +26 -1
|
@@ -8,18 +8,52 @@ module Bible270
|
|
|
8
8
|
RecentActivity = Struct.new(:occurred_at, :day, :track, :part, :day_completed?, keyword_init: true)
|
|
9
9
|
|
|
10
10
|
belongs_to :reader, class_name: 'Bible270::Reader'
|
|
11
|
+
belongs_to :participation, class_name: 'Bible270::Participation', inverse_of: :checkoffs
|
|
12
|
+
|
|
13
|
+
before_validation :assign_participation
|
|
14
|
+
validate :participation_matches_reader
|
|
15
|
+
validate :participation_is_writable
|
|
16
|
+
around_save :with_writable_participation
|
|
17
|
+
around_destroy :with_writable_participation
|
|
11
18
|
|
|
12
19
|
validates :day, inclusion: { in: 1..Plan::DAYS }
|
|
13
20
|
validates :track, inclusion: { in: Plan::TRACKS.keys }
|
|
14
|
-
validates :track, uniqueness: { scope: %i[
|
|
21
|
+
validates :track, uniqueness: { scope: %i[participation_id day part] }
|
|
15
22
|
validates :part, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
|
16
23
|
validate :track_present_on_day
|
|
17
24
|
validate :part_within_reading
|
|
18
25
|
|
|
26
|
+
# Legacy callers (including historical chapter backfills) use insert_all
|
|
27
|
+
# without a membership. Resolve those IDs in bulk without creating membership
|
|
28
|
+
# or reactivating anyone. Explicit participation IDs remain a trusted bulk API.
|
|
29
|
+
def self.insert_all(attributes, **)
|
|
30
|
+
return super unless column_names.include?('participation_id') && attributes.any? { |row| !row.key?(:participation_id) }
|
|
31
|
+
|
|
32
|
+
transaction do
|
|
33
|
+
ids = attributes.filter_map { |row| row[:reader_id] unless row.key?(:participation_id) }.uniq
|
|
34
|
+
memberships = Participation.chosen.where(reader_id: ids).index_by(&:reader_id)
|
|
35
|
+
# Use the same locks as archive/restart, in a stable order for batches.
|
|
36
|
+
# Keep them until after super, not merely until validation has finished.
|
|
37
|
+
runs = PlanRun.where(id: memberships.values.map(&:plan_run_id).uniq).order(:id).lock.index_by(&:id)
|
|
38
|
+
memberships.each_value do |membership|
|
|
39
|
+
membership.reload
|
|
40
|
+
membership.plan_run = runs.fetch(membership.plan_run_id)
|
|
41
|
+
membership.ensure_writable!
|
|
42
|
+
end
|
|
43
|
+
attributes = attributes.map do |row|
|
|
44
|
+
next row if row.key?(:participation_id)
|
|
45
|
+
|
|
46
|
+
row.merge(participation_id: memberships.fetch(row[:reader_id]).id)
|
|
47
|
+
end
|
|
48
|
+
super
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
19
52
|
# The most recently created check-off still present for each reader. This uses
|
|
20
53
|
# three bulk queries regardless of the number of readers displayed in Admin.
|
|
21
54
|
def self.recent_activity_by_reader(reader_ids)
|
|
22
|
-
latest_ids = where(reader_id: reader_ids
|
|
55
|
+
latest_ids = where(reader_id: reader_ids, participation_id: Participation.chosen.select(:id))
|
|
56
|
+
.group(:reader_id).maximum(:id).values
|
|
23
57
|
latest_checkoffs = where(id: latest_ids).index_by(&:reader_id)
|
|
24
58
|
day_counts = recent_activity_day_counts(latest_checkoffs.values)
|
|
25
59
|
|
|
@@ -37,12 +71,44 @@ module Bible270
|
|
|
37
71
|
def self.recent_activity_day_counts(checkoffs)
|
|
38
72
|
return {} if checkoffs.empty?
|
|
39
73
|
|
|
40
|
-
where(
|
|
74
|
+
where(participation_id: checkoffs.map(&:participation_id), day: checkoffs.map(&:day))
|
|
75
|
+
.group(:reader_id, :day).count
|
|
41
76
|
end
|
|
42
77
|
private_class_method :recent_activity_day_counts
|
|
43
78
|
|
|
44
79
|
private
|
|
45
80
|
|
|
81
|
+
def assign_participation
|
|
82
|
+
self.participation ||= reader&.participation
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def participation_matches_reader
|
|
86
|
+
if persisted? && (will_save_change_to_reader_id? || will_save_change_to_participation_id?)
|
|
87
|
+
errors.add(:participation, 'cannot change the owner of existing progress')
|
|
88
|
+
end
|
|
89
|
+
return unless participation && participation.reader_id != reader_id
|
|
90
|
+
|
|
91
|
+
errors.add(:participation, 'must belong to the same reader')
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def participation_is_writable
|
|
95
|
+
return unless participation
|
|
96
|
+
return if Participation.active.where(id: participation.id).exists?
|
|
97
|
+
|
|
98
|
+
errors.add(:participation, 'has archived progress and is read-only')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Share the run lock with restart/archive, including the actual write, not
|
|
102
|
+
# just its validation. Cascading identity deletion intentionally removes history.
|
|
103
|
+
def with_writable_participation
|
|
104
|
+
return yield if destroyed_by_association
|
|
105
|
+
|
|
106
|
+
participation.plan_run.with_lock do
|
|
107
|
+
participation.reload.ensure_writable!
|
|
108
|
+
yield
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
46
112
|
def track_present_on_day
|
|
47
113
|
return if day.nil? || track.nil?
|
|
48
114
|
return if Plan.present_tracks(day).include?(track)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bible270
|
|
4
|
+
class Participation < ApplicationRecord
|
|
5
|
+
self.table_name = 'bible270_participations'
|
|
6
|
+
|
|
7
|
+
class ArchivedProgress < StandardError; end
|
|
8
|
+
|
|
9
|
+
belongs_to :reader, class_name: 'Bible270::Reader', inverse_of: :participations
|
|
10
|
+
belongs_to :plan_run, class_name: 'Bible270::PlanRun', inverse_of: :participations
|
|
11
|
+
has_many :checkoffs, class_name: 'Bible270::Checkoff', dependent: :destroy, inverse_of: :participation
|
|
12
|
+
|
|
13
|
+
validates :reader_id, uniqueness: { scope: :plan_run_id }
|
|
14
|
+
validates :joined_at, presence: true
|
|
15
|
+
|
|
16
|
+
scope :active, -> { joins(:plan_run).where(active: true, bible270_plan_runs: { current: true }) }
|
|
17
|
+
# Prefer the latest non-reverted membership, so undoing a restart restores
|
|
18
|
+
# both current readers and earlier alumni. Readers who joined only reverted
|
|
19
|
+
# runs can still view their latest archived progress. Keep this preloadable.
|
|
20
|
+
scope :chosen, -> {
|
|
21
|
+
joins(:plan_run).where(<<~SQL.squish)
|
|
22
|
+
NOT EXISTS (
|
|
23
|
+
SELECT 1 FROM bible270_participations newer
|
|
24
|
+
INNER JOIN bible270_plan_runs newer_run ON newer_run.id = newer.plan_run_id
|
|
25
|
+
WHERE newer.reader_id = bible270_participations.reader_id
|
|
26
|
+
AND (
|
|
27
|
+
(newer_run.reverted_at IS NULL AND bible270_plan_runs.reverted_at IS NOT NULL)
|
|
28
|
+
OR (
|
|
29
|
+
newer.plan_run_id > bible270_participations.plan_run_id
|
|
30
|
+
AND (newer_run.reverted_at IS NULL OR bible270_plan_runs.reverted_at IS NOT NULL)
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
SQL
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
def alumni? = !active?
|
|
38
|
+
|
|
39
|
+
def effective_start_date
|
|
40
|
+
return frozen_start_date if alumni?
|
|
41
|
+
|
|
42
|
+
if Bible270.config.allow_reader_start_date && started_on
|
|
43
|
+
started_on
|
|
44
|
+
else
|
|
45
|
+
plan_run.effective_start_date
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Archive is idempotent: never replace an earlier alumni snapshot at restart.
|
|
50
|
+
# Reader#archive! and PlanRun.restart! hold the run lock when calling this.
|
|
51
|
+
def archive!(on: Bible270.today, shared_start_date: Setting.run_start_date)
|
|
52
|
+
return self if alumni?
|
|
53
|
+
|
|
54
|
+
date = if Bible270.config.allow_reader_start_date && reader.started_on
|
|
55
|
+
reader.started_on
|
|
56
|
+
else
|
|
57
|
+
shared_start_date
|
|
58
|
+
end
|
|
59
|
+
update!(active: false, archived_on: on, frozen_start_date: date, started_on: reader.started_on)
|
|
60
|
+
self
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def ensure_writable!
|
|
64
|
+
unless active? && plan_run.current?
|
|
65
|
+
raise ArchivedProgress, 'Archived progress is read-only; activate the reader in the current run first'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bible270
|
|
4
|
+
class PlanRun < ApplicationRecord
|
|
5
|
+
self.table_name = 'bible270_plan_runs'
|
|
6
|
+
|
|
7
|
+
class InvalidStartDate < ArgumentError; end
|
|
8
|
+
class StaleRun < StandardError; end
|
|
9
|
+
class CannotRevert < StandardError; end
|
|
10
|
+
|
|
11
|
+
RESTART_SETTING_KEYS = [Setting::RUN_START_DATE, Setting::ENROLLMENT_OPEN, Setting::ENROLLMENT_CLOSED_AT].freeze
|
|
12
|
+
|
|
13
|
+
has_many :participations, class_name: 'Bible270::Participation', inverse_of: :plan_run
|
|
14
|
+
|
|
15
|
+
# The unique current marker also makes first enrollment safe on an empty DB.
|
|
16
|
+
def self.current
|
|
17
|
+
find_by(current: true) || create_or_find_by!(current: true)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# expected_run_id is mandatory optimistic concurrency protection for the form.
|
|
21
|
+
# All membership transitions lock the run first, so enrollment cannot land in
|
|
22
|
+
# the old run halfway through this transaction.
|
|
23
|
+
def self.restart!(start_date:, expected_run_id:)
|
|
24
|
+
date = Plan.to_date(start_date)
|
|
25
|
+
raise InvalidStartDate, 'start_date must be a valid date' unless date
|
|
26
|
+
|
|
27
|
+
old_run = current
|
|
28
|
+
transaction do
|
|
29
|
+
old_run.lock!
|
|
30
|
+
unless old_run.current? && old_run.id.to_s == expected_run_id.to_s
|
|
31
|
+
raise StaleRun, 'The current run changed; reload before restarting'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Membership changes and admin role changes lock readers before their
|
|
35
|
+
# participations. Hold those locks while archiving and enrolling admins.
|
|
36
|
+
Reader.order(:id).lock.load
|
|
37
|
+
snapshot = {
|
|
38
|
+
'previous_run_id' => old_run.id,
|
|
39
|
+
'active_participation_ids' => old_run.participations.where(active: true).order(:id).pluck(:id),
|
|
40
|
+
'reader_start_dates' => Reader.order(:id).pluck(:id, :started_on).to_h.transform_values { |value| value&.iso8601 },
|
|
41
|
+
'settings' => Setting.where(key: RESTART_SETTING_KEYS).pluck(:key, :value).to_h
|
|
42
|
+
}
|
|
43
|
+
old_run.archive_current_memberships!
|
|
44
|
+
new_run = create!(current: true, start_date: date, restart_snapshot: snapshot)
|
|
45
|
+
Setting.set_run_start_date!(date)
|
|
46
|
+
Setting.open_enrollment!
|
|
47
|
+
activate_admins!
|
|
48
|
+
new_run
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Retain the abandoned run as read-only history rather than deleting work
|
|
53
|
+
# completed since the restart. Lock both runs in the same order as bulk writes.
|
|
54
|
+
def self.revert!(expected_run_id:)
|
|
55
|
+
run = find_by(current: true)
|
|
56
|
+
unless run && run.id.to_s == expected_run_id.to_s
|
|
57
|
+
raise StaleRun, 'The current run changed; reload before reverting'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
previous_id = run.restart_snapshot&.fetch('previous_run_id', nil)
|
|
61
|
+
transaction do
|
|
62
|
+
runs = where(id: [previous_id, run.id]).order(:id).lock.index_by(&:id)
|
|
63
|
+
run = runs.fetch(run.id)
|
|
64
|
+
unless run.current? && run.id.to_s == expected_run_id.to_s
|
|
65
|
+
raise StaleRun, 'The current run changed; reload before reverting'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
previous = runs[previous_id]
|
|
69
|
+
unless run.revertible? && previous && !previous.current? && !previous.reverted_at
|
|
70
|
+
raise CannotRevert, 'No saved restart is available to revert'
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
snapshot = run.restart_snapshot
|
|
74
|
+
# Deletion and profile changes lock the reader before membership rows.
|
|
75
|
+
# Match that order to avoid deadlocks while restoring personal dates.
|
|
76
|
+
Reader.where(id: snapshot.fetch('reader_start_dates').keys)
|
|
77
|
+
.or(Reader.where(id: run.participations.select(:reader_id))).order(:id).lock.load
|
|
78
|
+
run.archive_current_memberships!
|
|
79
|
+
run.update!(reverted_at: Time.current)
|
|
80
|
+
previous.update!(current: true, archived_at: nil)
|
|
81
|
+
previous.participations.where(id: snapshot.fetch('active_participation_ids')).update_all(
|
|
82
|
+
active: true, archived_on: nil, frozen_start_date: nil, updated_at: Time.current
|
|
83
|
+
)
|
|
84
|
+
# Bypass schedule callbacks: these are restored snapshots, not edits to
|
|
85
|
+
# whichever membership was selected before the transition.
|
|
86
|
+
snapshot.fetch('reader_start_dates').group_by { |_id, date| date }.each do |date, readers|
|
|
87
|
+
Reader.where(id: readers.map(&:first)).update_all(started_on: date)
|
|
88
|
+
end
|
|
89
|
+
settings = snapshot.fetch('settings')
|
|
90
|
+
RESTART_SETTING_KEYS.each do |key|
|
|
91
|
+
settings.key?(key) ? Setting.write(key, settings.fetch(key)) : Setting.delete_key(key)
|
|
92
|
+
end
|
|
93
|
+
previous
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def self.activate_admins!
|
|
98
|
+
Reader.preload(participation: :plan_run).find_each do |reader|
|
|
99
|
+
reader.activate! if Bible270.config.admin?(reader) && reader.alumni?
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
private_class_method :activate_admins!
|
|
103
|
+
|
|
104
|
+
def revertible?
|
|
105
|
+
current? && !reverted_at && restart_snapshot.is_a?(Hash) &&
|
|
106
|
+
self.class.where(id: restart_snapshot['previous_run_id'], current: nil, reverted_at: nil).exists?
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def archive_current_memberships!
|
|
110
|
+
shared_date = Setting.run_start_date
|
|
111
|
+
participations.where(active: true).includes(:reader).find_each do |membership|
|
|
112
|
+
membership.archive!(shared_start_date: shared_date)
|
|
113
|
+
end
|
|
114
|
+
update!(current: nil, start_date: shared_date, archived_at: Time.current)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def effective_start_date
|
|
118
|
+
current? ? Setting.run_start_date : start_date
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -6,7 +6,13 @@ module Bible270
|
|
|
6
6
|
class Reader < ApplicationRecord
|
|
7
7
|
self.table_name = 'bible270_readers'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
# Keep destruction unscoped: the selected progress association intentionally
|
|
10
|
+
# exposes only one run, but deleting an identity must remove every run's rows.
|
|
11
|
+
has_many :all_checkoffs, class_name: 'Bible270::Checkoff', dependent: :destroy, inverse_of: :reader
|
|
12
|
+
has_many :participations, class_name: 'Bible270::Participation', dependent: :destroy, inverse_of: :reader
|
|
13
|
+
has_one :participation, -> { chosen }, class_name: 'Bible270::Participation'
|
|
14
|
+
has_many :checkoffs, ->(reader) { where(Checkoff.arel_table[:participation_id].eq(reader.participation&.id)) },
|
|
15
|
+
class_name: 'Bible270::Checkoff', inverse_of: :reader
|
|
10
16
|
has_many :comments, class_name: 'Bible270::Comment', dependent: :destroy
|
|
11
17
|
has_many :likes, class_name: 'Bible270::Like', dependent: :destroy, inverse_of: :reader
|
|
12
18
|
belongs_to :owner, polymorphic: true, optional: true
|
|
@@ -24,7 +30,16 @@ module Bible270
|
|
|
24
30
|
|
|
25
31
|
def self.avatar_uploads? = AVATAR_UPLOADS
|
|
26
32
|
|
|
27
|
-
scope :with_avatar, -> {
|
|
33
|
+
scope :with_avatar, -> {
|
|
34
|
+
relation = preload(participation: :plan_run)
|
|
35
|
+
avatar_uploads? ? relation.includes(AVATAR_ASSOCIATIONS) : relation
|
|
36
|
+
}
|
|
37
|
+
scope :active, -> { where(id: Participation.active.select(:reader_id)) }
|
|
38
|
+
scope :alumni, -> { where.not(id: Participation.active.select(:reader_id)) }
|
|
39
|
+
|
|
40
|
+
after_create :enroll_in_current_run!
|
|
41
|
+
before_update :protect_archived_schedule
|
|
42
|
+
after_update :sync_participation_start_date
|
|
28
43
|
|
|
29
44
|
# Fires for every way a reader comes into existence — email sign-in, OmniAuth,
|
|
30
45
|
# or a bridged host user — rather than in each controller, so no path can
|
|
@@ -63,7 +78,7 @@ module Bible270
|
|
|
63
78
|
if: -> { self.class.daily_reminder_columns? }
|
|
64
79
|
|
|
65
80
|
scope :daily_reminder_recipients, ->(on:) {
|
|
66
|
-
where(daily_reminders: true)
|
|
81
|
+
active.where(daily_reminders: true)
|
|
67
82
|
.where.not(email: [nil, ''])
|
|
68
83
|
.where.not(id: where(last_daily_reminder_sent_on: on).select(:id))
|
|
69
84
|
}
|
|
@@ -90,7 +105,8 @@ module Bible270
|
|
|
90
105
|
end
|
|
91
106
|
|
|
92
107
|
def self.completed_days_by_id
|
|
93
|
-
Checkoff.
|
|
108
|
+
Checkoff.where(participation_id: Participation.chosen.select(:id))
|
|
109
|
+
.group(:reader_id, :day).count.each_with_object(Hash.new(0)) do |((reader_id, day), done), totals|
|
|
94
110
|
totals[reader_id] += 1 if done >= Plan.total_parts(day)
|
|
95
111
|
end
|
|
96
112
|
end
|
|
@@ -98,7 +114,7 @@ module Bible270
|
|
|
98
114
|
# A day is completed when its final required checkoff is created, so MAX(created_at)
|
|
99
115
|
# is the completion moment. Keep reader_id as a deterministic tie-breaker.
|
|
100
116
|
def self.completers_for(day)
|
|
101
|
-
reader_ids = Checkoff.where(day: day)
|
|
117
|
+
reader_ids = Checkoff.where(day: day, participation_id: Participation.active.select(:id))
|
|
102
118
|
.group(:reader_id)
|
|
103
119
|
.having('COUNT(*) >= ?', Plan.total_parts(day))
|
|
104
120
|
.order(Checkoff.arel_table[:created_at].maximum.asc, :reader_id)
|
|
@@ -329,7 +345,7 @@ module Bible270
|
|
|
329
345
|
def self.all_comment_notification_recipients
|
|
330
346
|
return none unless comment_notification_columns?
|
|
331
347
|
|
|
332
|
-
where(notify_on_all_comments: true).where.not(email: [nil, ''])
|
|
348
|
+
active.where(notify_on_all_comments: true).where.not(email: [nil, ''])
|
|
333
349
|
end
|
|
334
350
|
|
|
335
351
|
def self.completion_dove_column?
|
|
@@ -461,6 +477,88 @@ module Bible270
|
|
|
461
477
|
display_name.to_s.split(%r{\s+}).first(2).map { |w| w[0] }.join.upcase.presence || '?'
|
|
462
478
|
end
|
|
463
479
|
|
|
480
|
+
def alumni?
|
|
481
|
+
participation.nil? || participation.alumni? || !participation.plan_run.current?
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Used by admin membership changes and new-run admin enrollment, never by
|
|
485
|
+
# sign-in or comments. A new run clears the personal date; a same-run return retains its own date
|
|
486
|
+
# and checkoffs. Historical calendars live on the archived participation.
|
|
487
|
+
def activate!
|
|
488
|
+
run = PlanRun.current
|
|
489
|
+
run.with_lock do
|
|
490
|
+
raise PlanRun::StaleRun, 'The current run changed; retry activation' unless run.current?
|
|
491
|
+
|
|
492
|
+
with_lock do
|
|
493
|
+
membership = participations.find_by(plan_run_id: run.id)
|
|
494
|
+
if membership
|
|
495
|
+
update_columns(started_on: membership.started_on)
|
|
496
|
+
membership.update!(active: true, archived_on: nil, frozen_start_date: nil)
|
|
497
|
+
else
|
|
498
|
+
update_columns(started_on: nil)
|
|
499
|
+
participations.create!(plan_run: run, joined_at: Time.current)
|
|
500
|
+
end
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
reload_progress
|
|
504
|
+
participation
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
def archive!
|
|
508
|
+
run = PlanRun.current
|
|
509
|
+
run.with_lock do
|
|
510
|
+
with_lock { participation&.archive! }
|
|
511
|
+
end
|
|
512
|
+
reload_progress
|
|
513
|
+
participation
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def ensure_progress_writable!
|
|
517
|
+
membership = participation
|
|
518
|
+
raise Participation::ArchivedProgress, 'Reader has no participation' unless membership
|
|
519
|
+
|
|
520
|
+
# Recheck persisted state even when this Reader predates an admin restart.
|
|
521
|
+
membership.reload
|
|
522
|
+
membership.association(:plan_run).reset
|
|
523
|
+
membership.ensure_writable!
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
def enroll_in_current_run!
|
|
527
|
+
# A restart may win between selection and locking. Retry within this new
|
|
528
|
+
# reader's creation, rather than failing a sign-in whose token was consumed.
|
|
529
|
+
3.times do
|
|
530
|
+
run = PlanRun.current
|
|
531
|
+
membership = run.with_lock do
|
|
532
|
+
next unless run.current?
|
|
533
|
+
|
|
534
|
+
participations.create!(plan_run: run, joined_at: created_at, started_on: started_on)
|
|
535
|
+
end
|
|
536
|
+
return membership if membership
|
|
537
|
+
end
|
|
538
|
+
raise PlanRun::StaleRun, 'The current run kept changing during enrollment; retry sign-in'
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
def with_writable_progress
|
|
542
|
+
membership = participation
|
|
543
|
+
raise Participation::ArchivedProgress, 'Reader has no participation' unless membership
|
|
544
|
+
|
|
545
|
+
membership.plan_run.with_lock do
|
|
546
|
+
ensure_progress_writable!
|
|
547
|
+
yield
|
|
548
|
+
end
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
def protect_archived_schedule
|
|
552
|
+
# save's outer transaction retains this row lock through the reader update
|
|
553
|
+
# and sync_participation_start_date, even after with_writable_progress returns.
|
|
554
|
+
with_writable_progress { true } if will_save_change_to_started_on?
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
def sync_participation_start_date
|
|
558
|
+
participation.update!(started_on: started_on) if saved_change_to_started_on?
|
|
559
|
+
end
|
|
560
|
+
private :enroll_in_current_run!, :protect_archived_schedule, :sync_participation_start_date, :with_writable_progress
|
|
561
|
+
|
|
464
562
|
# {day => number of tracks checked off}
|
|
465
563
|
def checked_counts
|
|
466
564
|
@checked_counts ||= checkoffs.group(:day).count
|
|
@@ -562,13 +660,12 @@ module Bible270
|
|
|
562
660
|
def mark_day_complete!(day)
|
|
563
661
|
return false unless Plan.valid_day?(day)
|
|
564
662
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
insert_checkoffs(day, missing) if missing.any?
|
|
663
|
+
with_writable_progress do
|
|
664
|
+
# Read first rather than rescuing RecordNotUnique: model uniqueness
|
|
665
|
+
# validation can reject duplicates before they reach the database.
|
|
666
|
+
missing = missing_parts_on(day)
|
|
667
|
+
insert_checkoffs(day, missing) if missing.any?
|
|
668
|
+
end
|
|
572
669
|
|
|
573
670
|
reload_progress
|
|
574
671
|
true
|
|
@@ -588,7 +685,8 @@ module Bible270
|
|
|
588
685
|
now = Time.current
|
|
589
686
|
Checkoff.insert_all(
|
|
590
687
|
pairs.map do |track, part|
|
|
591
|
-
{ reader_id: id, day: day, track: track, part: part,
|
|
688
|
+
{ reader_id: id, participation_id: participation.id, day: day, track: track, part: part,
|
|
689
|
+
created_at: now, updated_at: now }
|
|
592
690
|
end
|
|
593
691
|
)
|
|
594
692
|
end
|
|
@@ -596,7 +694,7 @@ module Bible270
|
|
|
596
694
|
def clear_day!(day)
|
|
597
695
|
return false unless Plan.valid_day?(day)
|
|
598
696
|
|
|
599
|
-
checkoffs.where(day: day).destroy_all
|
|
697
|
+
with_writable_progress { checkoffs.where(day: day).destroy_all }
|
|
600
698
|
reload_progress
|
|
601
699
|
true
|
|
602
700
|
end
|
|
@@ -610,7 +708,7 @@ module Bible270
|
|
|
610
708
|
day = day.to_i
|
|
611
709
|
return false unless day.between?(0, Plan::DAYS)
|
|
612
710
|
|
|
613
|
-
|
|
711
|
+
with_writable_progress do
|
|
614
712
|
checkoffs.where(day: (day + 1)..).delete_all
|
|
615
713
|
(1..day).each { |d| mark_day_complete!(d) }
|
|
616
714
|
end
|
|
@@ -649,6 +747,12 @@ module Bible270
|
|
|
649
747
|
|
|
650
748
|
def reload_progress
|
|
651
749
|
@checked_counts = nil
|
|
750
|
+
association(:participation).reset
|
|
751
|
+
association(:participations).reset
|
|
752
|
+
association(:checkoffs).reset
|
|
753
|
+
# The association scope binds the selected membership ID; a revert can
|
|
754
|
+
# select an older membership on this same Reader instance.
|
|
755
|
+
association(:checkoffs).reset_scope
|
|
652
756
|
self
|
|
653
757
|
end
|
|
654
758
|
|
|
@@ -656,16 +760,27 @@ module Bible270
|
|
|
656
760
|
|
|
657
761
|
# The start date that actually governs this reader. A reader's own
|
|
658
762
|
# started_on wins when per-reader dates are allowed; otherwise (or if they
|
|
659
|
-
# haven't got one) the current run's shared date applies.
|
|
660
|
-
#
|
|
763
|
+
# haven't got one) the current run's shared date applies. Alumni instead use
|
|
764
|
+
# their immutable participation snapshot, including an explicitly undated one.
|
|
661
765
|
def effective_start_date
|
|
662
|
-
|
|
663
|
-
|
|
766
|
+
membership = participation
|
|
767
|
+
return membership.frozen_start_date if membership&.alumni?
|
|
768
|
+
|
|
769
|
+
# The selected run's snapshot remains authoritative after a revert, even
|
|
770
|
+
# when this Reader instance still holds the newer run's personal date.
|
|
771
|
+
personal_date = membership ? membership.started_on : started_on
|
|
772
|
+
if Bible270.config.allow_reader_start_date && personal_date
|
|
773
|
+
personal_date
|
|
664
774
|
else
|
|
665
775
|
Setting.run_start_date
|
|
666
776
|
end
|
|
667
777
|
end
|
|
668
778
|
|
|
779
|
+
# Calendar status and consistency stop moving on the archive date.
|
|
780
|
+
def progress_today
|
|
781
|
+
participation&.archived_on || Bible270.today
|
|
782
|
+
end
|
|
783
|
+
|
|
669
784
|
def dated?
|
|
670
785
|
effective_start_date.present?
|
|
671
786
|
end
|
|
@@ -677,7 +792,7 @@ module Bible270
|
|
|
677
792
|
|
|
678
793
|
# The plan day that today corresponds to (clamped into range), or nil when undated.
|
|
679
794
|
def calendar_day
|
|
680
|
-
Plan.day_for(
|
|
795
|
+
Plan.day_for(progress_today, effective_start_date)
|
|
681
796
|
end
|
|
682
797
|
|
|
683
798
|
def progress_status(completed_days: days_completed)
|
|
@@ -696,6 +811,8 @@ module Bible270
|
|
|
696
811
|
# plan's window. calendar_day clamps, so before the start date it reports day
|
|
697
812
|
# 1 — which made day 1 claim to be "today" for anyone whose plan hadn't begun.
|
|
698
813
|
def today_day
|
|
814
|
+
return nil if alumni?
|
|
815
|
+
|
|
699
816
|
raw = raw_calendar_day
|
|
700
817
|
return nil if raw.nil?
|
|
701
818
|
|
|
@@ -707,15 +824,15 @@ module Bible270
|
|
|
707
824
|
end
|
|
708
825
|
|
|
709
826
|
def raw_calendar_day
|
|
710
|
-
Plan.day_for(
|
|
827
|
+
Plan.day_for(progress_today, effective_start_date, clamp: false)
|
|
711
828
|
end
|
|
712
829
|
|
|
713
830
|
def not_started_yet?
|
|
714
|
-
Plan.before_start?(
|
|
831
|
+
Plan.before_start?(progress_today, effective_start_date)
|
|
715
832
|
end
|
|
716
833
|
|
|
717
834
|
def past_end_date?
|
|
718
|
-
Plan.after_end?(
|
|
835
|
+
Plan.after_end?(progress_today, effective_start_date)
|
|
719
836
|
end
|
|
720
837
|
|
|
721
838
|
def plan_end_date
|
|
@@ -758,6 +875,7 @@ module Bible270
|
|
|
758
875
|
# Called when a reader first participates. Only stamps a personal start date
|
|
759
876
|
# when per-reader dates are enabled and a shared date isn't already in force.
|
|
760
877
|
def ensure_started!
|
|
878
|
+
return if alumni?
|
|
761
879
|
return unless Bible270.config.allow_reader_start_date
|
|
762
880
|
return if started_on.present?
|
|
763
881
|
return if Setting.run_start_date.present?
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bible270
|
|
4
|
+
class ScriptureFrame < ApplicationRecord
|
|
5
|
+
self.table_name = 'bible270_scripture_frames'
|
|
6
|
+
|
|
7
|
+
scope :enabled, -> { where(enabled: true) }
|
|
8
|
+
|
|
9
|
+
before_validation :normalize_fields
|
|
10
|
+
|
|
11
|
+
before_validation :assign_translation_attribution, unless: :disabling_only?
|
|
12
|
+
|
|
13
|
+
validates :reference, presence: true, length: { maximum: 200 }
|
|
14
|
+
validates :text, presence: true, length: { maximum: 4000 }
|
|
15
|
+
validates :translation, presence: true, length: { maximum: 40 }
|
|
16
|
+
validates :translation, inclusion: {
|
|
17
|
+
in: ->(_frame) { ScriptureFrames.translations + [ScriptureFrames::OTHER_QUOTATION] },
|
|
18
|
+
message: 'is unsupported; add translation and attribution to YAML'
|
|
19
|
+
}, allow_blank: true, unless: :disabling_only?
|
|
20
|
+
validates :attribution, length: { maximum: 500 }, allow_nil: true
|
|
21
|
+
validates :enabled, inclusion: { in: [true, false] }
|
|
22
|
+
|
|
23
|
+
def attribution
|
|
24
|
+
return nil if ScriptureFrames.quotation?(translation)
|
|
25
|
+
|
|
26
|
+
# A registered nil credit is authoritative; only retired translations use stored credit.
|
|
27
|
+
ScriptureFrames.translation_attributions.fetch(translation.to_s.strip.upcase) { super }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def disabling_only?
|
|
33
|
+
persisted? && will_save_change_to_enabled?(from: true, to: false) && changed_attribute_names_to_save == ['enabled']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def normalize_fields
|
|
37
|
+
%i[reference text translation].each do |field|
|
|
38
|
+
value = public_send(field)
|
|
39
|
+
public_send("#{field}=", value.gsub("\r\n", "\n").strip) if value.is_a?(String)
|
|
40
|
+
end
|
|
41
|
+
self.translation = translation.upcase if translation.is_a?(String)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def assign_translation_attribution
|
|
45
|
+
self.attribution = ScriptureFrames.attribution_for(translation)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Bible270
|
|
4
|
-
# Runtime state an admin can change without a deploy: enrollment
|
|
5
|
-
# current run's shared calendar date.
|
|
4
|
+
# Runtime state an admin can change without a deploy: enrollment, the
|
|
5
|
+
# current run's shared calendar date, and default Scripture text availability.
|
|
6
6
|
class Setting < ApplicationRecord
|
|
7
7
|
self.table_name = 'bible270_settings'
|
|
8
8
|
|
|
9
9
|
ENROLLMENT_CLOSED_AT = 'enrollment_closed_at'
|
|
10
10
|
RUN_START_DATE = 'run_start_date'
|
|
11
|
+
ENROLLMENT_OPEN = 'enrollment_open'
|
|
11
12
|
|
|
12
13
|
validates :key, presence: true, uniqueness: true
|
|
13
14
|
|
|
@@ -62,6 +63,8 @@ module Bible270
|
|
|
62
63
|
def self.enrollment_closed?
|
|
63
64
|
return true if read(ENROLLMENT_CLOSED_AT).present?
|
|
64
65
|
|
|
66
|
+
return false if read(ENROLLMENT_OPEN) == 'true'
|
|
67
|
+
|
|
65
68
|
Bible270.config.respond_to?(:enrollment_open) && Bible270.config.enrollment_open == false
|
|
66
69
|
end
|
|
67
70
|
|
|
@@ -75,12 +78,18 @@ module Bible270
|
|
|
75
78
|
end
|
|
76
79
|
|
|
77
80
|
def self.close_enrollment!(at: Time.current)
|
|
78
|
-
|
|
81
|
+
transaction do
|
|
82
|
+
delete_key(ENROLLMENT_OPEN)
|
|
83
|
+
write(ENROLLMENT_CLOSED_AT, at.iso8601)
|
|
84
|
+
end
|
|
79
85
|
true
|
|
80
86
|
end
|
|
81
87
|
|
|
82
88
|
def self.open_enrollment!
|
|
83
|
-
|
|
89
|
+
transaction do
|
|
90
|
+
delete_key(ENROLLMENT_CLOSED_AT)
|
|
91
|
+
write(ENROLLMENT_OPEN, 'true')
|
|
92
|
+
end
|
|
84
93
|
true
|
|
85
94
|
end
|
|
86
95
|
end
|