65 lines
1.6 KiB
Ruby
65 lines
1.6 KiB
Ruby
|
|
class ElementsController < ApplicationController
|
||
|
|
before_action :set_element, only: %i[show edit update destroy]
|
||
|
|
|
||
|
|
# GET /elements
|
||
|
|
def index
|
||
|
|
@elements = Element.all
|
||
|
|
end
|
||
|
|
|
||
|
|
# GET /elements/1
|
||
|
|
def show
|
||
|
|
end
|
||
|
|
|
||
|
|
# GET /elements/new
|
||
|
|
def new
|
||
|
|
@element = Element.new
|
||
|
|
end
|
||
|
|
|
||
|
|
# GET /elements/1/edit
|
||
|
|
def edit
|
||
|
|
end
|
||
|
|
|
||
|
|
# POST /elements
|
||
|
|
def create
|
||
|
|
checklist_id = element_params.delete(:checklist_id)
|
||
|
|
checklist = Checklist.find(checklist_id)
|
||
|
|
@element = Element.new(element_params.merge(title: checklist.name))
|
||
|
|
|
||
|
|
if @element.save
|
||
|
|
checklist.checks.each do |check|
|
||
|
|
@element.success_criteria.create!(title: check.name, description: check.success_criterion, level: check.level)
|
||
|
|
end
|
||
|
|
redirect_to [:work, @element.report], notice: 'Element was successfully created.'
|
||
|
|
else
|
||
|
|
render :new, status: :unprocessable_entity
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
# PATCH/PUT /elements/1
|
||
|
|
def update
|
||
|
|
if @element.update(element_params)
|
||
|
|
redirect_to @element, notice: 'Element was successfully updated.', status: :see_other
|
||
|
|
else
|
||
|
|
render :edit, status: :unprocessable_entity
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
# DELETE /elements/1
|
||
|
|
def destroy
|
||
|
|
@element.destroy!
|
||
|
|
redirect_to elements_url, notice: 'Element was successfully destroyed.', status: :see_other
|
||
|
|
end
|
||
|
|
|
||
|
|
private
|
||
|
|
|
||
|
|
# Use callbacks to share common setup or constraints between actions.
|
||
|
|
def set_element
|
||
|
|
@element = Element.find(params[:id])
|
||
|
|
end
|
||
|
|
|
||
|
|
# Only allow a list of trusted parameters through.
|
||
|
|
def element_params
|
||
|
|
params.require(:element).permit(:report_id, :path, :title, :description, :checklist_id)
|
||
|
|
end
|
||
|
|
end
|