From 165a605af35d7484855ebb0221c3451e9a5a6753 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 29 Nov 2021 12:10:09 +0000 Subject: [PATCH] Add user case log association --- app/models/user.rb | 3 ++- spec/models/user_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index c2212fa2d..121a35ab7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,5 +4,6 @@ class User < ApplicationRecord devise :database_authenticatable, :recoverable, :rememberable, :validatable belongs_to :organisation - has_many :case_logs, through: :organisation + has_many :owned_case_logs, through: :organisation + has_many :managed_case_logs, through: :organisation end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 21ae656c9..10c1260fb 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,9 +3,32 @@ require "rails_helper" RSpec.describe User, type: :model do describe "#new" do let(:user) { FactoryBot.create(:user) } + let(:other_organisation) { FactoryBot.create(:organisation) } + let!(:owned_case_log) do + FactoryBot.create( + :case_log, + owning_organisation: user.organisation, + managing_organisation: other_organisation + ) + end + let!(:managed_case_log) do + FactoryBot.create( + :case_log, + owning_organisation: other_organisation, + managing_organisation: user.organisation + ) + end it "belongs to an organisation" do expect(user.organisation).to be_a(Organisation) end + + it "has owned case logs through their organisation" do + expect(user.owned_case_logs.first).to eq(owned_case_log) + end + + it "has managed case logs through their organisation" do + expect(user.managed_case_logs.first).to eq(managed_case_log) + end end end