Class: Course::Assessment::Answer::TextResponse

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/course/assessment/answer/text_response.rb

Instance Method Summary collapse

Instance Method Details

#assign_params(params) ⇒ Object


46
47
48
49
50
# File 'app/models/course/assessment/answer/text_response.rb', line 46

def assign_params(params)
  acting_as.assign_params(params)
  self.answer_text = params[:answer_text] if params[:answer_text]
  self.files = params[:files] if params[:files]
end

#download(dir) ⇒ Object


22
23
24
25
# File 'app/models/course/assessment/answer/text_response.rb', line 22

def download(dir)
  download_answer(dir) unless question.actable.file_upload_question?
  attachments.each { |a| download_attachment(a, dir) }
end

#download_answer(dir) ⇒ Object


27
28
29
30
31
32
# File 'app/models/course/assessment/answer/text_response.rb', line 27

def download_answer(dir)
  answer_path = File.join(dir, 'answer.txt')
  File.open(answer_path, 'w') do |file|
    file.write(normalized_answer_text)
  end
end

#download_attachment(attachment, dir) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/course/assessment/answer/text_response.rb', line 34

def download_attachment(attachment, dir)
  name_generator = FileName.new(File.join(dir, attachment.name), position: :middle,
                                                                 format: '(%d)',
                                                                 delimiter: ' ')
  attachment_path = name_generator.create
  File.open(attachment_path, 'wb') do |file|
    attachment.open(binmode: true) do |attachment_stream|
      FileUtils.copy_stream(attachment_stream, file)
    end
  end
end

#normalized_answer_textObject

Normalize the newlines to \n.


18
19
20
# File 'app/models/course/assessment/answer/text_response.rb', line 18

def normalized_answer_text
  answer_text.strip.encode(universal_newline: true)
end

#reset_answerObject

Specific implementation of Course::Assessment::Answer#reset_answer


11
12
13
14
15
# File 'app/models/course/assessment/answer/text_response.rb', line 11

def reset_answer
  self.answer_text = ''
  save
  acting_as
end