Class: Rapidfire::AnswerGroupBuilder

Inherits:
BaseService show all
Defined in:
app/services/rapidfire/answer_group_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#persisted

Constructor Details

#initialize(params = {}) ⇒ AnswerGroupBuilder

Returns a new instance of AnswerGroupBuilder.



5
6
7
8
# File 'app/services/rapidfire/answer_group_builder.rb', line 5

def initialize(params = {})
  super(params)
  build_answer_group
end

Instance Attribute Details

#answersObject

Returns the value of attribute answers.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def answers
  @answers
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def params
  @params
end

#question_groupObject

Returns the value of attribute question_group.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def question_group
  @question_group
end

#questionsObject

Returns the value of attribute questions.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def questions
  @questions
end

#unique_idObject

Returns the value of attribute unique_id.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def unique_id
  @unique_id
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'app/services/rapidfire/answer_group_builder.rb', line 3

def user
  @user
end

Instance Method Details

#save(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/services/rapidfire/answer_group_builder.rb', line 26

def save(options = {})
  save!(options)
rescue Exception => e
  # repopulate answers here in case of failure as they are not getting updated
  @answers = @question_group.questions.collect do |question|
    @answer_group.answers.find { |a| a.question_id == question.id }
  end
  false
end

#save!(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/rapidfire/answer_group_builder.rb', line 14

def save!(options = {})
  params.each do |question_id, answer_attributes|
    if answer = @answer_group.answers.find { |a| a.question_id.to_s == question_id.to_s }
      text = answer_attributes[:answer_text]
      answer.answer_text =
        text.is_a?(Array) ? strip_checkbox_answers(text).join(',') : text
    end
  end

  @answer_group.save!(options)
end

#to_modelObject



10
11
12
# File 'app/services/rapidfire/answer_group_builder.rb', line 10

def to_model
  @answer_group
end