Skip to content

Commit 117d741

Browse files
committed
Revert "Merge pull request #2267 from davidmillen50/issue-2241-use-associations-more"
This reverts commit 986142e, reversing changes made to 4544b83.
1 parent a93f2d9 commit 117d741

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

app/models/workshop.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@ class Workshop < ApplicationRecord
3030
before_validation :set_date_and_time, :set_end_date_and_time, if: proc { |model| model.chapter_id.present? }
3131
before_validation :set_opens_at
3232

33-
def workshop_sponsors_with_host_true
34-
Workshop.includes(:workshop_sponsors).flat_map do |workshop|
35-
workshop.workshop_sponsors.select { |ws| ws.host == true && ws.workshop_id == id }
36-
end
37-
end
38-
3933
def host
40-
workshop_sponsors_with_host_true.first&.sponsor
34+
WorkshopSponsor.hosts.for_workshop(id).first&.sponsor
4135
end
4236

4337
def waiting_list

app/models/workshop_sponsor.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ class WorkshopSponsor < ApplicationRecord
33
belongs_to :workshop
44

55
validates :sponsor_id, uniqueness: { scope: :workshop_id, message: :already_sponsoring }
6+
7+
scope :hosts, -> { where('workshop_sponsors.host = ?', true) }
8+
scope :for_workshop, ->(workshop_id) { where('workshop_sponsors.workshop_id = ?', workshop_id) }
69
end

spec/models/workshop_sponsor_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,31 @@
66
.with_message('already a sponsor')
77
end
88
end
9+
10+
context '#scopes' do
11+
context '#hosts' do
12+
it 'includes workshops with hosts' do
13+
workshop_sponsor = Fabricate(:workshop_sponsor, host: true)
14+
15+
expect(WorkshopSponsor.hosts).to include(workshop_sponsor)
16+
end
17+
18+
it 'excludes workshops without hosts' do
19+
expect(WorkshopSponsor.hosts).to eq []
20+
end
21+
end
22+
23+
context '#for_workshop' do
24+
it 'includes sponsors of the workshop' do
25+
workshop_sponsor = Fabricate(:workshop_sponsor)
26+
expect(WorkshopSponsor.for_workshop(workshop_sponsor.workshop)).to include(workshop_sponsor)
27+
end
28+
29+
it 'excludes sponsors not sponsoring the workshop' do
30+
Fabricate(:workshop_sponsor)
31+
workshop = Fabricate(:workshop_no_sponsor)
32+
expect(WorkshopSponsor.for_workshop(workshop)).to eq []
33+
end
34+
end
35+
end
936
end

0 commit comments

Comments
 (0)