Class: Bible270::CheckoffsController

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

Constant Summary

Constants inherited from ApplicationController

ApplicationController::REMEMBER_COOKIE

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
# 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)

  existing = current_reader.checkoffs.find_by(day: @day, track: @track, part: @part)
  if existing
    existing.destroy
  else
    current_reader.ensure_started!
    current_reader.checkoffs.create(day: @day, track: @track, part: @part)
  end

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

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to day_path(@day) }
  end
end