Skip to content

Commit

Permalink
handle saving string values correctly. Check for array first and then…
Browse files Browse the repository at this point in the history
… continue, this was stopping simple string fields from being saved
  • Loading branch information
Gerrit Riessen committed May 11, 2011
1 parent c3fbc6e commit 25038af
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/surveyor/models/response_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ def self.included(base)
unless @@validations_already_included
# Validations
base.send :validates_presence_of, :response_set_id, :question_id, :answer_id

@@validations_already_included = true
end
base.send :include, Surveyor::ActsAsResponse # includes "as" instance method

# Class methods
base.instance_eval do
def applicable_attributes(attrs)
result = HashWithIndifferentAccess.new(attrs)
result[:answer_id] = result[:answer_id].compact.delete_if(&:blank?) if result[:answer_id].is_a?(Array)
if result[:answer_id].present? && result[:string_value] && Answer.exists?(result[:answer_id])
result[:answer_id] = result[:answer_id].
compact.delete_if(&:blank?) if result[:answer_id].is_a?(Array)

if (result[:answer_id].present? && result[:string_value] &&
Answer.exists?(result[:answer_id]))
answer = Answer.find(result[:answer_id])
result.delete(:string_value) unless answer.is_a?(Array)&&answer.response_class && answer.response_class.to_sym == :string
unless answer.is_a?(Array)
result.delete(:string_value) unless (answer.response_class &&
answer.response_class.to_sym == :string)
end
end
result
end
Expand All @@ -46,4 +52,4 @@ def to_s # used in dependency_explanation_helper
end
end
end
end
end

0 comments on commit 25038af

Please sign in to comment.