Skip to content

Commit

Permalink
add invalidation date and user
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-mj committed Dec 13, 2024
1 parent fac84b2 commit fc86163
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions config/locales/reports.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc86163

Please sign in to comment.