a11yist/app/controllers/elements_controller.rb

70 lines
1.8 KiB
Ruby
Raw Normal View History

2024-09-05 22:54:38 +02:00
# frozen_string_literal: true
class ElementsController < ApplicationController
before_action :set_element, only: %i[show edit update destroy]
# GET /elements
def index
@elements = Element.all
end
# GET /elements/1
2024-09-05 22:54:38 +02:00
def show; end
# GET /elements/new
def new
2024-07-19 02:29:18 +02:00
@element = Element.new(report_id: params[:report_id])
end
# GET /elements/1/edit
2024-09-05 22:54:38 +02:00
def edit; end
# POST /elements
def create
checklist_id = element_params.delete(:checklist_id)
checklist = Checklist.find(checklist_id)
2024-07-19 02:29:18 +02:00
@element = Element.new(element_params)
@element.title = checklist.name if @element.title.blank?
if @element.save
checklist.checks.each do |check|
2024-09-05 22:54:38 +02:00
@element.success_criteria.create!(title: check.t_name, description_html: check.t_criterion,
2024-07-19 02:29:18 +02:00
level: check.level)
end
2024-07-20 22:32:35 +02:00
respond_to do |format|
2024-09-05 22:54:38 +02:00
format.html { redirect_to @element.report, notice: "Element was successfully created." }
2024-07-20 22:32:35 +02:00
format.turbo_stream
end
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /elements/1
def update
if @element.update(element_params)
2024-09-05 22:54:38 +02:00
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!
2024-09-05 22:54:38 +02:00
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
2024-07-19 02:29:18 +02:00
params.require(:element).permit(:report_id, :path, :title, :description_html, :checklist_id)
end
end