Skip to content

Commit

Permalink
Update some of the views for users
Browse files Browse the repository at this point in the history
  • Loading branch information
VanessaAoki committed May 4, 2021
1 parent 30031a9 commit 11ec70b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 34 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
13 changes: 13 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@

.navbar {
color: #f7f1f1;
}

.event-title {
color: #0a7565;
font-size: 18px;
font-weight: 600;
}

.event-title:hover { color: black;}

.a-reset {
color: inherit;
text-decoration: none;
}
31 changes: 27 additions & 4 deletions app/controllers/event_attendees_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
class EventAttendee < ApplicationRecord
belongs_to :attendee, :class_name => "User"
belongs_to :attended_event, :class_name => "Event"
def create
@attendance = EventAttendee.new(event_attendance_params)
@attendance.attendee_id = current_user.id

validates :attendee_id, presence: true
validates :attended_event_id, presence: true
respond_to do |format|
if EventAttendee.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

private

def event_attendance_params
params.require(:event_attendance).permit(:attended_event_id)
end
end
28 changes: 27 additions & 1 deletion app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@ module EventsHelper
def show_event_list(events)
out = ''
events.each do |event|
out += "<ul><li>Event Name: #{event.title}</li>"
out += "<div class=\"box\">"
out += "<ul><li>#{link_to event.title, event_url(event), class: 'event-title'}</li>"
out += "<li>Description: #{event.description}</li>"
out += "<li>Date: #{event.date}</li>"
out += "<li>Location: #{event.location}</li></ul>"
out += "</div>"
end
out.html_safe
end

def show_attendance_button(event, attendance)
render 'attend', attendance: attendance if user_signed_in? && event.date > Time.now
end

def show_attendee_lists(attendees)
out = ''
attendees.each do |attendee|
out += "<li>#{attendee.name}</li>"
end
out.html_safe
end

def event_controls(event)
out = ''
if user_signed_in? && current_user.id == event.creator_id
out += link_to 'Edit', edit_event_path(@event)
out += ' | '
out += link_to 'Delete', event, method: :delete, data: { confirm: 'Are you sure?' }
out += ' | '
end
out += link_to 'Back', events_path
out.html_safe
end
end
9 changes: 9 additions & 0 deletions app/views/events/_attend.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= 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 %>
2 changes: 1 addition & 1 deletion app/views/events/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<%= render 'form', event: @event %>
<%= link_to 'Show', @event %> |
<%= link_to 'Show', @event %>
<%= link_to 'Back', events_path %>
18 changes: 6 additions & 12 deletions app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
<div class="columns">
<div class="column is-11 is-offset-1">
<div class="column is-10 is-offset-1">
<h1 class="title mt-4">All Events</h1>
<div class="columns">
<div class="column is-two-fifths">
<div class="column">
<div class="subtitle is-5 mt-4">Upcoming Events:</div>
<div class="upcoming">
<ul>
<ul>
<%= show_event_list(@upcoming_events) %>
</ul>
<button class="button is-primary is-light">Read More</button>
</div>
</div>
<div class="column"></div>

<div class="column is-two-fifths">
<div class="column">
<div class="subtitle mt-4">Past Events:</div>
<div class="upcoming">
<ul>
<%= show_event_list(@past_events) %>
</ul>
<button class="button is-primary is-light">Read More</button>
</div>
</div>
</div>
<p class="button is-info is-light mt-6">
<%= link_to 'New Event', new_event_path %>
<p class="button is-primary is-light mt-6">
<%= link_to 'New Event', new_event_path, class: 'a-reset' %>
</p>
</div>
</div>
19 changes: 5 additions & 14 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
<p>Published By: <%= @event.creator.username %></p>
<p>Description:<%= @event.description %></p>
<p>Date:<%= @event.date %></p>

<% if @event.attendees.any? %>
<% @event.attendees.each do |attendee| %>
<span class="attendee">
<%= attendee.username %>
</span> <br/>
<% end %>
<% else %>
No attendees
<% end %><br/>

<%= show_attendance_button(@event, @attendance) %>
<% show_attendee_lists(@attendees) %>

<div>
<%= link_to "Back", events_path, class: "btn btn-sm btn-default" %>
<%= link_to "Edit", edit_event_path, class: "btn btn-sm btn-success" %>
<%= button_to "Delete", event_path(@event),
method: :delete, data: {confirm: 'Are you sure?'}, class: "btn btn-sm btn-danger" %>
<%= event_controls(@event)%>
</div>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
get 'user/show'
get "/users/:id", to: "users#show", :as => :user_show
devise_for :users, :controllers => { registrations: 'registrations' }
resources :events
resources :event_attendances
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root "events#index"
end

0 comments on commit 11ec70b

Please sign in to comment.