Class: Rapidfire::AnswerGroupBuilder
- Inherits:
-
Object
- Object
- Rapidfire::AnswerGroupBuilder
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- app/models/rapidfire/answer_group_builder.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#params ⇒ Object
Returns the value of attribute params.
-
#question_group ⇒ Object
Returns the value of attribute question_group.
-
#questions ⇒ Object
Returns the value of attribute questions.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(user, question_group, params = {}) ⇒ AnswerGroupBuilder
constructor
A new instance of AnswerGroupBuilder.
- #persisted? ⇒ Boolean
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #to_model ⇒ Object
Constructor Details
#initialize(user, question_group, params = {}) ⇒ AnswerGroupBuilder
Returns a new instance of AnswerGroupBuilder.
10 11 12 13 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 10 def initialize(user, question_group, params = {}) @user, @question_group, @params = user, question_group, params build_answer_group end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
8 9 10 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 8 def answers @answers end |
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 8 def params @params end |
#question_group ⇒ Object
Returns the value of attribute question_group.
8 9 10 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 8 def question_group @question_group end |
#questions ⇒ Object
Returns the value of attribute questions.
8 9 10 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 8 def questions @questions end |
#user ⇒ Object
Returns the value of attribute user.
8 9 10 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 8 def user @user end |
Instance Method Details
#persisted? ⇒ Boolean
6 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 6 def persisted?; false end |
#save(options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 31 def save( = {}) save!() 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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 19 def save!( = {}) 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!() end |
#to_model ⇒ Object
15 16 17 |
# File 'app/models/rapidfire/answer_group_builder.rb', line 15 def to_model @answer_group end |