Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/controllers/event_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class EventRegistrationsController < ApplicationController

def create
@event_registration = EventRegistration.new(event_registration_params)
if @event_registration.save
redirect_to @event_registration.event, notice: 'Successfully registered for the event.'
else
redirect_to @event_registration.event, alert: "Registration failed: #{@event_registration.errors.full_messages.join(', ')}"
end
end

def bulk_create
event_ids = Array(params[:event_ids]).map(&:to_i).uniq
if event_ids.blank?
redirect_to events_path, alert: "Please select at least one event."
return
end

attendee_attrs = {
first_name: current_user.first_name || current_user.email.split('@').first,
last_name: current_user.last_name || 'User',
email: current_user.email
}

created = 0
errors = []

Event.transaction do
event_ids.each do |event_id|
existing_registration = EventRegistration.where(
event_id: event_id,
email: attendee_attrs[:email]
).first

if existing_registration
errors << "Event '#{Event.find(event_id).title}': You are already registered for this event."
next
end

reg = EventRegistration.new(attendee_attrs.merge(event_id: event_id))
unless reg.save
errors << "Event '#{Event.find(event_id).title}': #{reg.errors.full_messages.to_sentence}"
else
created += 1
end
end

raise ActiveRecord::Rollback if errors.any?
end

if errors.any?
redirect_to events_path, alert: errors.join("; ")
else
redirect_to events_path, notice: "Successfully registered for #{created} event#{'s' if created != 1}."
end
end

private

def event_registration_params
params.require(:event_registration).permit(:event_id, :first_name, :last_name, :email)
end
end
67 changes: 67 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class EventsController < ApplicationController
before_action :set_event, only: %i[ show edit update destroy ]
before_action :authorize_admin!, only: %i[ new edit update destroy ]

def index
@events = Event.all
end

def show
end

def new
@event = Event.new
end

def edit
end

def create
@event = Event.new(event_params)

respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: "Event was successfully created." }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to @event, notice: "Event was successfully updated." }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end

def destroy
@event.destroy

respond_to do |format|
format.html { redirect_to events_path, status: :see_other, notice: "Event was successfully destroyed." }
format.json { head :no_content }
end
end

private

def set_event
@event = Event.find(params[:id])
end

def event_params
params.require(:event).permit(:title, :description, :start_date, :end_date, :registration_close_date, :publicly_visible)
end

def authorize_admin!
redirect_to events_path, alert: "You are not authorized to perform this action." unless current_admin
end
end
6 changes: 6 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Event < ApplicationRecord
has_many :event_registrations, dependent: :destroy

validates_presence_of :title, :start_date, :end_date
validates_inclusion_of :publicly_visible, in: [true, false]
end
7 changes: 7 additions & 0 deletions app/models/event_registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class EventRegistration < ApplicationRecord
belongs_to :event

validates_presence_of :first_name, :last_name, :email, :event_id
validates_uniqueness_of :email, scope: :event_id, message: 'is already registered for this event', case_sensitive: false
validates_format_of :email, with: URI::MailTo::EMAIL_REGEXP
end
29 changes: 29 additions & 0 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@event) do |f| %>
<%= render 'shared/errors', resource: @event if @event.errors.any? %>

<div class="form-group">
<%= f.label :title, class: 'bold' %>
<%= f.text_field :title, class: 'form-control' %>

<%= f.label :description, class: 'bold' %>
<%= f.text_area :description, class: 'form-control' %>

<%= f.label :start_date, class: 'bold' %>
<%= f.text_field :start_date, type: 'datetime-local', class: 'form-control', value: @event.start_date&.strftime('%Y-%m-%dT%H:%M') %>

<%= f.label :end_date, class: 'bold' %>
<%= f.text_field :end_date, type: 'datetime-local', class: 'form-control', value: @event.end_date&.strftime('%Y-%m-%dT%H:%M') %>

<%= f.label :registration_close_date, class: 'bold' %>
<%= f.text_field :registration_close_date, type: 'datetime-local', class: 'form-control', value: @event.registration_close_date&.strftime('%Y-%m-%dT%H:%M') %>

