CSC/ECE 517 Spring 2019 - Project E1903. Create quiz questionnaires controller.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 14: Line 14:


3. Refactoring current controller <br>
3. Refactoring current controller <br>
to be filled
'''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 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.
 
[[File:Refactor Create Method.jpg]]


4. Creation of new route for quiz_questionnaire_controller and updated the affected links in views <br>
4. Creation of new route for quiz_questionnaire_controller and updated the affected links in views <br>
Created a new route quiz_questionnaire for quiz_questionnaire_controller in "routs.rb" and moved the custom created "get/post" routes for quizzes from questionnaires route to it.
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.
<pre>
<pre>
  resources :quiz_questionnaire, only: %i[new create edit update] do
  resources :quiz_questionnaire, only: %i[new create edit update] do

Revision as of 00:26, 26 March 2019

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
to be filled

2. Creation of Rspec file for new controller
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 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.

File:Refactor Create Method.jpg

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

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