Skip to content

Commit

Permalink
added locking and transation to surveyor update action. Prevents bug …
Browse files Browse the repository at this point in the history
…that caused duplicated answers
  • Loading branch information
brian-lc committed Mar 4, 2010
1 parent 8edce68 commit 4f5fc02
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions app/controllers/surveyor_controller.rb
Expand Up @@ -60,23 +60,25 @@ def edit
end

def update
if @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer})
@response_set.current_section_id = params[:current_section_id]
else
flash[:notice] = "Unable to find your responses to the survey"
redirect_to(available_surveys_path) and return
end
saved = nil
ActiveRecord::Base.transation do
if @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer},:lock => true)
@response_set.current_section_id = params[:current_section_id]
else
flash[:notice] = "Unable to find your responses to the survey"
redirect_to(available_surveys_path) and return
end

if params[:responses] or params[:response_groups]
@response_set.clear_responses
saved = @response_set.update_attributes(:response_attributes => (params[:responses] || {}).dup ,
:response_group_attributes => (params[:response_groups] || {}).dup) #copy (dup) to preserve params because we manipulate params in the response_set methods
if (saved && params[:finish])
@response_set.complete!
saved = @response_set.save!
if params[:responses] or params[:response_groups]
@response_set.clear_responses
saved = @response_set.update_attributes(:response_attributes => (params[:responses] || {}).dup ,
:response_group_attributes => (params[:response_groups] || {}).dup) #copy (dup) to preserve params because we manipulate params in the response_set methods
if (saved && params[:finish])
@response_set.complete!
saved = @response_set.save!
end
end
end

respond_to do |format|
format.html do
if saved && params[:finish]
Expand Down

0 comments on commit 4f5fc02

Please sign in to comment.