Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ def organisers
@organisers ||= Member.with_role(:organiser, self)
end


def students
Member.joins(:groups)
.merge(Group.students)
.distinct
members_for_group('Students')
end

def coaches
Member.joins(:groups)
.merge(Group.coaches)
.distinct
members_for_group('Coaches')
end


private

def members_for_group(name)
members.where(groups: { name: name }).distinct
end

def expire_chapters_sidebar_cache
Rails.cache.delete('chapters-sidebar')
end
Expand Down
30 changes: 30 additions & 0 deletions spec/models/chapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,34 @@
expect(Rails.cache.read(cache_key)).to be_nil
end
end

describe "helper methods return only that chapter's coaches/students" do
RSpec.shared_examples 'group-scoped members' do |group_name, method_name|
let(:this_chapter) { Fabricate(:chapter) }
let(:that_chapter) { Fabricate(:chapter) }

let!(:this_group) { Fabricate(:group, chapter: this_chapter, name: group_name) }
let!(:that_group) { Fabricate(:group, chapter: that_chapter, name: group_name) }

let!(:this_member) { Fabricate(:member) }
let!(:that_member) { Fabricate(:member) }

before do
Fabricate(:subscription, group: this_group, member: this_member)
Fabricate(:subscription, group: that_group, member: that_member)
end

it "returns only #{group_name.downcase} for the chapter" do
expect(this_chapter.public_send(method_name))
.to contain_exactly(this_member)
end

it "does not include #{group_name.downcase} from another chapter" do
expect(this_chapter.public_send(method_name))
.not_to include(that_member)
end
end
Copy link
Copy Markdown
Contributor

@mikej mikej Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use some shared examples here, but currently these test cases won't actually get run when running chapter_spec.rb. After defining the shared_examples, you also need to add include_examples lines with the appropriate group_name and method_name for students and coaches.

e.g. inside the describe "helper methods... but after the RSpec.shared_examples you can put:

include_examples "group-scoped members", "Coaches", "coaches"
include_examples "group-scoped members", "Students", "students"

On doing this, the specs then fail with Validation failed: Group has already been taken so looks like the before results in trying to create the groups twice?

Are you OK to take a look at this as I agree having these tests in place is definitely worthwhile.

Lastly, if you want to check which test cases RSpec is running you can use the "documentation" format which lists the passing tests as well as any failures: bin/drspec -fd spec/models/chapter_spec.rb

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this! I hate Ruby 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken AI advice and put in a fix that seems to work. can you look over it?

it_behaves_like 'group-scoped members', 'Students', :students
it_behaves_like 'group-scoped members', 'Coaches', :coaches
end
end