Skip to content

Commit 499e240

Browse files
committed
Add events and event organizers including tests.
1 parent 747a9f1 commit 499e240

File tree

15 files changed

+463
-1
lines changed

15 files changed

+463
-1
lines changed

app/abilities/ability.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def initialize(user)
6363
can [:create, :destroy], CommunityMember, :user_id => user.id
6464
can [:destroy, :edit, :update], CommunityMember, :community => user_is_community_organizer
6565
can [:create, :edit, :new, :update], Event, :community => user_is_community_organizer
66+
can [:create], EventAttendance
67+
can [:update], EventAttendance, :user_id => user.id
6668
can [:close, :reopen], Note
6769
can [:show, :edit, :update], :preference
6870
can [:edit, :update], :profile
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class EventAttendancesController < ApplicationController
2+
layout "site"
3+
before_action :authorize_web
4+
before_action :set_event_attendance, :only => [:update]
5+
6+
authorize_resource
7+
8+
def create
9+
attendance = EventAttendance.new(event_attendance_params)
10+
if attendance.save
11+
redirect_to event_path(attendance.event), :notice => t(".success")
12+
else
13+
redirect_to event_path(attendance.event), :alert => t(".failure")
14+
end
15+
end
16+
17+
def update
18+
respond_to do |format|
19+
if @event_attendance.update(update_params)
20+
format.html { redirect_to @event_attendance.event, :notice => t(".success") }
21+
else
22+
format.html { redirect_to :edit, :alert => t(".failure") }
23+
end
24+
end
25+
end
26+
27+
private
28+
29+
def set_event_attendance
30+
@event_attendance = EventAttendance.find(params[:id])
31+
end
32+
33+
def event_attendance_params
34+
params.require(:event_attendance).permit(:event_id, :user_id, :intention)
35+
end
36+
37+
def update_params
38+
params.require(:event_attendance).permit(:intention)
39+
end
40+
end

app/controllers/events_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ def index
2727
# GET /events/1.json
2828
def show
2929
@community = Community.friendly.find(params[:community_id]) if params[:community_id]
30+
@yes_check = ""
31+
@no_check = ""
32+
@maybe_check = ""
33+
@yes_disabled = true
34+
@no_disabled = true
35+
@maybe_disabled = true
36+
if current_user
37+
@my_attendance = EventAttendance.find_or_initialize_by(:event_id => @event.id, :user_id => current_user&.id)
38+
@yes_check = @my_attendance.intention == EventAttendance::Intentions::YES ? "✓" : ""
39+
@no_check = @my_attendance.intention == EventAttendance::Intentions::NO ? "✓" : ""
40+
@maybe_check = @my_attendance.intention == EventAttendance::Intentions::MAYBE ? "✓" : ""
41+
@yes_disabled = @my_attendance.intention == EventAttendance::Intentions::YES
42+
@no_disabled = @my_attendance.intention == EventAttendance::Intentions::NO
43+
@maybe_disabled = @my_attendance.intention == EventAttendance::Intentions::MAYBE
44+
end
3045
rescue ActiveRecord::RecordNotFound
3146
@not_found_community = params[:community_id]
3247
render :template => "communities/no_such_community", :status => :not_found

app/models/event.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class Event < ApplicationRecord
2727
belongs_to :community
2828
has_many :event_organizers
29+
has_many :event_attendances
2930

3031
scope :future, -> { where(:moment => Time.now.utc..) }
3132
scope :past, -> { where(:moment => ...Time.now.utc) }
@@ -63,4 +64,20 @@ def organizers
6364
def past?
6465
moment < Time.now.utc
6566
end
67+
68+
def attendees(intention)
69+
EventAttendance.where(:event_id => id, :intention => intention)
70+
end
71+
72+
def yes_attendees
73+
attendees(EventAttendance::Intentions::YES)
74+
end
75+
76+
def no_attendees
77+
attendees(EventAttendance::Intentions::NO)
78+
end
79+
80+
def maybe_attendees
81+
attendees(EventAttendance::Intentions::MAYBE)
82+
end
6683
end

app/models/event_attendance.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# == Schema Information
2+
#
3+
# Table name: event_attendances
4+
#
5+
# id :bigint(8) not null, primary key
6+
# user_id :bigint(8) not null
7+
# event_id :bigint(8) not null
8+
# intention :enum not null
9+
# created_at :datetime not null
10+
# updated_at :datetime not null
11+
#
12+
# Indexes
13+
#
14+
# index_event_attendances_on_event_id (event_id)
15+
# index_event_attendances_on_user_id (user_id)
16+
# index_event_attendances_on_user_id_and_event_id (user_id,event_id) UNIQUE
17+
#
18+
# Foreign Keys
19+
#
20+
# fk_rails_... (event_id => events.id)
21+
# fk_rails_... (user_id => users.id)
22+
#
23+
24+
class EventAttendance < ApplicationRecord
25+
module Intentions
26+
YES = "Yes".freeze
27+
NO = "No".freeze
28+
MAYBE = "Maybe".freeze
29+
ALL_INTENTIONS = [YES, NO, MAYBE].freeze
30+
end
31+
validates :intention, :inclusion => { :in => Intentions::ALL_INTENTIONS }
32+
33+
belongs_to :event
34+
belongs_to :user
35+
end

