Skip to content

Commit

Permalink
Change events attendees views, controllers and route, fix the attend …
Browse files Browse the repository at this point in the history
…button
  • Loading branch information
VanessaAoki committed May 4, 2021
1 parent a28741b commit 267ea99
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'
ruby '2.7.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3', '>= 6.1.3.1'
Expand Down
39 changes: 12 additions & 27 deletions app/controllers/event_attendees_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
class EventAttendee < ApplicationRecord
def create
@attendance = EventAttendance.new(event_attendance_params)
@attendance.attendee_id = current_user.id
class EventAttendeesController < ApplicationController

respond_to do |format|
if EventAttendance.where('attendee_id = ? AND attended_event_id = ?', @attendance.attendee_id,
@attendance.attended_event_id).exists?
format.html do
redirect_to event_path(@attendance.attended_event_id), alert: "You're already attending this event."
end
format.json { render json: @attendance.errors, status: :unprocessable_entity }
elsif @attendance.save
format.html do
redirect_to event_path(@attendance.attended_event_id), notice: 'Attendance was successfully confirmed.'
end
format.json { render :show, status: :created, location: @event }
else
format.html { render :show, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def create
@event = Event.find(params[:event_attendee][:attended_event_id])
current_user.attend!(@event)
redirect_to @event
end

private
def destroy
@event = EventAttendee.find(params[:id]).attended_event
current_user.cancel!(@event)
redirect_to @event
end

def event_attendance_params
params.require(:event_attendance).permit(:attended_event_id)
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class UserController < ApplicationController
def show
@user = User.find(params[:id])
@user_events = @user.created_events
@user_attendance = @user.attended_events
@events = @user.events.paginate(page: params[:page])
@upcoming_events = @user.upcoming_events
end
end
3 changes: 2 additions & 1 deletion app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def show_attendance_button(event, attendance)
def show_attendee_lists(attendees)
out = ''
@event.attendees.each do |attendee|
out += "<li>#{attendee.name}</li>"
out += "<li>#{attendee.username}</li>"
end
out.html_safe
end
Expand All @@ -35,4 +35,5 @@ def event_controls(event)
out += link_to 'Back', events_path
out.html_safe
end

end
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ class User < ApplicationRecord
has_many :event_attendees, :foreign_key => :attendee_id

validates :username, length: { maximum: 50 }, uniqueness: { case_sensitive: false }

def attending?(event)
event.attendees.include?(self)
end

def attend!(event)
self.event_attendees.create!(attended_event_id: event.id)
end

def cancel!(event)
self.event_attendees.find_by(attended_event_id: event.id).destroy
end
end
15 changes: 6 additions & 9 deletions app/views/events/_attend.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<%= form_with(model: attendance) do |form| %>
<div class="field">
<%= form.hidden_field :attended_event_id, value: @event.id %>
</div>

<div class="actions">
<%= form.submit "Attend this Event"%>
</div>
<% end %>
<% if user_signed_in? %>
<%= form_for(current_user.event_attendees.build(attended_event_id: @event.id)) do |f| %>
<%= f.hidden_field :attended_event_id %>
<%= f.submit "Attend", class: "btn btn-sm btn-primary" %>
<% end %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/events/_attend_cancel.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if user_signed_in? && current_user.attending?(@event) %>
<%= render 'cancel' %>
<% else %>
<%= render 'attend' %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/events/_cancel.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= form_for(current_user.event_attendees.find_by(attended_event_id: @event.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Cancel", class: "btn btn-sm btn-default" %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="columns">
<div class="column is-10 is-offset-1">
<div class="column is-1 is-offset-1">
<h1 class="title mt-4">All Events</h1>
<div class="columns">
<div class="column">
Expand Down
53 changes: 29 additions & 24 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
<p>
<strong>Title:</strong>
<%= @event.title %>
</p>
<div class="columns">
<div class="column columns is-half is-offset-one-quarter box mt-6">
<div class="column is-10 is-offset-1">
<p class="title">
<%= @event.title %>
</p>

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

<p>
<strong>Date:</strong>
<%= @event.date %>
</p>
<p>
Date:
<%= @event.date %>
</p>

<p>
<strong>Location:</strong>
<%= @event.location %>
</p>
<p>
Location:
<%= @event.location %>
</p>

<br>
<%= show_attendance_button(@event, @attendance) %>
<p>
<%= render 'attend_cancel' %>
</p>

<strong>Attendees:</strong>
Attendees:

<ol>
<%= show_attendee_lists(@attendees) %>
</ol>
<ol>
<%= show_attendee_lists(@attendees) %>
</ol>

<br>
<%= event_controls(@event) %>
<%= event_controls(@event) %>
</div>
</div>
</div>
12 changes: 6 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<nav class="navbar is-primary is-light">
<div class="navbar-brand">
<%= link_to root_path, class: "navbar-item" do %>
<h1 class="title is-5">Private Event</h1>
<h1 class="title is-4 has-text-white">Private Events</h1>
<% end %>
<div class="navbar-burger burger" data-target="navbarExample">
<span></span>
Expand All @@ -38,21 +38,21 @@
<div class="navbar-item">
<div class="field is-grouped">
<p class="control">
<%= link_to 'New Event', new_event_url, class: "button is-info is-inverted" %>
<%= link_to 'New Event', new_event_url, class: "button is-primary is-inverted" %>
</p>
<% if user_signed_in? %>
<p class="control">
<%= link_to current_user.username, edit_user_registration_path, class: "button is-info" %>
<%= link_to current_user.username, edit_user_registration_path, class: "button is-primary" %>
</p>
<p>
<%= link_to "Logout", destroy_user_session_path, method: :delete, class:"button is-info" %>
<%= link_to "Logout", destroy_user_session_path, method: :delete, class:"button is-primary" %>
</p>
<% else %>
<p class="control">
<%= link_to 'Sign In', new_user_session_path, class: "button is-info" %>
<%= link_to 'Sign In', new_user_session_path, class: "button is-primary" %>
</p>
<p class="control">
<%= link_to 'Sign Up', new_user_registration_path, class: "button is-info" %>
<%= link_to 'Sign Up', new_user_registration_path, class: "button is-primary" %>
</p>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
resources :events
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root "events#index"
resources :event_attendees_path
resources :event_attendees, only: [:create, :destroy]
end

0 comments on commit 267ea99

Please sign in to comment.