Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modelling #42

Merged
merged 9 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions rails_root/app/controllers/survey_questions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

class SurveyQuestionsController < ApplicationController
before_action :set_survey_question, only: %i[show edit update destroy]

# GET /survey_questions or /survey_questions.json
def index
@survey_questions = SurveyQuestion.all
end

# GET /survey_questions/1 or /survey_questions/1.json
def show; end

# GET /survey_questions/new
def new
@survey_question = SurveyQuestion.new
end

# GET /survey_questions/1/edit
def edit; end

# POST /survey_questions or /survey_questions.json
def create
@survey_question = SurveyQuestion.new(survey_question_params)

respond_to do |format|
if @survey_question.save
format.html { redirect_to survey_question_url(@survey_question), notice: 'Survey question was successfully created.' }
format.json { render :show, status: :created, location: @survey_question }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @survey_question.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /survey_questions/1 or /survey_questions/1.json
def update
respond_to do |format|
if @survey_question.update(survey_question_params)
format.html { redirect_to survey_question_url(@survey_question), notice: 'Survey question was successfully updated.' }
format.json { render :show, status: :ok, location: @survey_question }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @survey_question.errors, status: :unprocessable_entity }
end
end
end

# DELETE /survey_questions/1 or /survey_questions/1.json
def destroy
@survey_question.destroy!

respond_to do |format|
format.html { redirect_to survey_questions_url, notice: 'Survey question was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_survey_question
@survey_question = SurveyQuestion.find(params[:id])
end

# Only allow a list of trusted parameters through.
def survey_question_params
params.require(:survey_question).permit(:text, :explanation, :section)
end
end
131 changes: 84 additions & 47 deletions rails_root/app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# Controller for survey responses
class SurveyResponsesController < ApplicationController
before_action :set_survey_response, only: %i[show edit update destroy]
before_action :set_survey_data, only: %i[show edit update destroy]
before_action :set_survey_sections, only: %i[show edit update new]

# GET /survey_responses or /survey_responses.json
def index
Expand All @@ -15,54 +16,52 @@ def show; end
# GET /survey_responses/new
def new
@survey_response = SurveyResponse.new
@survey_sections = [
{
title: 'Part 1: Leadership Behavior - Management',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?',
questions: %i[leads_by_example ability_to_juggle communicator]
},
{
title: 'Part 2: Leadership Behavior - Interpersonal',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?',
questions: %i[lifelong_learner high_expectations cooperative empathetic people_oriented]
}
]
@questions = SurveyQuestion
end

# GET /survey_responses/1/edit
def edit
# FIXME: DRY
@survey_sections = [
{
title: 'Part 1: Leadership Behavior - Management',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?',
questions: %i[leads_by_example ability_to_juggle communicator]
},
{
title: 'Part 2: Leadership Behavior - Interpersonal',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?',
questions: %i[lifelong_learner high_expectations cooperative empathetic people_oriented]
}
]
end
def edit; end

# POST /survey_responses or /survey_responses.json
def create
# If any of the survey_response_params values are nil, then the form is invalid
if survey_response_params.values.any? { |value| value.nil? || value.empty? || survey_response_params.keys.length != SurveyResponse.column_names.length - 3 }
if survey_response_params.values.any? { |value| value.nil? || value.empty? }
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

else
@survey_response = SurveyResponse.new(survey_response_params)
begin
# FIXME: Validation
profile = get_user_profile_from_params(survey_response_params)
# puts "profile: #{profile.inspect}"
rescue ActiveRecord::RecordNotFound
return respond_with_error 'invalid user_id'
end
@survey_response = SurveyResponse.new(profile:)
# puts "survey_response: #{@survey_response.inspect}"

# puts "survey_response_params: #{survey_response_params.inspect}"
survey_response_params.each do |question_id, choice|
next if question_id == 'user_id'

# puts "question_id: #{question_id}, choice: #{choice}"

# print all the questions in the database
# puts "SurveyQuestion.all: #{SurveyQuestion.all.inspect}"

# FIXME: Validation
question = SurveyQuestion.where(id: question_id).first!
# puts "question: #{question.inspect}"
SurveyAnswer.create(choice:, question:, response: @survey_response)
end

respond_to do |format|
if @survey_response.save
format.html do
puts 'survey response created successfully'
redirect_to survey_response_url(@survey_response), notice: 'Survey response was successfully created.'
end
format.json { render :show, status: :created, location: @survey_response }
Expand All @@ -74,22 +73,33 @@ def create

# PATCH/PUT /survey_responses/1 or /survey_responses/1.json
def update
if survey_response_params.values.any?(&:nil?)
respond_to do |format|
format.html do
redirect_to edit_survey_response_url(@survey_response), notice: 'invalid form', status: :unprocessable_entity
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
format.json { render json: { error: 'invalid form' }, status: :unprocessable_entity }

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

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
end
Expand All @@ -107,13 +117,40 @@ def destroy
private

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

def set_survey_sections
@sections = [
{
title: 'Part 1: Leadership Behavior - Management',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?'
},
{
title: 'Part 2: Leadership Behavior - Interpersonal',
prompt: 'To what extent do you agree the following behaviors reflect your personal leadership behaviors?'
}
]
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)
respond_to do |format|
format.html do
redirect_to new_survey_response_url, notice: message, status:
end
format.json { render json: { error: message }, status: }
end
end