app/views/events/show.html.erb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
<%= render :partial => "events/event_property", :locals => { :name => t(".description"), :value => @event.description } %>
2828
</table>
2929
</div>
30+
<div>
31+
<p><%= t(".people_are_going", :count => @event.yes_attendees.size) %></p>
32+
<p><%= t(".are_you_going") %></p>
33+
<%= form_with :model => @my_attendance do |form| %>
34+
<%= form.hidden_field(:event_id, :value => @event.id) %>
35+
<%= form.hidden_field(:user_id, :value => current_user&.id) %>
36+
<%= form.submit :name => "event_attendance[intention]", :value => @yes_check + t(".going_yes"), :disabled => @yes_disabled %>
37+
<%= form.submit :name => "event_attendance[intention]", :value => @no_check + t(".going_no"), :disabled => @no_disabled %>
38+
<%= form.submit :name => "event_attendance[intention]", :value => @maybe_check + t(".going_maybe"), :disabled => @maybe_disabled %>
39+
<% if !current_user %>
40+
<%= t(".login_to_rsvp") %>
41+
<% end %>
42+
<% end %>
43+
</div>
3044
</div>
3145
<div class="col-sm">
3246
<%= tag.div "",
@@ -43,3 +57,27 @@
4357
</div>
4458
</div>
4559
</div>
60+
<div class="row">
61+
<div class="row">
62+
<div class="col-sm">
63+
<strong><%= t(".who_yes") %></strong>
64+
<div>
65+
<% @event.yes_attendees.each do |attendance| %>
66+
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
67+
<% end %>
68+
</div>
69+
<strong><%= t(".who_maybe") %></strong>
70+
<div>
71+
<% @event.maybe_attendees.each do |attendance| %>
72+
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
73+
<% end %>
74+
</div>
75+
<strong><%= t(".who_no") %></strong>
76+
<div>
77+
<% @event.no_attendees.each do |attendance| %>
78+
<%= render :partial => "users/user_card", :locals => { :user => attendance.user } %>
79+
<% end %>
80+
</div>
81+
</div>
82+
</div>
83+
</div>

app/views/layouts/_header.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<% end %>
7373
<li><%= link_to t("layouts.gps_traces"), traces_path, :class => "dropdown-item" %></li>
7474
<li><%= link_to t("layouts.user_diaries"), diary_entries_path, :class => "dropdown-item" %></li>
75-
<li><%= link_to t("layouts.communities"), communities_path, :class => "dropdown-item" %></li>
75+
<li><%= link_to t("layouts.communities"), communities_index_path, :class => "dropdown-item" %></li>
7676
<li><%= link_to t("layouts.copyright"), copyright_path, :class => "dropdown-item" %></li>
7777
<li><%= link_to t("layouts.help"), help_path, :class => "dropdown-item" %></li>
7878
<li><%= link_to t("layouts.about"), about_path, :class => "dropdown-item" %></li>

config/locales/en.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,13 @@ en:
774774
not_found:
775775
title: File not found
776776
description: Couldn't find a file/directory/API operation by that name on the OpenStreetMap server (HTTP 404)
777+
event_attendances:
778+
create:
779+
success: Attendance was successfully saved.
780+
failure: Attendance could not be saved.
781+
update:
782+
success: Attendance was successfully updated.
783+
failure: Attendance could not be updated.
777784
events:
778785
create:
779786
success: Event was created successfully.
@@ -795,14 +802,27 @@ en:
795802
new:
796803
new: "New Event"
797804
show:
805+
are_you_going: "Are you going?"
798806
description: "Description"
799807
directions_to: "Directions to this location."
800808
edit: "Edit"
809+
going_maybe: "Maybe"
810+
going_no: "No"
811+
going_yes: "Yes"
801812
hosted_by: "Hosted by"
802813
location: "Location"
814+
login_to_rsvp: "Login to RSVP."
803815
organized_by: "Organized by"
804816
past: "Event is in the past."
817+
people_are_going:
818+
0: "" # TODO: Not working, but can't use "zero" due to lint.
819+
1: "1 person is going." # TODO: Not working.
820+
one: "1 person is going."
821+
other: "%{count} people are going"
805822
when: "When"
823+
who_yes: "Going"
824+
who_no: "Not Going"
825+
who_maybe: "Might Go"
806826
update:
807827
success: "The event was successfully updated."
808828
failure: "The event was not updated."
@@ -1717,6 +1737,9 @@ en:
17171737
home: Go to Home Location
17181738
logout: Log Out
17191739
log_in: Log In
1740+
log_in_tooltip: Log in with an existing account
1741+
communities: Communities
1742+
events: Events
17201743
sign_up: Sign Up
17211744
start_mapping: Start Mapping
17221745
edit: Edit

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
resources :community_members, :only => [:create, :destroy, :edit, :new, :update]
357357
get "/community_members" => "community_members#create", :as => "login_to_join"
358358
resources :events
359+
resources :event_attendances
359360

360361
# errors
361362
match "/400", :to => "errors#bad_request", :via => :all
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class CreateEventAttendances < ActiveRecord::Migration[7.0]
2+
def up
3+
create_enum :event_attendances_intention_enum, %w[Maybe No Yes]
4+
create_table :event_attendances do |t|
5+
t.references :user, :foreign_key => true, :null => false, :index => true
6+
t.references :event, :foreign_key => true, :null => false, :index => true
7+
t.column :intention, :event_attendances_intention_enum, :null => false
8+
9+
t.timestamps
10+
end
11+
add_index :event_attendances, [:user_id, :event_id], :unique => true
12+
end
13+
14+
def down
15+
drop_table :event_attendances
16+
drop_enum :event_attendances_intention_enum
17+
end
18+
end

0 commit comments

Comments
 (0)