Skip to content

Commit

Permalink
Show upcoming Events for a chapter on the chapter page
Browse files Browse the repository at this point in the history
  • Loading branch information
matyikriszta committed Sep 14, 2023
1 parent daa7230 commit 293ae86
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
13 changes: 11 additions & 2 deletions app/controllers/chapter_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ class ChapterController < ApplicationController
def show
@chapter = ChapterPresenter.new(Chapter.active.find_by!(slug: slug))

upcoming_events = @chapter.upcoming_workshops.includes(:sponsors).sort_by(&:date_and_time).group_by(&:date)
@upcoming_workshops = event_presenters_by_date(upcoming_events)
upcoming_workshops = upcoming_events_by_chapter(@chapter)
@upcoming_workshops = event_presenters_by_date(upcoming_workshops)

past_event = @chapter.workshops.most_recent
past_events = past_event.present? ? [past_event].group_by(&:date) : []
@latest_workshops = event_presenters_by_date(past_events)

@recent_sponsors = Sponsor.recent_for_chapter(@chapter)
end

Expand All @@ -16,6 +18,13 @@ def slug
params.permit(:id)[:id]
end

def upcoming_events_by_chapter(chapter)
workshops = chapter.upcoming_workshops.includes(:sponsors)
events = chapter.events.upcoming

[*workshops, *events].uniq.compact.sort_by(&:date_and_time).group_by(&:date)
end

def event_presenters_by_date(events)
events.map.inject({}) do |hash, (date, value)|
hash[date] = EventPresenter.decorate_collection(value)
Expand Down
2 changes: 1 addition & 1 deletion app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Chapter < ApplicationRecord
validates :description, length: { maximum: 280 }

has_many :workshops
has_and_belongs_to_many :events
has_and_belongs_to_many :events, join_table: 'chapters_events'
has_many :groups
has_many :sponsors, through: :workshops
has_many :subscriptions, through: :groups
Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Event < ApplicationRecord
has_many :silver_sponsors, -> { where('sponsorships.level' => 'silver') }, through: :sponsorships, source: :sponsor
has_many :gold_sponsors, -> { where('sponsorships.level' => 'gold') }, through: :sponsorships, source: :sponsor
has_many :organisers, -> { where('permissions.name' => 'organiser') }, through: :permissions, source: :members
has_and_belongs_to_many :chapters
has_and_belongs_to_many :chapters, join_table: 'chapters_events'
has_many :invitations

validates :name, :slug, :info, :schedule, :description, presence: true
Expand Down
18 changes: 8 additions & 10 deletions app/views/chapter/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.col-md-6
= image_tag @chapter.image.bg.url, alt: "#{@chapter.name} chapter", class: 'mw-100'

.alert-primary.pt-4.pb-4
.alert.alert-primary.pt-4.pb-4.mb-0.rounded-0
.container
.row.align-items-center
.col-md-6.mb-4.mb-md-0
Expand All @@ -19,24 +19,22 @@
- if logged_in?
= render partial: 'subscriptions'
- else
=link_to 'Sign up', new_member_path, class: 'btn btn-primary'
= link_to 'Sign up', new_member_path, class: 'btn btn-primary'

.container.py-4.py-lg-5
.row
.col-md-8.col-sm-12
- if @upcoming_workshops.any?
.pt-4
%h3 Upcoming Events
- @upcoming_workshops.each do |date, workshops|
.grouped-events
= render workshops
%h2.mb-4= t('homepage.events.upcoming')
- @upcoming_workshops.each do |date, workshops|
%h3.h5= date
= render workshops

- if @latest_workshops.any?
.pt-4
%h3 Past Events
%h2.mb-4 Past Events
- @latest_workshops.each do |date, workshops|
.grouped-events
= render workshops
= render workshops

.col-md-4.col-sm-12.mt-4
- if @chapter.twitter_handle
Expand Down
13 changes: 13 additions & 0 deletions spec/features/chapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
end
end

it 'renders any upcoming events for the chapter' do
chapter = Fabricate(:chapter)
2.times.map do |n|
Fabricate(:event, name: "Event #{n + 1}",
chapters: [chapter],
date_and_time: Time.zone.now + 2.months - n.months)
end

visit chapter_path(chapter.slug)
expect(page).to have_content 'Event 1'
expect(page).to have_content 'Event 2'
end

it 'renders the most recent past workshop for the chapter' do
chapter = Fabricate(:chapter)
past_workshop = Fabricate(:workshop, chapter: chapter, date_and_time: Time.zone.today - 2.weeks)
Expand Down

0 comments on commit 293ae86

Please sign in to comment.