aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/answer.rb')
-rw-r--r--app/models/answer.rb31
1 files changed, 7 insertions, 24 deletions
diff --git a/app/models/answer.rb b/app/models/answer.rb
index c4e8a93..4410980 100644
--- a/app/models/answer.rb
+++ b/app/models/answer.rb
@@ -76,36 +76,19 @@ class Answer < ActiveRecord::Base
end
def self.update_from(params)
- ans = Answer.find(params['id'])
-
- if ans.class == Answer
- update = params["answer"] || []
- elsif ans.class == MultipleChoiceAnswer
- params["multiple_choice_answer"] = {} unless params["multiple_choice_answer"]
- params["multiple_choice_answer"]["options"] = params["options"].inject(Array.new){ |a, cur| a.push cur.to_i }
- update = params["multiple_choice_answer"]
- end
-
- result = ans.attributes
+ ans = Answer.find(params['id'])
+ result = ans.attributes
- for u in update
- result[u[0]] = u[1]
- end
+ params[ans.class.to_s.underscore].try.each{ |u| result[u[0]] = u[1] }
- result
+ result
end
def self.new_from(params)
-
- if params.include? "answer"
- Answer.new params["answer"]
- elsif params.include? "multiple_choice_answer"
- ans_hash = params["multiple_choice_answer"]
- new_ans = MultipleChoiceAnswer.new ans_hash
- new_ans.options = params["options"].try.inject(Array.new){ |a, cur| a.push cur.to_i } || []
- return new_ans
+ for klass in [Answer, MultipleChoiceAnswer]
+ name = klass.to_s.underscore
+ return klass.new(params[name]) if params.include? name
end
-
end
def self.wrong_answers_of(uid)