Skip to content

Commit

Permalink
* bugfix for :pick => :any questions
Browse files Browse the repository at this point in the history
   * if the last checkbox was deselected, the corresponding responses weren't removed
   * conditions (dependencies) depending on the corresponding question weren't fulfilled
  • Loading branch information
Mathias Kutzner committed Jun 10, 2011
1 parent 0244408 commit 1acfd75
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/surveyor/surveyor_controller_methods.rb
Expand Up @@ -63,20 +63,20 @@ def update
:lock => true) :lock => true)
return redirect_with_message(available_surveys_path, :notice, return redirect_with_message(available_surveys_path, :notice,
t('surveyor.unable_to_find_your_responses')) if @response_set.blank? t('surveyor.unable_to_find_your_responses')) if @response_set.blank?

question_ids = params[:r].values.map{|r| r["question_id"]}.flatten.uniq
parameters_sanitized = ResponseSet.reject_or_destroy_blanks(params[:r]) parameters_sanitized = ResponseSet.reject_or_destroy_blanks(params[:r])
saved = false saved = false
if params[:finish]
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
@response_set.responses.where("question_id" => parameters_sanitized.values.map{|r| r["question_id"]}.flatten.uniq).each{ |r| r.destroy } # ensure that all answers for the submitted questions are removed even if there are no new answers
saved = @response_set. @response_set.responses.where("question_id" => question_ids).each{ |r| r.destroy }
update_attributes( { :responses_attributes => parameters_sanitized }) saved = @response_set.update_attributes( { :responses_attributes => parameters_sanitized })

if params[:finish]
@response_set.complete! if saved @response_set.complete! if saved
saved &= @response_set.save saved &= @response_set.save
end end
else
@response_set.responses.where("question_id" => parameters_sanitized.values.map{|r| r["question_id"]}.flatten.uniq).each{ |r| r.destroy }
saved = @response_set.
update_attributes( { :responses_attributes => parameters_sanitized })
end end


return redirect_with_message(surveyor_finish, :notice, return redirect_with_message(surveyor_finish, :notice,
Expand All @@ -90,13 +90,12 @@ def update
end end


format.js do format.js do
ids, remove, question_ids = {}, {}, [] ids, remove = {}, {}
parameters_sanitized.each do |k,v| parameters_sanitized.each do |k,v|
ids[k] = @response_set.responses. ids[k] = @response_set.responses.
find(:first, :conditions => v).id unless v.has_key?("id") find(:first, :conditions => v).id unless v.has_key?("id")


remove[k] = v["id"] if v.has_key?("id") && v.has_key?("_destroy") remove[k] = v["id"] if v.has_key?("id") && v.has_key?("_destroy")
question_ids << v["question_id"]
end end


render :json => {"ids" => ids, "remove" => remove}. render :json => {"ids" => ids, "remove" => remove}.
Expand Down

5 comments on commit 1acfd75

@kobaltz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One Issue that I'm experiencing with this commit is if I already have items checked off and I go back to the survey and add an additional item, all of the previous items are removed. Only the new items are saved. Also, whenever I check off a few items that I'm changing, I get an error (404) when going to the next tab. It looks like this could be a mapping issue on my end.

@kobaltz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it looks like it's doing on your line 73 is deleting everything. But existing items that were not unchecked are also being deleted. Any idea how to keep those?

@gorenje
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, but the question ids that are deleted are the ones that came over the wire. That shoud really mean that we have new answers for these questions. ok, but i'm not 100% sure, we'll investigate and try to find a fix for this ...

@ewto
Copy link

@ewto ewto commented on 1acfd75 Jun 14, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed by ewto@1947036

@kobaltz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. The pick any is finally working the way it should be. Thanks so much!

Please sign in to comment.