diff --git a/app/controllers/concerns/event_registration_session.rb b/app/controllers/concerns/event_registration_session.rb new file mode 100644 index 000000000..833633f34 --- /dev/null +++ b/app/controllers/concerns/event_registration_session.rb @@ -0,0 +1,19 @@ +module EventRegistrationSession + extend ActiveSupport::Concern + + private + + def store_event_reg_slug(event_id, slug) + session[:event_reg_slugs] ||= {} + session[:event_reg_slugs][event_id.to_s] = slug + end + + def clear_event_reg_slug(event_id) + return unless session[:event_reg_slugs] + session[:event_reg_slugs].delete(event_id.to_s) + end + + def event_reg_slug_for(event_id) + session.dig(:event_reg_slugs, event_id.to_s) + end +end diff --git a/app/controllers/events/public_registrations_controller.rb b/app/controllers/events/public_registrations_controller.rb index ade7bfcec..96b738b77 100644 --- a/app/controllers/events/public_registrations_controller.rb +++ b/app/controllers/events/public_registrations_controller.rb @@ -1,5 +1,6 @@ module Events class PublicRegistrationsController < ApplicationController + include EventRegistrationSession skip_before_action :authenticate_user!, only: [ :new, :create, :show ] before_action :set_event before_action :ensure_registerable, only: [ :new, :create ] @@ -53,6 +54,7 @@ def create ) if result.success? + store_event_reg_slug(@event.id, result.event_registration.slug) redirect_to registration_ticket_path(result.event_registration.slug), notice: "You have been successfully registered!" else diff --git a/app/controllers/events/registrations_controller.rb b/app/controllers/events/registrations_controller.rb index a6b82c20c..d2c808a1c 100644 --- a/app/controllers/events/registrations_controller.rb +++ b/app/controllers/events/registrations_controller.rb @@ -1,5 +1,6 @@ module Events class RegistrationsController < ApplicationController + include EventRegistrationSession before_action :authenticate_user!, only: [ :create, :destroy ] before_action :set_event, only: [ :create, :destroy ] before_action :set_registrant, only: [ :create, :destroy ] @@ -7,6 +8,7 @@ class RegistrationsController < ApplicationController def show authorize! @event_registration, to: :show_public? + store_event_reg_slug(@event_registration.event_id, @event_registration.slug) if @event_registration.active? end def resend_confirmation @@ -20,6 +22,7 @@ def cancel if @event_registration.active? @event_registration.update!(status: "cancelled") + clear_event_reg_slug(@event_registration.event_id) redirect_to registration_ticket_path(@event_registration.slug), notice: "Your registration has been cancelled." else redirect_to registration_ticket_path(@event_registration.slug), alert: "Registration is already cancelled." @@ -31,6 +34,7 @@ def reactivate if @event_registration.status == "cancelled" @event_registration.update!(status: "registered") + store_event_reg_slug(@event_registration.event_id, @event_registration.slug) redirect_to registration_ticket_path(@event_registration.slug), notice: "Your registration has been reactivated." else redirect_to registration_ticket_path(@event_registration.slug), alert: "Registration is not cancelled." @@ -43,6 +47,7 @@ def create if existing&.status == "cancelled" authorize! existing existing.update!(status: "registered") + store_event_reg_slug(@event.id, existing.slug) success = "Your registration has been reactivated." respond_to do |format| format.turbo_stream { flash.now[:notice] = success } @@ -55,6 +60,7 @@ def create authorize! @event_registration if @event_registration.save + store_event_reg_slug(@event.id, @event_registration.slug) success = "You have successfully registered for this event." respond_to do |format| format.turbo_stream { flash.now[:notice] = success } diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index c043b3f52..cbb90e830 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,5 +1,5 @@ class EventsController < ApplicationController - include AhoyTracking, TagAssignable + include AhoyTracking, TagAssignable, EventRegistrationSession skip_before_action :authenticate_user!, only: [ :index, :show ] skip_before_action :verify_authenticity_token, only: [ :preview ] before_action :set_event, only: %i[ show edit update destroy preview manage copy_registration_form ] @@ -8,12 +8,14 @@ def index authorize! base_scope = authorized_scope(Event.all) @events = base_scope.search_by_params(params).order(start_date: :desc) + persist_reg_slug_from_params end def show authorize! @event @event = @event.decorate track_view(@event) + persist_reg_slug_from_params end def new @@ -228,6 +230,12 @@ def set_event @event = Event.find(params[:id]) end + def persist_reg_slug_from_params + return unless params[:reg].present? + reg = EventRegistration.find_by(slug: params[:reg]) + store_event_reg_slug(reg.event_id, reg.slug) if reg&.active? + end + def event_params params.require(:event).permit(:cost, :created_by_id, diff --git a/app/views/events/_card.html.erb b/app/views/events/_card.html.erb index 5ba05ace0..5c2d72150 100644 --- a/app/views/events/_card.html.erb +++ b/app/views/events/_card.html.erb @@ -30,6 +30,11 @@ <% registered = current_user && event.actively_registered?(current_user.person) %> + <% unless registered + reg_slug = session.dig(:event_reg_slugs, event.id.to_s) + slug_registration = reg_slug ? EventRegistration.find_by(slug: reg_slug, event_id: event.id) : nil + slug_registered = slug_registration&.active? + end %>