Total Complexity | 11 |
Total Lines | 64 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | class AttendancesController < ApplicationController |
||
2 | before_action :create_participant, only: :create |
||
3 | |||
4 | load_resource :session |
||
5 | |||
6 | def index |
||
7 | if current_participant |
||
8 | render json: current_participant.sessions_attending |
||
9 | .where(event: Event.current_event) |
||
10 | .pluck(:id) |
||
11 | else |
||
12 | render :unauthorized |
||
13 | end |
||
14 | end |
||
15 | |||
16 | def create |
||
17 | @attendance = @session.attendances.find_or_initialize_by(participant: current_participant) |
||
18 | if @attendance.save |
||
19 | respond_to do |format| |
||
20 | format.html do |
||
21 | flash[:notice] = "Thanks for your interest in this session." |
||
22 | redirect_to @session |
||
23 | end |
||
24 | |||
25 | format.json do |
||
26 | render :partial => 'sessions/participant', :formats => ['html'], :locals => { :participant => current_participant } |
||
27 | end |
||
28 | end |
||
29 | else |
||
30 | respond_to do |format| |
||
31 | format.json do |
||
32 | render :partial => 'sessions/new_participant', :formats => ['html'], :status => :unprocessable_entity |
||
33 | end |
||
34 | end |
||
35 | end |
||
36 | end |
||
37 | |||
38 | def destroy |
||
39 | return render :unauthorized unless current_participant |
||
40 | @session.attendances.where(participant: current_participant).destroy_all |
||
41 | end |
||
42 | |||
43 | private |
||
44 | |||
45 | def create_participant |
||
46 | return if logged_in? |
||
47 | return if params[:attendance].nil? |
||
48 | |||
49 | name, email, password = params[:attendance][:name], params[:attendance][:email], params[:attendance][:password] |
||
50 | |||
51 | participant_session = ParticipantSession.new(:email => email, :password => password) |
||
52 | if participant_session.save |
||
53 | @current_participant_session = participant_session |
||
54 | else |
||
55 | participant = Participant.new(:name => name, :email => email, :password => password) |
||
56 | if participant.save |
||
57 | @current_participant_session = ParticipantSession.create(participant, true) |
||
58 | else |
||
59 | flash[:error] = "There was a problem creating an account for you. Please try again." |
||
60 | redirect_to new_participant_path |
||
61 | end |
||
62 | end |
||
63 | end |
||
64 | end |
||
65 |