User:Ianiket18/sandbox

'''E1563. Refactoring the Questionnaires Controller'''

=Introduction= Questionnaire Controller is responsible for creating, editing and reviewing quizes. The Questionnaire controller is also used for peer-review, teammate-review and creating and managing quizes. The basic functionality of adding, removing, editing questions and or options is handled by this controller. This wikipedia page explains the Open Source Project of refactoring the questionnaire controller on expertiza.

The need of refactoring 
 * 1) Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp. This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
 * 2) Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.

=Project Overview= The scope of our project is to refactor the questionnaires controller. The questionnaires controller at this moment is very big and hence, difficult to understand. The aim of the project was to distribute the functionality in different controllers, so that the questionnaires controller will be slim and easy to understand.

Classes involved

 * questionnaire.rb
 * quiz_questionnaire.rb
 * questions_controller.rb

Changes made

 * Functionality moved to quiz_questionnaire.rb.
 * edit_advice method was not being used, so it was removed.
 * save_advice moved to the advice_controller.
 * copy, update_quiz, valid_quiz methods were long and have been broken up. clone_questionnaire_details was also broken up and renamed.
 * Added comments to select_questionnaire_type
 * Debug output (print statements) have been removed.
 * Changed code to follow the global rules.
 * save_new_questions, delete_questions, save_questions have been moved to a separate class.
 * A lot of code duplication has been removed.
 * Test cases are written using rspec for the functions in questionnaire_controller.rb.

Project Resources
GitHub Project Repository

VCL IP Address

Expertiza Wiki

=Code Snippets=

Future Consideration

 * Controller was very big than what the name describes. Appropriate functionality needs to be moved to different controllers to make them slim.
 * The Quiz methods are to be treated as any different kind of questionnaire, and the functionality be implemented in the model class of quiz_questionnaire.
 * Turn the questionnaire into a “form object.” The ..._questions methods: save_new_questions, delete_questions, save_questions should be in a separate class.
 * There is a valid_quiz in this controller, but it only check if the quiz is valid when new quiz is created, but not when the existing quiz is updated. The reason is that, the format for params is not the same. Please fix this issue and make the valid _quiz work both for quiz creation and quiz_update.
 * Write tests for quiz creation, for both scenario which the input quiz questions are valid and not valid.
 * Test (and fix if they are broken) the export and import functionality. Write test for importing and exporting questionnaires (Issue 577).

=Object Oriented Design Principles Followed=

The following object oriented design principles were followed during refactoring:
 * Single Responsibility Principle: The single responsibility principle states that every context (class, function, variable, etc.) should have a single responsibility. It was maintained that each method should be involved with a single responsibility and the code that was not related to that particular functionality was moved to other method. This was also taken care of in terms of classes.
 * DRY Principle: Don't Repeat Yourself! The repetitive and redundant code was removed from the associated classes and methods.

=Running Project & Testing Functionality=
 * Test Cases have been implemented for the functionality in quiz_controller.rb
 * This testing has been done using RSpec, a behavior-driven development framework.

=Conclusion=

=References= Expertiza Code Refactoring Wiki Global Rules for Refactoring Why Refactor Single Responsibility Principle