a11yist/app/controllers/reports_controller.rb

91 lines
2.2 KiB
Ruby
Raw Normal View History

class ReportsController < ApplicationController
before_action :set_report, only: %i[show edit update destroy work]
# GET /reports
def index
@reports = Report.all
end
# GET /reports/1
def show
2024-07-20 16:52:12 +02:00
respond_to do |format|
format.html
format.pdf
format.xlsx do
response.headers['Content-Disposition'] = %(attachment; filename="#{filename(@report, extension: 'xlsx')}")
render
end
format.docx do
template = Sablon.template(Rails.root.join('lib/templates/docx/report.docx'))
context = {
person: OpenStruct.new(first_name: @report.name),
skills: [],
education: [],
career: [],
referees: []
}
template.render_to_file(Rails.root.join('tmp/output.docx'), context)
send_file Rails.root.join('tmp/output.docx'),
filename: filename(@report, extension: 'docx'),
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
end
end
end
# GET /reports/new
def new
@report = Report.new
end
# GET /reports/1/edit
def edit
end
# POST /reports
def create
@report = Report.new(report_params)
if @report.save
redirect_to @report, notice: 'Report was successfully created.'
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /reports/1
def update
if @report.update(report_params)
redirect_to @report, notice: 'Report was successfully updated.', status: :see_other
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /reports/1
def destroy
@report.destroy!
redirect_to reports_url, notice: 'Report was successfully destroyed.', status: :see_other
end
def work
@report
end
private
# Use callbacks to share common setup or constraints between actions.
def set_report
@report = Report.find(params[:id])
end
# Only allow a list of trusted parameters through.
def report_params
2024-07-19 02:29:18 +02:00
params.require(:report).permit(:name, :comment_html)
end
2024-07-20 16:52:12 +02:00
def filename(report, extension: 'html')
"#{report.name}-#{Time.current.strftime('%Y%m%d%H%M')}.#{extension}"
end
end