2024-09-05 22:54:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-07-16 20:22:59 +02:00
|
|
|
class ElementsController < ApplicationController
|
2024-10-31 23:13:18 +01:00
|
|
|
before_action :set_page, only: %i[index new create]
|
2024-07-16 20:22:59 +02:00
|
|
|
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
|
2024-07-16 20:22:59 +02:00
|
|
|
|
|
|
|
|
# GET /elements/new
|
|
|
|
|
def new
|
2024-10-31 23:13:18 +01:00
|
|
|
@element = Element.new(page_id: params[:page_id])
|
|
|
|
|
@element.page = @page
|
2024-07-16 20:22:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# GET /elements/1/edit
|
2024-09-05 22:54:38 +02:00
|
|
|
def edit; end
|
2024-07-16 20:22:59 +02:00
|
|
|
|
|
|
|
|
# POST /elements
|
|
|
|
|
def create
|
2024-10-31 23:13:18 +01:00
|
|
|
# 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)
|
2024-10-31 23:13:18 +01:00
|
|
|
@element.page = @page
|
|
|
|
|
# @element.title = checklist.name if @element.title.blank?
|
2024-07-16 20:22:59 +02:00
|
|
|
|
|
|
|
|
if @element.save
|
2024-10-31 23:13:18 +01:00
|
|
|
# checklist.checks.each do |check|
|
|
|
|
|
# @element.success_criteria.create!(
|
|
|
|
|
# check:,
|
|
|
|
|
# title: check.t_name,
|
|
|
|
|
# level: check.conformity_level,
|
|
|
|
|
# quick_criterion: check.t_quick_criterion)
|
|
|
|
|
# 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
|
2024-07-16 20:22:59 +02:00
|
|
|
else
|
|
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# PATCH/PUT /elements/1
|
|
|
|
|
def update
|
|
|
|
|
if @element.update(element_params)
|
2024-11-11 04:04:13 +01:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.turbo_stream
|
|
|
|
|
format.html do
|
|
|
|
|
redirect_to @element, notice: "Element was successfully updated.", status: :see_other
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-07-16 20:22:59 +02:00
|
|
|
else
|
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# DELETE /elements/1
|
|
|
|
|
def destroy
|
2024-11-11 01:40:04 +01:00
|
|
|
Element.connection.transaction do
|
|
|
|
|
@element.destroy!
|
|
|
|
|
@element.page.elements.where("position > ?", @element.position).update_all("position = position - 1")
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.turbo_stream
|
|
|
|
|
format.html do
|
|
|
|
|
redirect_to page_elements_url(@element.page), notice: "Element was successfully destroyed.", status: :see_other
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-07-16 20:22:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
|
def set_element
|
|
|
|
|
@element = Element.find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-31 23:13:18 +01:00
|
|
|
def set_page
|
|
|
|
|
@page = Page.find(params[:page_id])
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-16 20:22:59 +02:00
|
|
|
# Only allow a list of trusted parameters through.
|
|
|
|
|
def element_params
|
2024-11-12 22:43:59 +01:00
|
|
|
params.require(:element).permit(:page_id, :title, :description, :position, :screenshot)
|
2024-07-16 20:22:59 +02:00
|
|
|
end
|
|
|
|
|
end
|