Completed
Push — master ( f38e39...3e86b8 )
by
unknown
10s
created

AttendancesController.destroy()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 4
rs 10
c 0
b 0
f 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
      head :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
        # Appears after the user logs in by clicking on “Yes! I might attend” while logged out
21
        format.html do
22
          flash[:notice] = "Thanks for your interest in this session."
23
          redirect_to @session
24
        end
25
26
        # Appears when user already logged in
27
        format.json do
28
          render :partial => 'sessions/participant', :formats => ['html'], :locals => { :participant => current_participant }
29
        end
30
      end
31
    else
32
      respond_to do |format|
33
        format.json do
34
          render :partial => 'sessions/new_participant', :formats => ['html'], :status => :unprocessable_entity
35
        end
36
      end
37
    end
38
  end
39
40
  def destroy
41
    return head :unauthorized unless current_participant
42
    @session.attendances.where(participant: current_participant).destroy_all
43
  end
44
45
  private
46
47
  def create_participant
48
    return if logged_in?
49
    return if params[:attendance].nil?
50
51
    name, email, password = params[:attendance][:name], params[:attendance][:email], params[:attendance][:password]
52
53
    participant_session = ParticipantSession.new(:email => email, :password => password)
54
    if participant_session.save
55
      @current_participant_session = participant_session
56
    else
57
      participant = Participant.new(:name => name, :email => email, :password => password)
58
      if participant.save
59
        @current_participant_session = ParticipantSession.create(participant, true)
60
      else
61
        flash[:error] = "There was a problem creating an account for you. Please try again."
62
        redirect_to new_participant_path
63
      end
64
    end
65
  end
66
end
67