Files
parse/app/controllers/projects_controller.rb
2025-09-10 15:36:34 +02:00

97 lines
2.9 KiB
Ruby

class ProjectsController < ApplicationController
before_action :set_project, only: %i[ show edit update destroy ]
before_action :set_collections, only: %i[new edit create update]
# GET /projects or /projects.json
def index
@projects = Project.includes(:subprojects => [:client, :owner, :builder]).all
end
# GET /projects/1 or /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new
@project.subprojects.build
@project.subprojects.each do |subproject|
subproject.build_client
subproject.build_owner
subproject.build_builder
subproject.build_subproject_address
end
end
# GET /projects/1/edit
def edit
end
# POST /projects or /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: "Project was successfully created." }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1 or /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: "Project was successfully updated." }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1 or /projects/1.json
def destroy
@project.destroy!
respond_to do |format|
format.html { redirect_to projects_path, status: :see_other, notice: "Project was successfully destroyed." }
format.json { head :no_content }
end
end
private
CLIENT_FIELDS = [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone]
SUBPROJECT_ADDRESS_FIELDS = [:streetname, :zipcode, :city, :country]
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
def set_collections
@clients = Client.order(:company_name, :lastname, :firstname).to_a
@projects = Project.order(:name).to_a
end
# Only allow a list of trusted parameters through.
def project_params
params.require(:project).permit(
:name, :email, :short_name,
subprojects_attributes: [
:id, :subproject_name, :client_id, :owner_id, :builder_id, :_destroy,
client_attributes: CLIENT_FIELDS,
owner_attributes: CLIENT_FIELDS,
builder_attributes: CLIENT_FIELDS,
subproject_address_attributes: SUBPROJECT_ADDRESS_FIELDS
]
)
end
end