Skip to content

Commit

Permalink
User can rsvp
Browse files Browse the repository at this point in the history
0021, 0217, 0220

Allow user to cancel their intention to attend an event.

0022, 0217, 0222?

Show event attendees.

Add attendees of an event, reformat some dates

0024

Translate all the things.

0055

Use .user_cards for event attendances too.

0092

Disable 1 button

0176

Add events to header.

0179

Add yes and no traits to event attendance factory.

0189

Add tests for routing.

0217

Not going to force user to belong to community in order to attend event.

When using constants instead of strings, use them everywhere.

0219

Create update_params for update and add another test.

Finish writing tests for EventAttendancesController.

0220

Handle plurals better

0228

If the user is not signed in, provide a link to login before having them RSVP.

0230

No need for :login_link.  We just display a sentence with no params.

0231

Add maybe constant and convenience functions.

Convert event attendance intention to enum.

0273

Display RSVPs as cards using a partial.

User can only update their own RSVP.

Use "could not be" phrasing.
  • Loading branch information
openbrian committed May 10, 2023
1 parent 99b97e0 commit 506495a
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/abilities/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def initialize(user)
can [:destroy, :edit, :update], CommunityMember, :community => user_is_community_organizer
can [:destroy], CommunityMember, :user_id => user.id
can [:create, :edit, :new, :update], Event, :community => user_is_community_organizer
can [:create], EventAttendance
can [:update], EventAttendance, :user_id => user.id

if user.moderator?
can [:hide, :hidecomment], DiaryEntry
Expand Down
40 changes: 40 additions & 0 deletions app/controllers/event_attendances_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class EventAttendancesController < ApplicationController
layout "site"
before_action :authorize_web
before_action :set_event_attendance, :only => [:update]

authorize_resource

def create
attendance = EventAttendance.new(event_attendance_params)
if attendance.save
redirect_to event_path(attendance.event), :notice => t(".success")
else
redirect_to event_path(attendance.event), :alert => t(".failure")
end
end

def update
respond_to do |format|
if @event_attendance.update(update_params)
format.html { redirect_to @event_attendance.event, :notice => t(".success") }
else
format.html { redirect_to :edit, :alert => t(".failure") }
end
end
end

private

def set_event_attendance
@event_attendance = EventAttendance.find(params[:id])
end

def event_attendance_params
params.require(:event_attendance).permit(:event_id, :user_id, :intention)
end

def update_params
params.require(:event_attendance).permit(:intention)
end
end
7 changes: 7 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def update
# GET /events/1.json
def show
@community = Community.friendly.find(params[:community_id]) if params[:community_id]
@my_attendance = EventAttendance.find_or_initialize_by(:event_id => @event.id, :user_id => current_user&.id)
@yes_check = @my_attendance.intention == EventAttendance::Intentions::YES ? "✓" : ""
@no_check = @my_attendance.intention == EventAttendance::Intentions::NO ? "✓" : ""
@maybe_check = @my_attendance.intention == EventAttendance::Intentions::MAYBE ? "✓" : ""
@yes_disabled = @my_attendance.intention == EventAttendance::Intentions::YES
@no_disabled = @my_attendance.intention == EventAttendance::Intentions::NO
@maybe_disabled = @my_attendance.intention == EventAttendance::Intentions::MAYBE
rescue ActiveRecord::RecordNotFound
@not_found_community = params[:community_id]
render :template => "communities/no_such_community", :status => :not_found
Expand Down
17 changes: 17 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class Event < ApplicationRecord
belongs_to :community
has_many :event_organizers
has_many :event_attendances

scope :future, -> { where("moment >= ?", Time.now.utc) }
scope :past, -> { where("moment < ?", Time.now.utc) }
Expand Down Expand Up @@ -55,4 +56,20 @@ def organizers
def past?
moment < Time.now.utc
end

def attendees(intention)
EventAttendance.where(:event_id => id, :intention => intention)
end

def yes_attendees
attendees(EventAttendance::Intentions::YES)
end

def no_attendees
attendees(EventAttendance::Intentions::NO)
end

def maybe_attendees
attendees(EventAttendance::Intentions::MAYBE)
end
end
29 changes: 29 additions & 0 deletions app/models/event_attendance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# == Schema Information
#
# Table name: event_attendances
#
# id :bigint(8) not null, primary key
# user_id :integer not null
# event_id :integer not null
# intention :enum not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_event_attendances_on_event_id (event_id)
# index_event_attendances_on_user_id (user_id)
#

class EventAttendance < ApplicationRecord
module Intentions
YES = "Yes".freeze
NO = "No".freeze
MAYBE = "Maybe".freeze
ALL_INTENTIONS = [YES, NO, MAYBE].freeze
end
validates :intention, :inclusion => { :in => Intentions::ALL_INTENTIONS }

