add archive model
This commit is contained in:
parent
959026ca4f
commit
c25f3c79d9
6 changed files with 153 additions and 0 deletions
51
app/models/archive.rb
Normal file
51
app/models/archive.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: archives
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# content_type :string not null
|
||||
# month :date
|
||||
# status :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
class Archive < ApplicationRecord
|
||||
include AASM
|
||||
|
||||
RETENTION_DURATION = 1.week
|
||||
|
||||
has_and_belongs_to_many :groupe_instructeurs
|
||||
|
||||
has_one_attached :file
|
||||
|
||||
scope :stale, -> { where('updated_at < ?', (Time.zone.now - RETENTION_DURATION)) }
|
||||
scope :for_groupe_instructeur, -> (groupe_instructeur) {
|
||||
joins(:archives_groupe_instructeurs)
|
||||
.where(
|
||||
archives_groupe_instructeurs: { groupe_instructeur: groupe_instructeur }
|
||||
)
|
||||
}
|
||||
|
||||
enum content_type: {
|
||||
everything: 'everything',
|
||||
monthly: 'monthly'
|
||||
}
|
||||
|
||||
enum status: {
|
||||
pending: 'pending',
|
||||
generated: 'generated'
|
||||
}
|
||||
|
||||
aasm whiny_persistence: true, column: :status, enum: true do
|
||||
state :pending, initial: true
|
||||
state :generated
|
||||
|
||||
event :make_available do
|
||||
transitions from: :pending, to: :generated
|
||||
end
|
||||
end
|
||||
|
||||
def available?
|
||||
status == 'generated' && file.attached?
|
||||
end
|
||||
end
|
|
@ -25,6 +25,7 @@ class Instructeur < ApplicationRecord
|
|||
has_many :followed_dossiers, through: :follows, source: :dossier
|
||||
has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier
|
||||
has_many :trusted_device_tokens, dependent: :destroy
|
||||
has_many :archives
|
||||
|
||||
has_one :user, dependent: :nullify
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
class CreateArchiveForGroupeInstructeur < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :archives do |t|
|
||||
t.string :status, null: false
|
||||
t.date :month
|
||||
t.string :content_type, null: false
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
create_table "archives_groupe_instructeurs", force: :cascade do |t|
|
||||
t.belongs_to :archive, foreign_key: true, null: false
|
||||
t.belongs_to :groupe_instructeur, foreign_key: true, null: false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
19
db/schema.rb
19
db/schema.rb
|
@ -81,6 +81,23 @@ ActiveRecord::Schema.define(version: 2021_04_27_120002) do
|
|||
t.index ["procedure_id"], name: "index_administrateurs_procedures_on_procedure_id"
|
||||
end
|
||||
|
||||
create_table "archives", force: :cascade do |t|
|
||||
t.string "status", null: false
|
||||
t.date "month"
|
||||
t.string "content_type", null: false
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
end
|
||||
|
||||
create_table "archives_groupe_instructeurs", force: :cascade do |t|
|
||||
t.bigint "archive_id", null: false
|
||||
t.bigint "groupe_instructeur_id", null: false
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["archive_id"], name: "index_archives_groupe_instructeurs_on_archive_id"
|
||||
t.index ["groupe_instructeur_id"], name: "index_archives_groupe_instructeurs_on_groupe_instructeur_id"
|
||||
end
|
||||
|
||||
create_table "assign_tos", id: :serial, force: :cascade do |t|
|
||||
t.integer "instructeur_id"
|
||||
t.datetime "created_at"
|
||||
|
@ -735,6 +752,8 @@ ActiveRecord::Schema.define(version: 2021_04_27_120002) do
|
|||
end
|
||||
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "archives_groupe_instructeurs", "archives"
|
||||
add_foreign_key "archives_groupe_instructeurs", "groupe_instructeurs"
|
||||
add_foreign_key "assign_tos", "groupe_instructeurs"
|
||||
add_foreign_key "attestation_templates", "procedures"
|
||||
add_foreign_key "attestations", "dossiers"
|
||||
|
|
14
spec/factories/archive.rb
Normal file
14
spec/factories/archive.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
FactoryBot.define do
|
||||
factory :archive do
|
||||
content_type { 'everything' }
|
||||
groupe_instructeurs { [association(:groupe_instructeur)] }
|
||||
|
||||
trait :pending do
|
||||
status { 'pending' }
|
||||
end
|
||||
|
||||
trait :generated do
|
||||
status { 'generated' }
|
||||
end
|
||||
end
|
||||
end
|
51
spec/models/archive_spec.rb
Normal file
51
spec/models/archive_spec.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
describe Dossier do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
before { Timecop.freeze(Time.zone.now) }
|
||||
after { Timecop.return }
|
||||
|
||||
let(:archive) { create(:archive) }
|
||||
|
||||
describe 'scopes' do
|
||||
describe 'staled' do
|
||||
let(:recent_archive) { create(:archive) }
|
||||
let(:staled_archive) { create(:archive, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) }
|
||||
|
||||
subject do
|
||||
archive; recent_archive; staled_archive
|
||||
Archive.stale
|
||||
end
|
||||
|
||||
it { is_expected.to match_array([staled_archive]) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '.status' do
|
||||
it { expect(archive.status).to eq('pending') }
|
||||
end
|
||||
|
||||
describe '#make_available!' do
|
||||
before { archive.make_available! }
|
||||
it { expect(archive.status).to eq('generated') }
|
||||
end
|
||||
|
||||
describe '#available?' do
|
||||
subject { archive.available? }
|
||||
context 'without attachment' do
|
||||
let(:archive) { create(:archive, file: nil) }
|
||||
it { is_expected.to eq(false) }
|
||||
end
|
||||
|
||||
context 'with an attachment' do
|
||||
context 'when the attachment was created but the process was not over' do
|
||||
let(:archive) { create(:archive, :pending, file: Rack::Test::UploadedFile.new('spec/fixtures/files/file.pdf', 'application/pdf')) }
|
||||
it { is_expected.to eq(false) }
|
||||
end
|
||||
|
||||
context 'when the attachment was created but the process was not over' do
|
||||
let(:archive) { create(:archive, :generated, file: Rack::Test::UploadedFile.new('spec/fixtures/files/file.pdf', 'application/pdf')) }
|
||||
it { is_expected.to eq(true) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue