From 4a3107b5e8414fe72d2b7f80e7f2fa8d373a8a2a Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 29 Nov 2021 13:17:03 +0000 Subject: [PATCH] User case log helper methods --- app/models/user.rb | 12 ++++++++++++ spec/models/user_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 121a35ab7..3640e1829 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,4 +6,16 @@ class User < ApplicationRecord belongs_to :organisation has_many :owned_case_logs, through: :organisation has_many :managed_case_logs, through: :organisation + + def case_logs + CaseLog.for_organisation(organisation) + end + + def completed_case_logs + case_logs.completed + end + + def not_completed_case_logs + case_logs.not_completed + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 10c1260fb..0e7eaa59c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -7,6 +7,7 @@ RSpec.describe User, type: :model do let!(:owned_case_log) do FactoryBot.create( :case_log, + :completed, owning_organisation: user.organisation, managing_organisation: other_organisation ) @@ -30,5 +31,14 @@ RSpec.describe User, type: :model do it "has managed case logs through their organisation" do expect(user.managed_case_logs.first).to eq(managed_case_log) end + + it "has case logs through their organisation" do + expect(user.case_logs.to_a).to eq([owned_case_log, managed_case_log]) + end + + it "has case log status helper methods" do + expect(user.completed_case_logs.to_a).to eq([owned_case_log]) + expect(user.not_completed_case_logs.to_a).to eq([managed_case_log]) + end end end