belongs_to :event
belongs_to :user
end
33 changes: 33 additions & 0 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@
</p>
</div>
<div class="col-sm">
<p><%= t(".people_are_going", :count => @event.yes_attendees.size) %></p>
<p><%= t(".are_you_going") %></p>
<% if current_user %>
<%= form_with :model => @my_attendance do |form| %>
<%= form.hidden_field(:event_id, :value => @event.id) %>
<%= form.hidden_field(:user_id, :value => current_user&.id) %>
<%= form.submit :name => "event_attendance[intention]", :value => @yes_check + t(".going_yes"), :disabled => @yes_disabled %>
<%= form.submit :name => "event_attendance[intention]", :value => @no_check + t(".going_no"), :disabled => @no_disabled %>
<%= form.submit :name => "event_attendance[intention]", :value => @maybe_check + t(".going_maybe"), :disabled => @maybe_disabled %>
<% end %>
<% else %>
<p>
<%# TODO: Use login_path %>
<%= t(".login_to_rsvp") %>
</p>
<% end %>
</div>
<h1>
<%= @event.title %>
Expand Down Expand Up @@ -56,6 +71,24 @@
<strong><%= t(".description") %>:</strong>
<%= @event.description %>
</p>
<strong><%= t(".who_yes") %></strong>
<div>
<% @event.yes_attendees.each do |attendance| %>
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
<% end %>
</div>
<strong><%= t(".who_maybe") %></strong>
<div>
<% @event.maybe_attendees.each do |attendance| %>
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
<% end %>
</div>
<strong><%= t(".who_no") %></strong>
<div>
<% @event.no_attendees.each do |attendance| %>
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
<% end %>
</div>
</div>
<div class="col-sm">
<p>
Expand Down
4 changes: 4 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<li class="compact-hide nav-item <%= current_page_class(communities_path) %>">
<%= link_to t("layouts.communities"), communities_path, :class => "nav-link" %>
</li>
<li class="compact-hide nav-item <%= current_page_class(events_path) %>">
<%= link_to t("layouts.events"), events_path, :class => "nav-link" %>
</li>
<li class="compact-hide nav-item <%= current_page_class(traces_path) %>">
<%= link_to t("layouts.gps_traces"), traces_path, :class => "nav-link" %>
</li>
Expand Down Expand Up @@ -77,6 +80,7 @@
</li>
<% end %>
<li class="<%= current_page_class(communities_path) %>"><%= link_to t("layouts.communities"), communities_path, :class => "dropdown-item" %></li>
<li class="<%= current_page_class(events_path) %>"><%= link_to t("layouts.events"), events_path, :class => "dropdown-item" %></li>
<li class="<%= current_page_class(traces_path) %>"><%= link_to t("layouts.gps_traces"), traces_path, :class => "dropdown-item" %></li>
<li class="<%= current_page_class(diary_entries_path) %>"><%= link_to t("layouts.user_diaries"), diary_entries_path, :class => "dropdown-item" %></li>
<li class="<%= current_page_class(community_index_path) %>"><%= link_to t("layouts.communities"), community_index_path, :class => "dropdown-item" %></li>
Expand Down
21 changes: 21 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ en:
not_found:
title: File not found
description: Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)
event_attendances:
create:
success: Attendance was successfully saved.
failure: Attendance could not be saved.
update:
success: Attendance was successfully updated.
failure: Attendance could not be updated.
events:
create:
success: Event was created successfully.
Expand All @@ -724,11 +731,22 @@ en:
description: "Description"
directions_to: "Directions to this location."
edit: "Edit"
going_maybe: "Maybe"
going_no: "No"
going_yes: "Yes"
hosted_by: "Hosted by"
location: "Location"
login_to_rsvp: "Login to RSVP."
organized_by: "Organized by"
past: "Event is in the past."
people_are_going:
zero: ""
one: "One person is going."
other: "%{count} people are going"
when: "When"
who_yes: "Going"
who_no: "Not Going"
who_maybe: "Might Go"
update:
success: "The event was successfully updated."
failure: "The event was not updated."
Expand Down Expand Up @@ -1644,6 +1662,9 @@ en:
home: Go to Home Location
logout: Log Out
log_in: Log In
log_in_tooltip: Log in with an existing account
communities: Communities
events: Events
sign_up: Sign Up
start_mapping: Start Mapping
edit: Edit
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@
resources :community_members, :only => [:create, :destroy, :edit, :new, :update]
get "/community_members" => "community_members#create", :as => "login_to_join"
resources :events
resources :event_attendances

# errors
match "/403", :to => "errors#forbidden", :via => :all
Expand Down
18 changes: 18 additions & 0 deletions db/migrate/20221010234421_create_event_attendances.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreateEventAttendances < ActiveRecord::Migration[7.0]
def up
create_enumeration :event_attendances_intention_enum, %w[Maybe No Yes]
create_table :event_attendances do |t|
t.references :user, :foreign_key => true, :null => false, :index => true
t.references :event, :foreign_key => true, :null => false, :index => true
t.column :intention, :event_attendances_intention_enum, :null => false

t.timestamps
end
add_index :event_attendances, [:user_id, :event_id], :unique => true
end

def down
drop_table :event_attendances
drop_enumeration :event_attendances_intention_enum
end
end
Loading

0 comments on commit 506495a

Please sign in to comment.