CSC/ECE 517 Fall 2019 - Project E1947. Refactor quiz questionnaire controller.rb: Difference between revisions
Line 40: | Line 40: | ||
<pre> | <pre> | ||
</pre> | |||
<br> | |||
* The method '''update_quiz''' has been renamed to '''update''' and has been refactored to reduce the line count by breaking functionalities. (The function '''multiple_choice_checkbox''' was renamed to '''update_checkbox''' and '''multiple_choice_radio''' was renamed to '''update_radio''' so as to reduce function name length and bring consistency with methods performing create operations: '''create_radio''', '''create_checkbox''', '''create_truefalse''') | |||
'''Before:''' | |||
<pre> | |||
def update | |||
@questionnaire = Questionnaire.find(params[:id]) | |||
if @questionnaire.nil? | |||
redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] | |||
return | |||
end | |||
if params['save'] && params[:question].try(:keys) | |||
@questionnaire.update_attributes(questionnaire_params) | |||
params[:question].each_key do |qid| | |||
@question = Question.find(qid) | |||
@question.txt = params[:question][qid.to_sym][:txt] | |||
@question.save | |||
@quiz_question_choices = QuizQuestionChoice.where(question_id: qid) | |||
question_index = 1 | |||
@quiz_question_choices.each do |question_choice| | |||
# Call to private method to handle Multile Choice Questions | |||
multiple_choice_checkbox(question_choice, question_index) if @question.type == "MultipleChoiceCheckbox" | |||
multiple_choice_radio(question_choice, question_index) if @question.type == "MultipleChoiceRadio" | |||
if @question.type == "TrueFalse" | |||
if params[:quiz_question_choices][@question.id.to_s][@question.type][1.to_s][:iscorrect] == "True" # the statement is correct | |||
question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '1') : question_choice.update_attributes(iscorrect: '0') | |||
# the statement is correct so "True" is the right answer | |||
else # the statement is not correct | |||
question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '0') : question_choice.update_attributes(iscorrect: '1') | |||
# the statement is not correct so "False" is the right answer | |||
end | |||
end | |||
question_index += 1 | |||
end | |||
end | |||
end | |||
redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] | |||
end | |||
</pre> | |||
'''After:''' | |||
<pre> | |||
def update | |||
@questionnaire = Questionnaire.find(params[:id]) | |||
if @questionnaire.nil? | |||
redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] | |||
return | |||
end | |||
if params['save'] && params[:question].try(:keys) | |||
@questionnaire.update_attributes(questionnaire_params) | |||
params[:question].each_key do |qid| | |||
@question = Question.find(qid) | |||
@question.txt = params[:question][qid.to_sym][:txt] | |||
@question.save | |||
@quiz_question_choices = QuizQuestionChoice.where(question_id: qid) | |||
question_index = 1 | |||
@quiz_question_choices.each do |question_choice| | |||
# Call to private method to handle Multile Choice Questions | |||
update_checkbox(question_choice, question_index) if @question.type == "MultipleChoiceCheckbox" | |||
update_radio(question_choice, question_index) if @question.type == "MultipleChoiceRadio" | |||
update_truefalse(question_choice) if @question.type == "TrueFalse" | |||
question_index += 1 | |||
end | |||
end | |||
end | |||
redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] | |||
end | |||
def update_truefalse(question_choice) | |||
if params[:quiz_question_choices][@question.id.to_s][@question.type][1.to_s][:iscorrect] == "True" # the statement is correct | |||
question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '1') : question_choice.update_attributes(iscorrect: '0') | |||
# the statement is correct so "True" is the right answer | |||
else # the statement is not correct | |||
question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '0') : question_choice.update_attributes(iscorrect: '1') | |||
# the statement is not correct so "False" is the right answer | |||
end | |||
end | |||
</pre> | </pre> | ||
Revision as of 01:23, 29 October 2019
E1947. Refactoring quiz_questionnaire_controller.rb
This page provides a description of the Expertiza based OSS project.
About Expertiza
Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.
Problem Statement
The following tasks were accomplished in this project:
- Rename filename such that it follows the coding standards
- Remove redundant information from method names
- Add comments to methods and complex lines of code
- Remove unsafe reflection method
- Simplify RSpec
- Try to fix issues from Code Climate
About Quiz Questionnaire Controller
This is a new controller, having recently been separated from questionnaires_controller.rb. It is used to allow students to take quizzes. The idea is that the author(s) of submitted work can write a quiz that is given to each reviewer before the reviewer is allowed to review the work. If the reviewer does badly on the quiz, then we know not to trust the review. It is also possible to set up an Expertiza assignment so that some participants just take the quiz and don’t review the work.
Current Implementation
Functionality
- to-do
Drawbacks and Solutions
- to-do
Code improvements
- The method save_changes had a congnitive complexity of _ and
orted
- The method update_quiz has been renamed to update and has been refactored to reduce the line count by breaking functionalities. (The function multiple_choice_checkbox was renamed to update_checkbox and multiple_choice_radio was renamed to update_radio so as to reduce function name length and bring consistency with methods performing create operations: create_radio, create_checkbox, create_truefalse)
Before:
def update @questionnaire = Questionnaire.find(params[:id]) if @questionnaire.nil? redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] return end if params['save'] && params[:question].try(:keys) @questionnaire.update_attributes(questionnaire_params) params[:question].each_key do |qid| @question = Question.find(qid) @question.txt = params[:question][qid.to_sym][:txt] @question.save @quiz_question_choices = QuizQuestionChoice.where(question_id: qid) question_index = 1 @quiz_question_choices.each do |question_choice| # Call to private method to handle Multile Choice Questions multiple_choice_checkbox(question_choice, question_index) if @question.type == "MultipleChoiceCheckbox" multiple_choice_radio(question_choice, question_index) if @question.type == "MultipleChoiceRadio" if @question.type == "TrueFalse" if params[:quiz_question_choices][@question.id.to_s][@question.type][1.to_s][:iscorrect] == "True" # the statement is correct question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '1') : question_choice.update_attributes(iscorrect: '0') # the statement is correct so "True" is the right answer else # the statement is not correct question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '0') : question_choice.update_attributes(iscorrect: '1') # the statement is not correct so "False" is the right answer end end question_index += 1 end end end redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] end
After:
def update @questionnaire = Questionnaire.find(params[:id]) if @questionnaire.nil? redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] return end if params['save'] && params[:question].try(:keys) @questionnaire.update_attributes(questionnaire_params) params[:question].each_key do |qid| @question = Question.find(qid) @question.txt = params[:question][qid.to_sym][:txt] @question.save @quiz_question_choices = QuizQuestionChoice.where(question_id: qid) question_index = 1 @quiz_question_choices.each do |question_choice| # Call to private method to handle Multile Choice Questions update_checkbox(question_choice, question_index) if @question.type == "MultipleChoiceCheckbox" update_radio(question_choice, question_index) if @question.type == "MultipleChoiceRadio" update_truefalse(question_choice) if @question.type == "TrueFalse" question_index += 1 end end end redirect_to controller: 'submitted_content', action: 'view', id: params[:pid] end def update_truefalse(question_choice) if params[:quiz_question_choices][@question.id.to_s][@question.type][1.to_s][:iscorrect] == "True" # the statement is correct question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '1') : question_choice.update_attributes(iscorrect: '0') # the statement is correct so "True" is the right answer else # the statement is not correct question_choice.txt == "True" ? question_choice.update_attributes(iscorrect: '0') : question_choice.update_attributes(iscorrect: '1') # the statement is not correct so "False" is the right answer end end
Automated Testing using RSPEC
- to-do
Testing from UI
to-do
References
- Expertiza on GitHub
- GitHub Project Repository Fork
- The live Expertiza website
- Demo link
- Expertiza project documentation wiki
- Rspec Documentation
- Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin