Skip to content

Commit

Permalink
Merge pull request #1929 from codebar/show-upcoming-events-for-chapte…
Browse files Browse the repository at this point in the history
…r-on-chapter-page

Show upcoming Events for a chapter on the chapter page
  • Loading branch information
matyikriszta authored Sep 20, 2023
2 parents daa7230 + 092643a commit 068e7d6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 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
2 changes: 1 addition & 1 deletion app/views/events/_event.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
= link_to twitter_url_for(organiser.twitter), class: 'd-inline-block mx-1' do
= image_tag(organiser.avatar(26), class: 'rounded-circle', title: organiser.full_name, alt: organiser.full_name)
- if event.sponsors.any?
%div
.ms-auto
- event.sponsors.each do |sponsor|
= link_to sponsor.website, class: 'd-inline-block' do
= image_tag(sponsor.avatar.thumb.url, class: 'sponsor-sm mx-1', alt: sponsor.name)
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 068e7d6

Please sign in to comment.