diff --git a/app/controllers/chapter_controller.rb b/app/controllers/chapter_controller.rb index 824bf38d2..35c200307 100644 --- a/app/controllers/chapter_controller.rb +++ b/app/controllers/chapter_controller.rb @@ -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 @@ -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) diff --git a/app/models/chapter.rb b/app/models/chapter.rb index cd15547f7..a0fa50de6 100644 --- a/app/models/chapter.rb +++ b/app/models/chapter.rb @@ -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 diff --git a/app/models/event.rb b/app/models/event.rb index e012da2f3..e247d334b 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/views/chapter/show.html.haml b/app/views/chapter/show.html.haml index 2ccd2c276..625a672c2 100644 --- a/app/views/chapter/show.html.haml +++ b/app/views/chapter/show.html.haml @@ -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 @@ -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 diff --git a/spec/features/chapter_spec.rb b/spec/features/chapter_spec.rb index d41b5c0fa..58dcd0f70 100644 --- a/spec/features/chapter_spec.rb +++ b/spec/features/chapter_spec.rb @@ -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)