From fc8616388df5f1ba1b6c31e7449c6a2a63064c27 Mon Sep 17 00:00:00 2001 From: Anderson Meireles Date: Fri, 13 Dec 2024 20:07:03 -0300 Subject: [PATCH] add invalidation date and user --- app/controllers/reports_controller.rb | 8 +++++--- app/models/report.rb | 13 +++++++++++-- config/locales/reports.pt-BR.yml | 3 +++ ...tion_date_and_invalidated_by_to_reports_table.rb | 13 +++++++++++++ db/schema.rb | 6 +++++- 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20241213223506_add_invalidation_date_and_invalidated_by_to_reports_table.rb diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 1f0193df..b30ea22c 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -10,10 +10,12 @@ class ReportsController < ApplicationController skip_before_action :authenticate_user!, only: :download_by_identifier active_scaffold :report do |config| - config.list.columns = [:user, :file_name, :identifier, :created_at, :expires_at] - config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at] + config.list.columns = [:user, :file_name, :identifier, :created_at, :expires_at_or_invalid] + config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at, :invalidated_by, :invalidated_at] config.update.columns = [:expires_at] + config.columns = config.list.columns config.columns[:user].clear_link + config.columns[:expires_at_or_invalid].label = I18n.t("activerecord.attributes.report.expires_at") config.actions.exclude :create, :delete config.action_links.add "download", label: " @@ -46,7 +48,7 @@ def download_by_identifier end def invalidate - @report.invalidate! + @report.invalidate!(user: current_user) redirect_to reports_path, notice: "Documento invalidado com sucesso." end diff --git a/app/models/report.rb b/app/models/report.rb index 4b886522..80caf151 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -2,15 +2,24 @@ class Report < ApplicationRecord belongs_to :user, foreign_key: "generated_by_id" + belongs_to :invalidated_by, foreign_key: "invalidated_by_id", class_name: "User", optional: true belongs_to :carrierwave_file, foreign_key: "carrierwave_file_id", class_name: "CarrierWave::Storage::ActiveRecord::ActiveRecordFile", optional: true def to_label "#{self.user.name} - #{I18n.l(self.created_at, format: '%d/%m/%Y %H:%M')}" end - def invalidate! + def invalidate!(user:) carrierwave_file = self.carrierwave_file - self.update!(carrierwave_file_id: nil) + self.update!(carrierwave_file_id: nil, invalidated_by: user, invalidated_at: Time.now) carrierwave_file.delete end + + def expires_at_or_invalid + if self.invalidated_at.present? + I18n.t("activerecord.attributes.report.invalidated") + else + self.expires_at.present? ? I18n.l(self.expires_at, format: "%d/%m/%Y") : nil + end + end end diff --git a/config/locales/reports.pt-BR.yml b/config/locales/reports.pt-BR.yml index 0b889d97..e277d9fc 100644 --- a/config/locales/reports.pt-BR.yml +++ b/config/locales/reports.pt-BR.yml @@ -10,6 +10,9 @@ pt-BR: identifier: "Identificador" created_at: "Data de Criação" expires_at: "Data de Expiração" + invalidated: "Invalidado" + invalidated_at: "Data de Invalidação" + invalidated_by: "Invalidado por" models: report: diff --git a/db/migrate/20241213223506_add_invalidation_date_and_invalidated_by_to_reports_table.rb b/db/migrate/20241213223506_add_invalidation_date_and_invalidated_by_to_reports_table.rb new file mode 100644 index 00000000..63d1205b --- /dev/null +++ b/db/migrate/20241213223506_add_invalidation_date_and_invalidated_by_to_reports_table.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AddInvalidationDateAndInvalidatedByToReportsTable < ActiveRecord::Migration[7.0] + def up + add_reference :reports, :invalidated_by, foreign_key: { to_table: :users } + add_column :reports, :invalidated_at, :datetime + end + + def down + remove_column :reports, :invalidated_at + remove_reference :reports, :invalidated_by, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2d4d0379..7c9b9c8b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_11_29_022042) do +ActiveRecord::Schema[7.0].define(version: 2024_12_13_223506) do create_table "accomplishments", force: :cascade do |t| t.integer "enrollment_id" t.integer "phase_id" @@ -855,8 +855,11 @@ t.date "expires_at" t.string "identifier" t.string "file_name" + t.integer "invalidated_by_id" + t.datetime "invalidated_at" t.index ["carrierwave_file_id"], name: "index_reports_on_carrierwave_file_id" t.index ["generated_by_id"], name: "index_reports_on_generated_by_id" + t.index ["invalidated_by_id"], name: "index_reports_on_invalidated_by_id" end create_table "research_areas", force: :cascade do |t| @@ -1046,4 +1049,5 @@ add_foreign_key "grants", "professors" add_foreign_key "reports", "carrier_wave_files", column: "carrierwave_file_id" add_foreign_key "reports", "users", column: "generated_by_id" + add_foreign_key "reports", "users", column: "invalidated_by_id" end