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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
# Controller to manage dogs
class Webui::DogsController < ApplicationController
#### Includes and extends
include AnimalControl
#### Constants
BASIC_DOG_NAMES = ['Tobby', 'Thor', 'Rambo', 'Dog', 'Blacky'].freeze
#### Self config
#### Callbacks macros: before_action, after_action, etc.
before_action :set_dog, only: [:show, :edit, :update, :destroy]
# Pundit authorization policies control
after_action :verify_authorized, except: [:index, :blacks]
after_action :verify_policy_scoped, only: [:index, :blacks]
#### CRUD actions
# GET /dogs
def index
@dogs = policy_scope(Dog)
end
# GET /dogs/1
def show
if @dog.present?
authorize @dog
else
skip_authorization
end
end
# GET /dogs/new
def new
@dog = Dog.new
authorize @dog
end
# GET /dogs/1/edit
def edit
authorize @dog
end
# POST /dogs
def create
@dog = Dog.new(dog_params)
authorize @dog
if @dog.save
redirect_to @dog, notice: 'Dog was successfully created.'
else
render :new
end
end
# PATCH/PUT /dogs/1
def update
authorize @dog
if @dog.update(dog_params)
redirect_to @dog, notice: 'Dog was successfully updated.'
else
render :edit
end
end
# DELETE /dogs/1
def destroy
authorize @dog
@dog.destroy
redirect_to dogs_url, notice: 'Dog was successfully destroyed.'
end
#### Non CRUD actions
# List all the black dogs
# GET /dogs/blacks
def blacks
@dogs = policy_scope(Dog).blacks
call_them(@dogs)
render :index
end
#### Non actions methods
# Use hide_action if they are not private
def call_them(dogs = [])
say('Hey!')
dogs.each(&:bark)
end
hide_action :call_them
private
# Use callbacks to share common setup or constraints between actions.
def set_dog
@dog = Dog.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def dog_params
params.require(:dog).permit(:name, :color)
end
end
|