# Events controller - Public event listings and individual event display # # This controller manages public event browsing and displays individual events # with their associated ticket types. No authentication required for public browsing. class EventsController < ApplicationController # No authentication required for public event viewing before_action :authenticate_user!, only: [] before_action :set_event, only: [ :show ] # Display paginated list of upcoming published events # # Shows events in published state, ordered by start time ascending # Includes event owner information and supports Kaminari pagination def index @events = Event.includes(:user).upcoming.page(params[:page]).per(12) end # Display individual event with ticket type information # # Shows complete event details including venue information, # available ticket types, and allows users to add tickets to cart def show # Event is set by set_event callback with ticket types preloaded # Template will display event details and ticket selection interface end private # Find and set the current event with eager-loaded associations # Supports both old slug-only format and new SEO-friendly year/month/slug format # Loads event with ticket types to avoid N+1 queries def set_event if params[:year] && params[:month] # New SEO-friendly format: /events/2024/07/summer-party year = params[:year].to_i month = params[:month].to_i start_of_month = Date.new(year, month, 1).beginning_of_month end_of_month = start_of_month.end_of_month @event = Event.includes(:ticket_types) .where(slug: params[:slug]) .where(start_time: start_of_month..end_of_month) .first! else # Legacy format: /events/summer-party (for backward compatibility) @event = Event.includes(:ticket_types).find_by!(slug: params[:slug]) end rescue ActiveRecord::RecordNotFound redirect_to events_path, alert: "Événement non trouvé" end # Generate SEO-friendly path for an event def seo_event_path(event) year = event.start_time.year month = format("%02d", event.start_time.month) event_path(year: year, month: month, slug: event.slug) end helper_method :seo_event_path end