Skip to content

Commit

Permalink
Survey/Responses: Move response creation from params out of controller
Browse files Browse the repository at this point in the history
  • Loading branch information
barnden committed Mar 19, 2024
1 parent 6c15672 commit 643a498
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 71 deletions.
94 changes: 23 additions & 71 deletions rails_root/app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,14 @@ def edit; end

# POST /survey_responses or /survey_responses.json
def create
if invalid_form?
handle_invalid_form
else
create_survey_response
end
end
return respond_with_error 'invalid_form' if invalid_form?

def invalid_form?
survey_response_params.values.any? { |value| value.nil? || value.empty? }
end

def handle_invalid_form
respond_to do |format|
format.html do
redirect_to new_survey_response_url, notice: 'invalid form', status: :unprocessable_entity
end
format.json { render json: { error: 'invalid form' }, status: :unprocessable_entity }
end
end

def create_survey_response
begin
profile = get_user_profile_from_params(survey_response_params)
@survey_response = SurveyResponse.create_from_params survey_response_params
rescue ActiveRecord::RecordNotFound
return respond_with_error 'invalid user_id'
return respond_with_error 'invalid survey response'
end
# create new survey response with unique share code
@survey_response = SurveyResponse.new(profile:, share_code: SecureRandom.hex(3))

create_survey_answers

save_survey_response
end

def create_survey_answers
survey_response_params.each do |question_id, choice|
next if question_id == 'user_id'

question = SurveyQuestion.where(id: question_id).first!
SurveyAnswer.create(choice:, question:, response: @survey_response)
end
end

def save_survey_response
respond_to do |format|
if @survey_response.save
format.html do
Expand All @@ -90,35 +54,20 @@ def save_survey_response
# logic removed because it is not used in the application
# PATCH/PUT /survey_responses/1 or /survey_responses/1.json
def update
# return respond_with_error 'invalid form' if survey_response_params.values.any?(&:nil?)

# begin
# # FIXME: Validation
# get_user_profile_from_params(survey_response_params)
# rescue ActiveRecord::RecordNotFound
# return respond_with_error 'invalid user id'
# end

# respond_to do |format|
# survey_response_params.each do |question_id, choice|
# next if question_id == 'user_id'

# begin
# # FIXME: Validation
# question = SurveyQuestion.where(id: question_id).first!
# answer = SurveyAnswer.where(question:, response: @survey_response).first!
# rescue ApplicationRecord::RecordNotFound
# return respond_with_error 'invalid question or answer'
# end

# answer.update(choice:)
# end

# format.html do
# redirect_to survey_response_url(@survey_response), notice: 'Survey response was successfully updated.'
# format.json { render :show, status: :ok, location: @survey_response }
# end
# end
return respond_with_error 'invalid form' if invalid_form?

begin
@survey_response.update_from_params survey_response_params
rescue ActiveRecord::RecordNotFound
return respond_with_error 'invalid user id'
end

respond_to do |format|
format.html do
redirect_to survey_response_url(@survey_response), notice: 'Survey response was successfully updated.'
format.json { render :show, status: :ok, location: @survey_response }
end
end
end

# DELETE /survey_responses/1 or /survey_responses/1.json
Expand All @@ -135,7 +84,7 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_survey_data
@survey_response = SurveyResponse.find(params[:id])
@survey_response = SurveyResponse.find params[:id]
@questions = @survey_response.questions
end

Expand Down Expand Up @@ -164,14 +113,17 @@ def set_survey_sections
{
title: 'Part 4. Values, Attitudes, and Beliefs',
prompt: 'To what extent do you agree the following apply to your external community
(board, management, citizens)?'
(board, management, citizens)?'
}
]
end

def invalid_form?
survey_response_params.values.any? { |value| value.nil? || value.empty? }
end

def get_user_profile_from_params(params)
SurveyProfile.where(user_id: params[:user_id]).first!
# params.delete(:user_id)
end

def respond_with_error(message, status = :unprocessable_entity)
Expand Down
34 changes: 34 additions & 0 deletions rails_root/app/models/survey_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,38 @@ class SurveyResponse < ApplicationRecord
class_name: 'SurveyProfile'

has_many :invitations

def self.create_from_params(params)
# FIXME: When we look up things and fail, we should use more descriptive exceptions instead of ActiveRecord::RecordNotFound
profile = SurveyProfile.where(user_id: params[:user_id]).first!

# FIXME: Handle share code already existing
survey_response = SurveyResponse.new profile:, share_code: SecureRandom.hex(3)

params.each do |key, choice|
next if key == 'user_id'

question = SurveyQuestion.find key
SurveyAnswer.create choice:, question:, response: survey_response
end

survey_response
end

def update_from_params(params)
# FIXME: When we look up things and fail, we should use more descriptive exceptions instead of ActiveRecord::RecordNotFound
print self

params.each do |key, choice|
if key == 'user_id'
profile = SurveyProfile.where(user_id: params[:user_id]).first!
update profile:
else
question = SurveyQuestion.find key
answer = SurveyAnswer.where(question:, response: self).first!

answer.update choice:
end
end
end
end

0 comments on commit 643a498

Please sign in to comment.