Class: Bible270::CheckoffsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bible270/checkoffs_controller.rb

Instance Method Summary collapse

Instance Method Details

#toggleObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/bible270/checkoffs_controller.rb', line 5

def toggle
  return unless require_reader!

  @day   = params[:day].to_i
  @track = params[:track].to_s
  # Part 0 is the first chapter of the reading. Requests without one predate
  # per-chapter check-offs and mean the first part.
  @part  = params[:part].to_i

  head :bad_request and return unless Plan.valid_day?(@day) &&
                                      Plan.present_tracks(@day).include?(@track) &&
                                      Plan.valid_part?(@day, @track, @part)

  requested_state = params[:checked] if params.key?(:checked)
  head :bad_request and return if params.key?(:checked) && !%w[0 1].include?(requested_state)

  marked_read = apply_checkoff(requested_state)

  @reader = current_reader.reload.reload_progress
  @reader_tracks = @reader.read_tracks_for(@day)
  @readings = Plan.readings_for(@day)

  required = Plan.total_parts(@day)
  completer_ids = Checkoff.where(day: @day)
    .group(:reader_id)
    .having('COUNT(*) >= ?', required)
    .pluck(:reader_id)
  @completers = Reader.where(id: completer_ids).order(:display_name)

  respond_to do |format|
    format.turbo_stream
    format.html do
      message = marked_read ? 'Reading marked read.' : 'Reading marked unread.'
      redirect_to day_path(@day), flash: { b270_interaction_status: message }
    end
  end
end