Class: Bible270::CheckoffsController

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

Instance Method Summary collapse

Instance Method Details

#toggleObject



4
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
# File 'app/controllers/bible270/checkoffs_controller.rb', line 4

def toggle
  return unless require_reader!

  @day   = params[:day].to_i
  @track = params[:track].to_s

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

  existing = current_reader.checkoffs.find_by(day: @day, track: @track)
  if existing
    existing.destroy
  else
    current_reader.ensure_started!
    current_reader.checkoffs.create(day: @day, track: @track)
  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