Class: Rapidfire::AttemptBuilder
- Inherits:
-
BaseService
- Object
- BaseService
- Rapidfire::AttemptBuilder
- Defined in:
- app/services/rapidfire/attempt_builder.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#attempt_id ⇒ Object
Returns the value of attribute attempt_id.
-
#params ⇒ Object
Returns the value of attribute params.
-
#questions ⇒ Object
Returns the value of attribute questions.
-
#survey ⇒ Object
Returns the value of attribute survey.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ AttemptBuilder
constructor
A new instance of AttemptBuilder.
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #to_model ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ AttemptBuilder
Returns a new instance of AttemptBuilder.
5 6 7 8 |
# File 'app/services/rapidfire/attempt_builder.rb', line 5 def initialize(params = {}) super(params) build_attempt(params[:attempt_id]) end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def answers @answers end |
#attempt_id ⇒ Object
Returns the value of attribute attempt_id.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def attempt_id @attempt_id end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def params @params end |
#questions ⇒ Object
Returns the value of attribute questions.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def questions @questions end |
#survey ⇒ Object
Returns the value of attribute survey.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def survey @survey end |
#user ⇒ Object
Returns the value of attribute user.
3 4 5 |
# File 'app/services/rapidfire/attempt_builder.rb', line 3 def user @user end |
Instance Method Details
#save(options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/rapidfire/attempt_builder.rb', line 47 def save( = {}) save!() rescue ActiveRecord::ActiveRecordError => e errors.add(:base, e.) # repopulate answers here in case of failure as they are not getting updated @answers = @survey.questions.collect do |question| @attempt.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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/services/rapidfire/attempt_builder.rb', line 14 def save!( = {}) params.each do |question_id, answer_attributes| answer = @attempt.answers.find { |a| a.question_id.to_s == question_id.to_s } next unless answer text = answer_attributes[:answer_text] # in case of checkboxes, values are submitted as an array of # strings. we will store answers as one big string separated # by delimiter. text = text.values if text.is_a?(ActionController::Parameters) answer.answer_text = if text.is_a?(Array) strip_checkbox_answers(text).join(Rapidfire.answers_delimiter) else text end case answer.question when Rapidfire::Questions::MultiFile answer.files = answer_attributes[:files] when Rapidfire::Questions::File answer.file = answer_attributes[:file] end end if Rails::VERSION::MAJOR >= 5 @attempt.save! else @attempt.save!() end end |
#to_model ⇒ Object
10 11 12 |
# File 'app/services/rapidfire/attempt_builder.rb', line 10 def to_model @attempt end |