Class: Questionable::AnswersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/questionable/answers_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'app/controllers/questionable/answers_controller.rb', line 5

def create
  answers = params[:answers]

  unless current_user and answers.is_a?(Hash)
    flash[:warn] = 'Something went wrong, please try again'
    return redirect_to_back
  end

  # Answers should always be a hash, and the values should be arrays,
  # even if the question input_type only supports a single answer.

  answers.each do |assignment_id, answers|
    assignment = Assignment.find(assignment_id)
    assignment.answers.where(user_id: current_user.id).delete_all

    input_type = assignment.question.input_type

    if input_type == 'string'
      message = answers.first || ''
      assignment.answers.create(user_id: current_user.id, message: message[0..255])
    elsif input_type == 'date'
      date = answers.first
      if date[:year].present? or date[:month].present? or date[:day].present?
        if date[:year].present? and date[:month].present? and date[:day].present?
          answer = assignment.answers.build(user_id: current_user.id, message: "#{date[:year]}-#{date[:month]}-#{date[:day]}")
          if answer.date_answer.nil?
            flash[:warn] = 'Could not save date. Invalid date.'
          else
            answer.save
          end
        else
          flash[:warn] = 'Could not save date. You did not select all three fields.'
        end
      end
    else
      option_ids = answers
      option_ids.each do |oid|
        unless oid.blank?
          assignment.answers.create(user_id: current_user.id, option_id: oid)
        end
      end
    end
  end

  redirect_to_back
end