diff options
-rw-r--r-- | app/models/answer.rb | 31 | ||||
-rw-r--r-- | app/views/taglibs/forms.dryml | 3 | ||||
-rw-r--r-- | app/views/taglibs/inputs.dryml | 4 | ||||
-rw-r--r-- | lib/rich_types/check_list.rb | 7 |
4 files changed, 14 insertions, 31 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) diff --git a/app/views/taglibs/forms.dryml b/app/views/taglibs/forms.dryml index 9f93b8a..eddfc0f 100644 --- a/app/views/taglibs/forms.dryml +++ b/app/views/taglibs/forms.dryml @@ -30,8 +30,7 @@ <table> <input:question type="hidden"/> <input:owner type="hidden"/> - <input:options/> - <field-list fields="reference, approved, feedback"/> + <field-list fields="reference, approved, feedback, options"/> </table> <div param="actions"> <submit label="#{ht 'answers.actions.save', :default=>['Save']}" param/><or-cancel param="cancel"/> diff --git a/app/views/taglibs/inputs.dryml b/app/views/taglibs/inputs.dryml index 18b1c59..66a8abd 100644 --- a/app/views/taglibs/inputs.dryml +++ b/app/views/taglibs/inputs.dryml @@ -1,9 +1,9 @@ -<def tag="input" for="RichTypes::CheckList" attrs="disabled"> +<def tag="input" for="RichTypes::CheckList" attrs="disabled, name"> <ul> <repeat:options> <li> <%= %{ - <input name="options[]" type="checkbox" #{this[:checked]?'checked':''} value="#{this[:id]}" id="option-#{this[:id]}" #{'disabled' if disabled}/> + <input name="#{name}[]" type="checkbox" #{this[:checked]?'checked':''} value="#{this[:id]}" id="option-#{this[:id]}" #{'disabled' if disabled}/> } %> <label for="option-<%=this[:id]%>" > <%= this[:content] %> diff --git a/lib/rich_types/check_list.rb b/lib/rich_types/check_list.rb index 7605d62..fe8986b 100644 --- a/lib/rich_types/check_list.rb +++ b/lib/rich_types/check_list.rb @@ -25,9 +25,10 @@ module RichTypes @opt_list = what elsif klass == Array - for i in @opt_list.keys - @opt_list[i][:checked] = what.include?(i) - end + what = what.collect{ |x| x.to_i } + for i in @opt_list.keys + @opt_list[i][:checked] = what.include?(i) + end elsif klass == String # Convert to Array and use = for Arrays |