# Only allow a list of trusted parameters through.
def survey_response_params
params.require(:survey_response).permit(:user_id, :leads_by_example, :ability_to_juggle, :communicator,
:lifelong_learner, :high_expectations, :cooperative, :empathetic, :people_oriented)
params.require(:survey_response).permit! # FIXME: Figure out how to use strong params with new model
end
end
4 changes: 4 additions & 0 deletions rails_root/app/helpers/survey_questions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module SurveyQuestionsHelper
end
10 changes: 4 additions & 6 deletions rails_root/app/helpers/survey_responses_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
module SurveyResponsesHelper
# method to calculate the average score of a survey response
def average_score(survey_response)
score_fields = %i[leads_by_example ability_to_juggle communicator lifelong_learner high_expectations
cooperative empathetic people_oriented]
scores = score_fields.map { |field| survey_response.send(field) }
scores.sum.to_f / scores.size
# returns the average score of the survey response
survey_response.answers.average(:choice).to_f
end

# method to format the date of a survey response
Expand All @@ -17,7 +15,7 @@ def formatted_date(survey_response)

# method to find the user of a survey response
def user_of_response(survey_response)
# returns user_id of the survey response
survey_response.user_id
# returns profile_id of the survey response
survey_response.profile_id
end
end
9 changes: 9 additions & 0 deletions rails_root/app/models/survey_answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class SurveyAnswer < ApplicationRecord
belongs_to :question,
class_name: 'SurveyQuestion'

belongs_to :response,
class_name: 'SurveyResponse'
end
4 changes: 4 additions & 0 deletions rails_root/app/models/survey_profile.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# frozen_string_literal: true

class SurveyProfile < ApplicationRecord
has_many :responses,
foreign_key: :profile_id,
class_name: 'SurveyResponse',
dependent: :destroy
end
4 changes: 4 additions & 0 deletions rails_root/app/models/survey_question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SurveyQuestion < ApplicationRecord
end
13 changes: 13 additions & 0 deletions rails_root/app/models/survey_response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# frozen_string_literal: true

class SurveyResponse < ApplicationRecord
has_many :answers,
foreign_key: :response_id,
class_name: 'SurveyAnswer',
dependent: :delete_all

validates_associated :answers

has_many :questions,
class_name: 'SurveyQuestion',
through: :answers

belongs_to :profile,
class_name: 'SurveyProfile'
end
32 changes: 32 additions & 0 deletions rails_root/app/views/survey_questions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_with(model: survey_question) do |form| %>
<% if survey_question.errors.any? %>
<div style="color: red">
<h2><%= pluralize(survey_question.errors.count, "error") %> prohibited this survey_question from being saved:</h2>

<ul>
<% survey_question.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :text, style: "display: block" %>
<%= form.text_area :text %>
</div>

<div>
<%= form.label :explanation, style: "display: block" %>
<%= form.text_area :explanation %>
</div>

<div>
<%= form.label :section, style: "display: block" %>
<%= form.text_field :section %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
17 changes: 17 additions & 0 deletions rails_root/app/views/survey_questions/_survey_question.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="<%= dom_id survey_question %>">
<p>
<strong>Question text:</strong>
<%= survey_question.text %>
</p>

<p>
<strong>Explanation:</strong>
<%= survey_question.explanation %>
</p>

<p>
<strong>Section:</strong>
<%= survey_question.section %>
</p>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

json.extract! survey_question, :id, :text, :explanation, :section, :created_at, :updated_at
json.url survey_question_url(survey_question, format: :json)
10 changes: 10 additions & 0 deletions rails_root/app/views/survey_questions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing survey question</h1>

<%= render "form", survey_question: @survey_question %>

<br>

<div>
<%= link_to "Show this survey question", @survey_question %> |
<%= link_to "Back to survey questions", survey_questions_path %>
</div>
Loading
Loading