<div style="display: flex; align-items: center; margin-top: 10px; column-gap: 10px;">
<%= f.label :publicly_visible, class: 'bold' %>
<%= f.check_box :publicly_visible, { class: 'bold mt-0' }, '1', '0' %>
</div>
</div>

<div class="form-actions">
<%= f.button :submit, class: 'btn btn-primary' %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/events/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Event</h1>

<%= render 'form', event: @event %>

<%= link_to 'Show', @event %> |
<%= link_to 'Back', events_path %>
60 changes: 60 additions & 0 deletions app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<p id="notice"><%= notice %></p>

<h1>Events</h1>

<%= form_with url: bulk_create_event_registrations_path, method: :post, local: true, id: 'bulk-registration-form' do |form| %>
<table>
<thead>
<tr>
<th></th>
<th>Title</th>
<th>Start Date</th>
<th>End Date</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
<% @events.each do |event| %>
<tr>
<td style="padding-right: 10px;">
<%= check_box_tag "event_ids[]", event.id, false, class: "event-checkbox" %>
</td>
<td style="padding-right: 10px;"><%= event.title %></td>
<td style="padding-right: 10px;"><%= event.start_date.strftime("%B %d, %Y") if event.start_date %></td>
<td style="padding-right: 10px;"><%= event.end_date.strftime("%B %d, %Y") if event.end_date %></td>
<td style="padding-right: 10px;">
<%= link_to 'Show', event %> |
<%= link_to 'Edit', edit_event_path(event) %> |
<%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</tbody>
</table>

<br>

<div>
<%= form.submit "Register for Selected Events", class: "btn btn-primary", id: "register-button", disabled: true %>
<%= link_to 'New Event', new_event_path, class: "btn btn-primary" %>
</div>
<% end %>

<script>
document.addEventListener('DOMContentLoaded', function() {
const checkboxes = document.querySelectorAll('.event-checkbox');
const registerButton = document.getElementById('register-button');

function updateRegisterButton() {
const checkedBoxes = document.querySelectorAll('.event-checkbox:checked');
registerButton.disabled = checkedBoxes.length === 0;
}

checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', updateRegisterButton);
});

updateRegisterButton();
});
</script>
5 changes: 5 additions & 0 deletions app/views/events/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Event</h1>

<%= render 'form', event: @event %>

<%= link_to 'Back', events_path %>
36 changes: 36 additions & 0 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<p id="notice"><%= notice %></p>

<h1>Event Details</h1>

<p>
<strong>Title:</strong>
<%= @event.title %>
</p>

<p>
<strong>Description:</strong>
<%= @event.description %>
</p>

<p>
<strong>Start Date:</strong>
<%= @event.start_date.strftime("%B %d, %Y %I:%M %p") if @event.start_date.present? %>
</p>

<p>
<strong>End Date:</strong>
<%= @event.end_date.strftime("%B %d, %Y %I:%M %p") if @event.end_date.present? %>
</p>

<p>
<strong>Registration Close Date:</strong>
<%= @event.registration_close_date.strftime("%B %d, %Y %I:%M %p") if @event.registration_close_date.present? %>
</p>

<p>
<strong>Publicly Visible:</strong>
<%= @event.publicly_visible? ? 'Yes' : 'No' %>
</p>

<%= link_to 'Edit', edit_event_path(@event) %> |
<%= link_to 'Back', events_path %>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
resources :workshop_log_creation_wizard
resources :workshop_logs, only: [:show, :edit, :new, :create, :update]

resources :events
resources :event_registrations, only: [:create] do
collection do
post :bulk_create
end
end
resources :resources

get 'stories', to: 'resources#stories'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20250912133056_create_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEvents < ActiveRecord::Migration[6.1]
def change
create_table :events do |t|
t.string "title"
t.text "description"
t.datetime "start_date"
t.datetime "end_date"
t.datetime "registration_close_date"
t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20250912155207_create_event_registrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEventRegistrations < ActiveRecord::Migration[6.1]
def change
create_table :event_registrations do |t|
t.string :first_name
t.string :last_name
t.string :email
t.references :event, foreign_key: true

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20250912173152_add_publicly_visible_to_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPubliclyVisibleToEvents < ActiveRecord::Migration[6.1]
def change
add_column :events, :publicly_visible, :boolean, default: false, null: false
end
end
Loading