CSC/ECE 517 Spring 2019 - Project E1903. Create quiz questionnaires controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

Problem Statement

1. The questionnaires_controller.rb has multiple methods to handle quizzes. Some of these methods cater only to quizzes. The controller has to be refactored into 2 controllers by moving the functions related to quizzes and the dependencies into a new controller called quiz_questionnaire_controller.rb.

2. Some methods in the questionnaires_controller.rb are long or have hardcoded parameters. These methods have to be refactored into shorter functions rid of hardcoded parameters.

Changes made to current Implementation

1. Creation of new controller
A new controller called quiz_questionnaire_controller.rb was created using scaffold. The methods in the questionnaire control pertaining to quizzes were moved to the new controller. Certain methods such as create_questionnaire which are to be needed in both classes were refactored as explained in later sections.
The routes.rb file was edited to reflect the new routes to the new controller's actions as shown in image below.

The views for the controller were not created or moved from questionnaires controller. Instead, every action in the quiz_questionnaire controller renders a view from the old controller itself. However, the links and redirect was edited multiple controllers and views (as stated in affected files), so that the action in the right controller was called.

to be filled

3. Refactoring current controller
Problem: Due to multiple refactoring over the years, there are few redundant methods in questionnaire_controller.rb. Additionally, a few of the methods like update_quiz, create contain the redundant piece of code and multiple switch statements.

Refactoring of create_questionnaire method:
The create_questionnaire method from questionnaire_controller.rb is used to create a questionnaire based on the type of questionnaire. The implementation corresponding to the quiz is irrelevant in the context of questionnaire_controller.rb. Newly created quiz_questionnaire_controller.rb undertakes the task of creating a questionnaire of type Quiz and the original method handles the creation of all other types of questionnaires. The control was delegated based on the type of questionnaire and the corresponding method is called.
Refactored create_questionnaire method


Refactored create_quiz_questionnaire method

Refactoring of create method:
The create method is used to create a questionnaire in the questionnaire_controller.rb. Based on the type of questionnaire, the name in the view is changed. Assignment of display type is done using a switch case that takes up 11 lines of code and each case adds "%" to the camel cased value. Solution: The implementation was changed to check if the display type matches with the available type and when matched, uses Regex, split and join methods to evaluate the same in a single line.

Refactoring of update_quiz method:
The update_quiz method is used to update the quiz when the user edits the existing quiz. Iterating over the set of questions, the attributes are updated based on the type of question (viz. Multiple Choice, Radio, True/False). The implementation for two of these types can be changed and can be used as private methods.

Refactoring of save_choices method:
The save_choices method contains a lot of redundant hard coded data that is not accessed/used in the application. These hardcoded variables were removed and the relevant code is refactored to work seamlessly.


4. Creation of new route for quiz_questionnaire_controller and updated the affected links in views
Created a new route quiz_questionnaire for quiz_questionnaire_controller in "routes.rb" and moved the custom created "get/post" routes for quizzes from questionnaires route to it.

 resources :quiz_questionnaire, only: %i[new create edit update] do
    collection do
      get :new_quiz
      post :create_quiz_questionnaire
      get :edit_quiz
      post :update_quiz
    end
  end

Then changed the controller name and route in all the following views which are accessing quiz_questionnaire_controller while keeping the application flow intact -

/app/views/questionnaire/view.html.erb

<% if @questionnaire.type == "QuizQuestionnaire" %>
      <%= link_to 'Edit', :controller => :quiz_questionnaire, :action => :edit_quiz, :id => 
@questionnaire.id, :pid => @participant.id %>|

/app/views/questionnaire/new_quiz.erb

 <%= form_tag :controller => 'quiz_questionnaire', :action => 'create_quiz_questionnaire', 
:pid => @participant_id, :aid => @assignment_id do %>
        <% $disp_flag = 1 %>
        <%= render :partial => 'quiz_questionnaire' %>
        <% $disp_flag = 0 %>

        <%= render :partial => 'new_question_template' %>

        <br>
        <%= submit_tag "Create Quiz", :name=>"save" %>
    <% end %>

/app/views/questionnaire/edit.html.erb

<%= form_tag({:controller=>'quiz_questionnaire', :action=>'update_quiz', :id=>@questionnaire.id, :pid => params[:pid]}, :multipart => true) do %>

/app/views/submitted_content/_main.html.erb

<% if !t_id.nil? && questionnaire = QuizQuestionnaire.where(["instructor_id = ?", t_id]).first %>
      <%= link_to 'View quiz', :controller => :quiz_questionnaire, :action => :view_quiz, :id => questionnaire.id, :pid => participant.id %>
      <br/>
      <% if !questionnaire.taken_by_anyone?%>
        <%= link_to 'Edit quiz', :controller => :quiz_questionnaire, :action => :edit_quiz, :id => questionnaire.id, :pid => participant.id %>
      <% end %>
      <br/>

    <% else %>
      <br/>
      <%= link_to 'Create a quiz', :controller => :quiz_questionnaire, :action => :new_quiz, :model => "QuizQuestionnaire", :private => 0, :aid => @assignment.id, :pid => participant.id %>
      <br/>
    <% end %>

Files affected

The following files were created or refactored 1. /app/controllers/questionnaires_controller.rb
2. /app/controllers/quiz_questionnaire_controller.rb
3. /app/views/questionnaire/view.html.erb
4. /app/views/questionnaire/new_quiz.erb
5. /app/views/questionnaire/edit.html.erb
6. /app/views/submitted_content/_main.html.erb
7. /config/routes.rb
8. /spec/controllers/questionnaires_controller_spec.rb
9. /spec/controllers/quiz_questionnaire_controller_spec.rb

Test Plan

The refactoring of code was done in a manner as to not affect the functionality or the code flow. Therefore, the test plan has not been modified from the original spec file apart from moving quiz_questionnaire relevant tests from questionnaire spec file to quiz_questionnaire spec file. The code is not hosted as there are no new functionalities created. As with other refactor projects, a video of using the UI is attached in the submission.

Team members

1. Abhishek Arya (aarya@ncsu.edu)
2. Nitin Nataraj Kuncham (nkuncha@ncsu.edu)
3. Suraj Siddharudh (ssiddha@ncsu.edu)

References

  1. Expertiza on GitHub
  2. rpec tutorial
  3. Stackoverflow