Skip to content

Commit 29ef33e

Browse files
tuzzfloehopper
authored andcommitted
Add Project.users, .with_users and #with_user methods
1 parent b1f2cf2 commit 29ef33e

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

app/models/project.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ class Project < ApplicationRecord
1919

2020
scope :internal_projects, -> { where(user_id: nil) }
2121

22+
def self.users
23+
User.from_userinfo(ids: pluck(:user_id))
24+
end
25+
26+
def self.with_users
27+
by_id = users.index_by(&:id)
28+
all.map { |instance| [instance, by_id[instance.user_id]] }
29+
end
30+
31+
def with_user
32+
[self, User.from_userinfo(ids: user_id).first]
33+
end
34+
2235
# Work around a CanCanCan issue with accepts_nested_attributes_for.
2336
# https://github.com/CanCanCommunity/cancancan/issues/774
2437
def components=(array)

spec/factories/project.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
FactoryBot.define do
44
factory :project do
5-
user_id { SecureRandom.uuid }
5+
user_id { '22222222-2222-2222-2222-222222222222' } # Matches users.json.
66
name { Faker::Book.title }
77
identifier { "#{Faker::Verb.base}-#{Faker::Verb.base}-#{Faker::Verb.base}" }
88
project_type { 'python' }

spec/models/project_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
require 'rails_helper'
44

55
RSpec.describe Project do
6+
before do
7+
stub_user_info_api
8+
end
9+
610
describe 'associations', :sample_words do
711
it { is_expected.to belong_to(:school).optional(true) }
812
it { is_expected.to belong_to(:lesson).optional(true) }
@@ -68,4 +72,70 @@
6872
expect { unsaved_project.valid? }.to change { unsaved_project.identifier.nil? }.from(true).to(false)
6973
end
7074
end
75+
76+
describe '.users' do
77+
it 'returns User instances for the current scope' do
78+
create(:project)
79+
80+
user = described_class.all.users.first
81+
expect(user.name).to eq('School Student')
82+
end
83+
84+
it 'ignores members where no profile account exists' do
85+
create(:project, user_id: SecureRandom.uuid)
86+
87+
user = described_class.all.users.first
88+
expect(user).to be_nil
89+
end
90+
91+
it 'ignores members not included in the current scope' do
92+
create(:project)
93+
94+
user = described_class.none.users.first
95+
expect(user).to be_nil
96+
end
97+
end
98+
99+
describe '.with_users' do
100+
it 'returns an array of class members paired with their User instance' do
101+
project = create(:project)
102+
103+
pair = described_class.all.with_users.first
104+
user = described_class.all.users.first
105+
106+
expect(pair).to eq([project, user])
107+
end
108+
109+
it 'returns nil values for members where no profile account exists' do
110+
project = create(:project, user_id: SecureRandom.uuid)
111+
112+
pair = described_class.all.with_users.first
113+
expect(pair).to eq([project, nil])
114+
end
115+
116+
it 'ignores members not included in the current scope' do
117+
create(:project)
118+
119+
pair = described_class.none.with_users.first
120+
expect(pair).to be_nil
121+
end
122+
end
123+
124+
describe '#with_user' do
125+
it 'returns the class member paired with their User instance' do
126+
project = create(:project)
127+
128+
pair = project.with_user
129+
user = described_class.all.users.first
130+
131+
expect(pair).to eq([project, user])
132+
end
133+
134+
it 'returns a nil value if the member has no profile account' do
135+
project = create(:project, user_id: SecureRandom.uuid)
136+
137+
pair = project.with_user
138+
expect(pair).to eq([project, nil])
139+
end
140+
end
71141
end

0 commit comments

Comments
 (0)