ChaelCodes/HuntersKeepers

View on GitHub
app/controllers/improvements_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

# Controller for Improvements
class ImprovementsController < ApplicationController
  before_action :set_improvement, only: %i[show edit update destroy]

  # GET /improvements
  # GET /improvements.json
  def index
    @improvements = Improvement.all
  end

  # GET /improvements/1
  # GET /improvements/1.json
  def show
    hunter = Hunter.find_by(id: params[:hunter_id]) if params[:hunter_id]
    @improvable_options = @improvement.improvable_options(hunter) if hunter
  end

  # GET /improvements/new
  def new
    @improvement = Improvement.new
    authorize @improvement
  end

  # GET /improvements/1/edit
  def edit; end

  # POST /improvements
  # POST /improvements.json
  def create
    @improvement = Improvement.new(improvement_params)
    authorize @improvement
    respond_to do |format|
      if @improvement.save
        format.html { redirect_to improvement_url(@improvement), notice: 'Improvement was successfully created.' }
        format.json { render :show, status: :created, location: @improvement }
      else
        format.html { render :new }
        format.json { render json: @improvement.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /improvements/1
  # PATCH/PUT /improvements/1.json
  def update
    respond_to do |format|
      if @improvement.update(improvement_params)
        format.html do
          redirect_to improvement_url(@improvement),
                      notice: 'Improvement was successfully updated.'
        end
        format.json { render :show, status: :ok, location: @improvement }
      else
        format.html { render :edit }
        format.json { render json: @improvement.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /improvements/1
  # DELETE /improvements/1.json
  def destroy
    @improvement.destroy
    respond_to do |format|
      format.html { redirect_to improvements_url, notice: 'Improvement was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_improvement
    @improvement = Improvement.find(params[:id])
    authorize @improvement
  end

  # Never trust parameters from the scary internet,
  # only permit the allow list through.
  def improvement_params
    params.require(:improvement)
          .permit(:advanced, :description, :type, :playbook_id, :rating,
                  :stat_limit)
  end
end