events.rb 2.53 KB
Newer Older
Tyler Lemburg's avatar
Tyler Lemburg committed
1
2
3
4
require 'models/event'
require 'models/event_signup'

get '/:service_space_url_name/events/:event_id/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
5
	require_login
Tyler Lemburg's avatar
Tyler Lemburg committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	load_service_space

	# this is an event details page
	begin
		event = Event.includes(:location, :event_type, :event_signups).find(params[:event_id])
	rescue ActiveRecord::RecordNotFound => e
		not_found
	end

	@breadcrumbs << {:text => event.title}
	erb :event_details, :layout => :fixed, :locals => {
		:event => event
	}
end

post '/:service_space_url_name/events/:event_id/sign_up/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
22
	require_login
Tyler Lemburg's avatar
Tyler Lemburg committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	load_service_space

	# check that is a valid event
	event = Event.includes(:event_type).find_by(:service_space_id => @space.id, :id => params[:event_id])

	if event.nil?
		# that event does not exist
		flash(:danger, 'Not Found', 'That event does not exist')
		redirect '/calendar/'
	end

	if !event.max_signups.nil? && event.signups.count >= event.max_signups
		# that event is full
		flash(:danger, 'Event Full', 'Sorry, that event is full.')
		redirect back
	end

	EventSignup.create(
		:event_id => params[:event_id],
		:name => @user.full_name,
		:user_id => @user.id,
		:email => @user.email
	)

	if event.type.description != 'Free Event'
		# flash a message that this works
		flash(:success, "You're signed up!", "Thanks for signing up! Don't forget, #{event.title} is #{event.start_time.in_time_zone.strftime('%A, %B %d at %l:%M %P')}.")
		redirect back
	else
		# flash a message that this works
		flash(:success, "Event Marked", "This free event has been marked on your hoempage.")
		redirect back
	end
end

58
59
60
61
62
63
64
65
66
67
68
69
70
get '/:service_space_url_name/events/:event_id/event.ics' do 
	require_login
	load_service_space

	event = Event.find_by(:service_space_id => @space.id, :id => params[:event_id])
	if event.nil?
		raise Sinatra::NotFound
	end

	attachment "#{event.title}.ics"
	event.to_ics
end

Tyler Lemburg's avatar
Tyler Lemburg committed
71
post '/:service_space_url_name/events/:event_id/remove_signup/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
72
	require_login
Tyler Lemburg's avatar
Tyler Lemburg committed
73
74
75
76
77
78
79
80
81
82
	load_service_space

	# get the event
	event = Event.includes(:event_type).where(:id => params[:event_id]).first

	# check that the signup exists
	signup = EventSignup.where(:event_id => params[:event_id], :user_id => @user.id).first

	if signup.nil?
		flash :alert, 'Not Found', 'That signup was not found.'
83
		redirect "#{params[:service_space_url_name]}"
Tyler Lemburg's avatar
Tyler Lemburg committed
84
85
86
87
88
89
90
91
92
	end
	signup.delete

	header = event.type.description == 'Free Event' ? 'Event Removed' : 'Signup Removed'
	message = event.type.description == 'Free Event' ? "#{event.title} has been removed from your calendar." : "Your signup for #{event.title} has been removed."

	flash :success, header, message
	redirect '/home/'
end