class DoodadsController < ApplicationController
  before_action :set_doodad, only: %i[ show edit update destroy ]

  # GET /doodads or /doodads.json
  def index
    authorize @doodads = policy_scope(Doodad)
  end

  # GET /doodads/1 or /doodads/1.json
  def show
    authorize @doodad
  end

  # GET /doodads/new
  def new
    authorize @doodad = current_user.doodads.new
  end

  # GET /doodads/1/edit
  def edit
    authorize @doodad
  end

  # POST /doodads or /doodads.json
  def create
    authorize @doodad = Doodad.new(doodad_params)

    respond_to do |format|
      if @doodad.save
        format.html { redirect_to @doodad, notice: "Doodad was successfully created." }
        format.json { render :show, status: :created, location: @doodad }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @doodad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /doodads/1 or /doodads/1.json
  def update
    authorize @doodad
    respond_to do |format|
      if @doodad.update(doodad_params)
        format.html { redirect_to @doodad, notice: "Doodad was successfully updated." }
        format.json { render :show, status: :ok, location: @doodad }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @doodad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /doodads/1 or /doodads/1.json
  def destroy
    authorize @doodad
    @doodad.destroy
    respond_to do |format|
      format.html { redirect_to doodads_url, notice: "Doodad was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_doodad
      @doodad = Doodad.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def doodad_params
      params.require(:doodad).permit(:body, :user_id, :is_public, :is_global)
    end
end
