diff --git a/app/controllers/chapter_controller.rb b/app/controllers/chapter_controller.rb index 35c200307..4bbe76e14 100644 --- a/app/controllers/chapter_controller.rb +++ b/app/controllers/chapter_controller.rb @@ -1,4 +1,5 @@ class ChapterController < ApplicationController + include AttendanceConcerns def show @chapter = ChapterPresenter.new(Chapter.active.find_by!(slug: slug)) @@ -10,6 +11,7 @@ def show @latest_workshops = event_presenters_by_date(past_events) @recent_sponsors = Sponsor.recent_for_chapter(@chapter) + @attending_ids = attending_workshops end private diff --git a/app/controllers/concerns/attendance_concerns.rb b/app/controllers/concerns/attendance_concerns.rb new file mode 100644 index 000000000..b8c2b66b8 --- /dev/null +++ b/app/controllers/concerns/attendance_concerns.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module AttendanceConcerns + extend ActiveSupport::Concern + + def attending_workshops + current_user.nil? ? Set.new : current_user.workshop_invitations.accepted.pluck(:id).to_set + end + + def attending_events + current_user.nil? ? Set.new : current_user.invitations.accepted.pluck(:id).to_set + end +end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 770aec86b..0682ff84f 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,4 +1,5 @@ class DashboardController < ApplicationController + include AttendanceConcerns before_action :is_logged_in?, only: %i[dashboard] skip_before_action :accept_terms, except: %i[dashboard show] @@ -13,7 +14,7 @@ def show hash[key] = EventPresenter.decorate_collection(value) hash end - + @attending_ids = attending_workshops @testimonials = Testimonial.order(Arel.sql('RANDOM()')).limit(5).includes(:member) end @@ -24,6 +25,7 @@ def dashboard hash end @announcements = current_user.announcements.active + @attending_ids = attending_workshops end def code; end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 1736eb6ca..3c983ce47 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,6 +1,7 @@ require 'services/ticket' class EventsController < ApplicationController + include AttendanceConcerns before_action :is_logged_in?, only: %i[student coach] RECENT_EVENTS_DISPLAY_LIMIT = 40 @@ -23,6 +24,7 @@ def index events << Event.upcoming.includes(:venue, :sponsors).all events = events.compact.flatten.sort_by(&:date_and_time).group_by(&:date) @events = events.map.inject({}) { |hash, (key, value)| hash[key] = EventPresenter.decorate_collection(value); hash } + @attending_ids = attending_events end def show diff --git a/app/presenters/member_presenter.rb b/app/presenters/member_presenter.rb index 388415202..c64cf9acf 100644 --- a/app/presenters/member_presenter.rb +++ b/app/presenters/member_presenter.rb @@ -1,10 +1,26 @@ class MemberPresenter < BasePresenter + def organiser? has_role? :organiser, :any end + def is_admin? + @admin ||= has_role?(:admin) + end + + # Gather all the ids of all the events the member has organised or has organising permissions for + def organising_events + @organised_events ||= roles.preload(:resource).where(name: 'organiser').pluck(:resource_type, :resource_id).group_by(&:first).transform_values { |pairs | pairs.map(&:last)} + end + def event_organiser?(event) - has_role?(:organiser, event) || has_role?(:organiser, event.chapter) || has_role?(:admin) + if model.nil? + false + else + event_types = %w[Workshop Meeting Event] + organising_events.slice(*event_types).values.any? { |ids| ids.include?(event.id) } || @organised_events['Chapter']&.include?(event.chapter.id) || is_admin? + end + end def newbie? @@ -23,6 +39,8 @@ def pairing_details_array(role, tutorial, note) role.eql?('Coach') ? coach_pairing_details(note) : student_pairing_details(tutorial, note) end + + private def coach_pairing_details(note) diff --git a/app/views/admin/members/_profile.html.haml b/app/views/admin/members/_profile.html.haml index bd4e7f583..6dd89a600 100644 --- a/app/views/admin/members/_profile.html.haml +++ b/app/views/admin/members/_profile.html.haml @@ -37,7 +37,8 @@ - if @member.groups.any? %h5 Groups %ul.list-unstyled.ms-0#subscriptions - - @member.groups.each do |group| + - member_groups = @member.groups + - member_groups.each do |group| %li = link_to [:admin, group] do #{group.name} (#{group.chapter.name}) diff --git a/app/views/chapter/show.html.haml b/app/views/chapter/show.html.haml index a61fbd6fb..99cbad5dd 100644 --- a/app/views/chapter/show.html.haml +++ b/app/views/chapter/show.html.haml @@ -28,13 +28,13 @@ %h2.mb-4= t('homepage.events.upcoming') - @upcoming_workshops.each do |date, workshops| %h3.h5= date - = render workshops + = render workshops, attending_ids: @attending_ids - if @latest_workshops.any? .pt-4 %h2.mb-4 Past Events - @latest_workshops.each do |date, workshops| - = render workshops + = render workshops, attending_ids: @attending_ids - if @recent_sponsors.any? .py-4.py-lg-5.bg-light diff --git a/app/views/dashboard/dashboard.html.haml b/app/views/dashboard/dashboard.html.haml index 89d766e17..1f669e39c 100644 --- a/app/views/dashboard/dashboard.html.haml +++ b/app/views/dashboard/dashboard.html.haml @@ -16,7 +16,8 @@ %small= link_to 'Edit', subscriptions_path, class: 'ms-2' - if current_user.chapters.any? .list-group.list-group-flush - - current_user.chapters.each do |chapter| + - chapters = current_user.chapters + - chapters.each do |chapter| = link_to chapter.name, chapter_url(chapter.slug), class: 'list-group-item list-group-item-action' - else %i.fas.fa-exclamation-triangle @@ -32,4 +33,4 @@ There are no upcoming events announced for the chapters you are subscribed to. - @ordered_events.each do |date, workshops| %h4= date - = render workshops + = render workshops, attending_ids: @attending_ids diff --git a/app/views/dashboard/show.html.haml b/app/views/dashboard/show.html.haml index c161757ea..8fffaccac 100644 --- a/app/views/dashboard/show.html.haml +++ b/app/views/dashboard/show.html.haml @@ -71,7 +71,7 @@ %h2.h3.mb-4= t('homepage.events.upcoming') - @upcoming_workshops.each do |date, workshops| %h3.h5= date - = render workshops + = render workshops, attending_ids: @attending_ids .col-lg-4.pl-lg-5 %h3 diff --git a/app/views/events/_event.html.haml b/app/views/events/_event.html.haml index d36a297aa..a80724684 100644 --- a/app/views/events/_event.html.haml +++ b/app/views/events/_event.html.haml @@ -5,10 +5,11 @@ - if event.chapter %span.badge.bg-primary.mb-3.mb-md-0 = link_to event.chapter.name, event.chapter.slug, class: 'text-light text-decoration-none' - - if @user - - if @user.attending?(event.__getobj__) + - if !attending_ids.blank? + - if attending_ids.include?(event.id) %span.badge.bg-success.mb-3.mb-md-0 = link_to 'Attending', event.path, class: 'text-light text-decoration-none' + - if @user - if @user.event_organiser?(event) %span.badge.bg-secondary.mb-3.mb-md-0 = link_to 'Manage', event.admin_path, class: 'text-light text-decoration-none' @@ -31,10 +32,12 @@ .d-md-flex.justify-content-md-between.align-items-md-center - if event.organisers.any? .mb-3.mb-md-0 - - event.organisers.each do |organiser| + - organisers = event.organisers + - organisers.each do |organiser| = image_tag(organiser.avatar(26), class: 'rounded-circle', title: organiser.full_name, alt: organiser.full_name) - if event.sponsors.any? .ms-auto - - event.sponsors.each do |sponsor| + - sponsors = event.sponsors + - 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) diff --git a/app/views/events/_events.html.haml b/app/views/events/_events.html.haml index 95a9a1dd0..f2c4a0520 100644 --- a/app/views/events/_events.html.haml +++ b/app/views/events/_events.html.haml @@ -4,4 +4,4 @@ %h3.h5= date .row .col-md-8 - = render workshops + = render workshops, attending_ids: attending_ids diff --git a/app/views/events/index.html.haml b/app/views/events/index.html.haml index fd5908a8d..86aa0e085 100644 --- a/app/views/events/index.html.haml +++ b/app/views/events/index.html.haml @@ -5,11 +5,11 @@ .col - if @events.any? %h3.mb-4 Upcoming Events - = render partial: 'events', locals: { grouped_events: @events } + = render partial: 'events', locals: { grouped_events: @events, attending_ids: @attending_ids} .container{'data-test': 'past-events'} .row .col - if @past_events.any? %h3.mb-4 Past Events - = render partial: 'events', locals: { grouped_events: @past_events } + = render partial: 'events', locals: { grouped_events: @past_events, attending_ids: @attending_ids } diff --git a/app/views/workshops/_workshop.html.haml b/app/views/workshops/_workshop.html.haml index b8cbe5e71..e013f1059 100644 --- a/app/views/workshops/_workshop.html.haml +++ b/app/views/workshops/_workshop.html.haml @@ -1 +1 @@ -= render partial: 'events/event', locals: { event: workshop } += render partial: 'events/event', locals: { event: workshop, attending_ids: attending_ids }