summaryrefslogtreecommitdiff
path: root/app/controllers/pokedex_entries_controller.rb
blob: 952f0cc4411f4fb7fbd85514a3305a18e4cc8d7f (plain)
1
2
3
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
31
32
33
34
35
36
class PokedexEntriesController < ApplicationController
  before_action :set_run
  before_action :only_allow_if_checked_out, only: [:create]

  def index
    @pokedex_entries = PokedexEntry.where(run: @run).order(:created_at).reverse
  end

  def new
    @pokedex_entry = PokedexEntry.new
  end

  def create
    @pokedex_entry = current_user.pokedex_entries.build(pokedex_entry_params)
    @pokedex_entry.run_id = params[:run_id]
    if @pokedex_entry.save
      redirect_to run_pokedex_entries_path(@run)
    else
      render :new, status: :unprocessable_entity
    end
  end

protected

  def pokedex_entry_params
    params.require(:pokedex_entry).permit(:pokemon_id, :recorded_at, :shiny)
  end

  def set_run
    @run = Run.find(params[:run_id])
  end

  def only_allow_if_checked_out
    redirect_to run_path(@run) unless @run.checked_out_user == current_user
  end
end