CSC/ECE 517 Fall 2022 - E2277. Further Refactoring questionnaires controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

This page gives a description of the changes made related to the CRUD operations related to the questionnaire of the Expertiza-based OSS project.

Expertiza Background

Expertiza is a web application where students can submit and peer-review learning objects (articles, codes, websites, etc). Instructors add and grade the assignments submitted by students to Expertiza. Students can be assigned in teams based on their selection of the topics. It has functionalities such as peer reviews in which students can provide feedback on others' work which helps peer in better developing the project. The National Science Foundation supports it.

Problems Encountered

This project builds on E2257. That project focused only on simplifying the methods in questionnaires_controller, while this project's objective is wider. It involves looking at any code related to the questionnaire and performing bug fixes, refactoring and cleanup. This code also introduces the changes made in E2257, as PR related to that required some changes to be made before merging. This was done as some of the current changes build upon changes made in the previous round.

Suggestion provided in the documentation of the problem

  • Use of temporary variables instead of instance variables wherever possible to narrow their scope.
  • question.rb contains a large number of constants. Make explicit comments on their usage, and remove them if not used.
  • Since a controller should only create objects of one type the questions_controller could be introduced to create and edit questions moving the logic of dealing with individual questions into a separate controller and improving the separation of responsibility.

What we have considered

  • Checked the flow of the code, by adding comments to figure out unused functions.
  • Check for unused constants.
  • Checked code climate issues found.
  • Found bugs where expected behaviour wasn't happening.

Since this is an important piece of code with usage at a lot of places we had to be very careful with making changes. We were able to fix most of the bugs and issues we found, but there still exist a few that we didn't have enough time to fix. These have been added to future work so that it can be easier for anyone who picks up the project.

The issues we have handled are as follows:

  • Presence of many unused functions in the questionnaire questionnaire_controller.rb
  • Presence of many unused constants in the model questions.rb
  • Unable to update questionnaire parameters.
  • Error in the logic used to assign sequence numbers to questions in a questionnaire.
  • High severity issue in the way questionnaire and question objects are created (Code Climate)
  • Updates made to issues fixed in E2257

Files Changed

  • app/controllers/questionnaires_controller.rb
  • app/models/question.rb
  • app/helpers/questionnaire_helper.rb
  • app/views/questionnaires/_questionnaire.html.erb
  • spec/controllers/questionnaires_controller_spec.rb

Files Added

  • app/helpers/question_helper.rb

Implementation

Remove Unused Functions

The following functions in questionnaire_controller.rb controller was found to be never used and was hence removed. They checked for calls throughout the project and ran the code and testing functionalities after commenting it out, to be sure.

Remove Unused Constants

A similar approach to the ones above was performed to figure out the unused constants before removing them. They were all present in the questions.rb model.

Questionnaire update bug

There was a bug where the questionnaire parameters were getting passed back to the function to get the parameters updated. This was happening as the submit button for that section was activated only during the "creation" of a new questionnaire. The edit was using the same page and thus didn't have the submit which was supposed to send values to the controller. This was fixed by adding a separate button to submit during the edit.

The code was updated from being as follows:

Bug in Sequence number assignment

The initial logic for assigning the sequence number for a question was to just assign it with (no_of_existing_questions)+1. This won't function properly in cases where the instructor deletes questions in the middle and then adds a question.

This issue was fixed by find the max sequence number and incrementing that to get the sequence number of a new question.

Codeclimate high severity issue because of usage of the following method "const_get"

This problem was solved by introducing factory methods for both Questionnaire and Questions object. These methods were then added to the corresponding helper functions. The initial code to create an object was as follows.

After the introduction of the factory method.

The following questionnaire factory method was added to questionnaire_helper.rb

The following question factory method was added to question_helper.rb

Acting on Feedback from previous Pull Request (PR#2472).

  1. Adjusted method names as suggested
  2. Revised the comments section
  3. Removed unnecessary constants used in questions.rb

Testing

Testing Plan

  1. Since we are removing the use of 'Object.const_get' we ran the tests again to see that it is functioning properly.
  2. Removed tests that were testing the methods which are not used in the workflow
  3. Tests were changed to handle the changes made with the sequence number assignment.

RSPEC Test Cases

The current version of expertiza included tests for the questionnaire_controller and qeustions_controller. we plan to modify a few of these tests so that it fits the changes that we have proposed.

To run the tests, run the following commands from the expertiza directory

rspec ./spec/controllers/questionnaires_controller_spec.rb
rspec ./spec/controllers/questions_controller_spec.rb

Manual Testing

Video Link

Testing as Instructor

  1. Login as Instructor.
  2. Click on Questionnaires.
  3. Choose a topic and create a new questionnaire.
  4. Check the save functionality.
  5. Add a new question to the questionnaire and check the save question functionality.
  6. delete a question and check if it's reflected properly.
  7. change the questionnaires name and try to update the questionnaire.

SOLID Principle

SOLID is a set of rules that guide us on how to achieve that in object-oriented programming. Using SOLID guidelines you can avoid bad design in your code and give a well-structured architecture to your design. Bad design leads to an inflexible and brittle codebase, a simple and small change can result in bugs with bad design.

We implemented the single responsibility principle to divide the questionnaires_controller into questionnaires and questions controller thus ensuring that each controller does only one job.

Factory Design pattern

In Factory pattern, we create objects without exposing the creation logic to the client and refer to a newly created object using a common interface.

We implemented the factory design pattern to create questionnaire objects based on the type selected by the user. We implemented a similar approach for the creation of question objects as well.

Future Work

  • Questionnaire delete functionality does not work, takes you to a broken link error on clicking the delete button.
  • View/Edit advice button is also not working, again takes you to a error page page which claims route doesn't exist.

References

Team Members