Skip to content

Commit

Permalink
refacto approving_instructor as a relation
Browse files Browse the repository at this point in the history
  • Loading branch information
JeSuisUnCaillou committed Jan 20, 2025
1 parent f33607a commit 45efb4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
18 changes: 10 additions & 8 deletions app/models/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Authorization < ApplicationRecord
as: :entity,
dependent: :nullify

has_one :approve_authorization_request_event,
-> { where(name: 'approve').order(created_at: :desc).limit(1) },
dependent: :nullify,
class_name: 'AuthorizationRequestEvent',
inverse_of: :entity

has_one :approving_instructor,
through: :approve_authorization_request_event,
source: :user

scope :validated, -> { joins(:request).where(authorization_requests: { state: 'validated' }) }

delegate :name, :kind, to: :request
Expand Down Expand Up @@ -68,14 +78,6 @@ def definition
authorization_request_class.constantize.definition
end

def approving_instructor
authorization_request_events
.where(name: 'approve')
.order(created_at: :desc)
.first
.try(:user)
end

private

def affect_snapshot_documents(request_as_validated)
Expand Down
26 changes: 26 additions & 0 deletions spec/models/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,30 @@
end
end
end

describe '#approving_instructor' do
subject { authorization.approving_instructor }

let!(:authorization) { create(:authorization) }

context 'when there is no approving instructor' do
it { is_expected.to be_nil }
end

context 'when there is an approve event' do
let!(:event) { create(:authorization_request_event, entity: authorization, name: 'approve') }

it { is_expected.to eq(event.user) }
end

context 'when there is several approve events' do
let!(:events) do
create_list(:authorization_request_event, 3, entity: authorization, name: 'approve') do |event, index|
event.update(created_at: Time.zone.now - index.days)
end
end

it { is_expected.to eq(events.first.user) }
end
end
end

0 comments on commit 45efb4a

Please sign in to comment.