CSC/ECE 517 Spring 2022 - E2218: Refactor response controller: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 13: Line 13:


* Sai Naga Vamshi Chidara (schidar@ncsu.edu)
* Sai Naga Vamshi Chidara (schidar@ncsu.edu)
* Akhil Kumar (amengan@ncsu.edu)
* Akhil Kumar Mengani (amengan@ncsu.edu)
* Samson Mulkur (smulkur@ncsu.edu)
* Samson Reddy Mulkur (smulkur@ncsu.edu)


== Code Modifications and Refactorization ==
== Modified Files and Refactorization ==
Described below are the code refactoring tasks and issues addressed by this team. We have also included any pertinent details, reasonings, comments, warnings, etc., corresponding to each task.
A. '''response_controller.rb''' <br>
1.  '''authorize_show_calibration_results'''
      This method is used to authorize if the user(reviewer) is allowed to view the calibration results for the corresponding ''calibration_response_map_id'' and ''review_response_map_id''
      passed as parameters in the URL.
      Ex: https://expertiza.ncsu.edu/response/show_calibration_results_for_student?calibration_response_map_id=165688&review_response_map_id=165790
      When someone tries to change the map_id's, the request should be authorized to check if the user can access the calibration result or not.  


=== METHOD: def set_response ===
B. '''response_helper.rb''' <br>
@response and @map instance variables are initialized the same way across multiple methods. In order to avoid duplication, we created a
1 . Following methods are moved from the ''response_controller.rb'' class to ''response_helper.rb'' class since these methods are called from the controller action methods.
before_action action which will be called first before calling the corresponding methods.
    i) '''current_user_is_reviewer'''
    ii) '''sort_questions'''
    iii) '''store_total_cake_score'''
    iv) '''set_content'''


[[File:Set_response_method.png|1200px|]]
C. '''assignment_questionnaire.rb''' <br>
1. ''get_questions_by_assignment_id''
      Parameters : assignment_id
      When a user clicks on 'calibration results' from UI, questions in the assignment that has been reviewed are displayed.
      A new method is added in the ''AssignmentQuestionnaire'' model class that returns the appropriate questions by querying the ''AssignmentQuestionnaire'' using the ''assignment_id''.
      This method is added to separate the business logic of retrieving the questions from the controller class.


=== METHOD: def award_badge ===
The code for awarding the badge to a participant is written inside the response controller. It is moved to the AwardedBadge model file and called from the response controller.


[[File:RespControllerAwardBadge.png|1200px|]]
D. '''awarded_badge.rb'''  <br>
1. ''award_badge''
    Parameters : participant_id, badge_name
    This method adds the ''badge_name'' to a specific ''participant_id'' in the AwardedBadges table.
    A separate method is created in the ''AwardedBadges'' model class to avoid the controller class from knowing the business logic of adding a badge name to a participant.


[[File:AwardBadgeMethod.png|1200px|]]
    [[File:RespControllerAwardBadge.png|1200px|]]


=== METHOD: update ===
    [[File:AwardBadgeMethod.png|1200px|]]
Made changes to split a line into multiple lines because it is too long.
   
E. '''cake.rb'''  <br>
1. ''store_total_cake_score''
  This method clearly exposes the logic of retrieving the score for a cake question in the ''response_controller.rb'' class which is an incorrect design. 
  So, a model class method ''get_total_score_for_questions'' is added and the same logic of iterating over the Cake questions is moved there.
  Return results for this method remains same here.


[[File:Update_line_long.png|1200px|]]
F. '''response.rb'''  <br>
1. ''calibration_results_info''
    This method in ''response.rb'' model is completely removed because this method only adds more complexity to the model class.
    We refactored the method ''show_calibration_results_for_student'' in the ''response_controller.rb'' class.
    Previously, ''calibration_results_info'' was just doing nothing but getting responses using ''calibration_response_map_id'' and ''review_response_map_id'', and querying ''AssignmentQuestionnaire''.
    Both of these operations can be done by using ''find'' method of model class and adding model class ''get_questions_by_assignment_id'' method.


=== METHOD: create ===
2. ''set_response''
Made changes to split a line into multiple lines because it is too long.
    @response and @map instance variables are initialized the same way across multiple methods.
    In order to avoid duplication, we created a before_action action which will be called first before calling the corresponding methods.


[[File:Create_line_long.png|1200px|]]
    [[File:Set_response_method.png|1200px|]]


== Modified Files ==
3. ''create_or_get_response''
<code>response_controller.rb</code>
    Method name ''populate_new_response'' is changed to ''create_or_get_response'' to convey the functionality of the method. <br>


<code>response_helper.rb</code>
G. '''Code Climate issues''' <br>
1. ''Update'' <br>
    <p> Made changes to split a line into multiple lines because it is too long. </p>


<code>assignment_questionnaire.rb</code>
    [[File:Update_line_long.png|1200px|]]
2. ''Create'' <br>
    <p> Made changes to split a line into multiple lines because it is too long. </p>


<code>awarded_badge.rb</code>
    [[File:Create_line_long.png|1200px|]]


<code>cake.rb</code>


<code>response.rb</code>
== Testing ==
== Testing ==


=== Running Tests ===
=== Running Tests ===
<pre>
  rspec spec/models/responses_spec.rb
</pre>
<pre>
<pre>
   rspec spec/controllers/questionnaires_controller_spec.rb
   rspec spec/controllers/questionnaires_controller_spec.rb

Revision as of 02:13, 22 March 2022

This page details project documentation for the CSC/ECE 517 Spring 2022, "E2218 refactor response_controller.rb" project.


Background

A response is the object that is created when someone fills out a review rubric, such as when one writes a review, gives feedback to a reviewer, or fills out a survey. Responses to the individual rubric items are kept in Answer objects; each Answer object has a response_id to say what Response it is part of. Since response_controller needs to work with many kinds of responses, its code is pretty general. It is not the worst controller in the system, but it would be much clearer if its method names were more descriptive of what they do.

Mentor

Ed Gehringer, efg@ncsu.edu

Team Members

  • Sai Naga Vamshi Chidara (schidar@ncsu.edu)
  • Akhil Kumar Mengani (amengan@ncsu.edu)
  • Samson Reddy Mulkur (smulkur@ncsu.edu)

Modified Files and Refactorization

A. response_controller.rb
1. authorize_show_calibration_results

     This method is used to authorize if the user(reviewer) is allowed to view the calibration results for the corresponding calibration_response_map_id and review_response_map_id 
     passed as parameters in the URL.
     Ex: https://expertiza.ncsu.edu/response/show_calibration_results_for_student?calibration_response_map_id=165688&review_response_map_id=165790
     When someone tries to change the map_id's, the request should be authorized to check if the user can access the calibration result or not. 

B. response_helper.rb
1 . Following methods are moved from the response_controller.rb class to response_helper.rb class since these methods are called from the controller action methods.

   i) current_user_is_reviewer 
   ii) sort_questions
   iii) store_total_cake_score
   iv) set_content

C. assignment_questionnaire.rb
1. get_questions_by_assignment_id

     Parameters : assignment_id 
     When a user clicks on 'calibration results' from UI, questions in the assignment that has been reviewed are displayed.
     A new method is added in the AssignmentQuestionnaire model class that returns the appropriate questions by querying the AssignmentQuestionnaire using the assignment_id.
     This method is added to separate the business logic of retrieving the questions from the controller class.


D. awarded_badge.rb
1. award_badge

   Parameters :  participant_id, badge_name
   This method adds the badge_name to a specific participant_id in the AwardedBadges table. 
   A separate method is created in the AwardedBadges model class to avoid the controller class from knowing the business logic of adding a badge name to a participant.
   
   
   

E. cake.rb
1. store_total_cake_score

  This method clearly exposes the logic of retrieving the score for a cake question in the response_controller.rb class which is an incorrect design.  
  So, a model class method get_total_score_for_questions is added and the same logic of iterating over the Cake questions is moved there.
  Return results for this method remains same here.

F. response.rb
1. calibration_results_info

   This method in response.rb model is completely removed because this method only adds more complexity to the model class.
   We refactored the method show_calibration_results_for_student in the response_controller.rb class. 
   Previously, calibration_results_info was just doing nothing but getting responses using calibration_response_map_id and review_response_map_id, and querying AssignmentQuestionnaire.
   Both of these operations can be done by using find method of model class and adding model class get_questions_by_assignment_id method.

2. set_response

   @response and @map instance variables are initialized the same way across multiple methods.
   In order to avoid duplication, we created a before_action action which will be called first before calling the corresponding methods.
   

3. create_or_get_response

    Method name populate_new_response is changed to create_or_get_response to convey the functionality of the method. 

G. Code Climate issues
1. Update

Made changes to split a line into multiple lines because it is too long.

    

2. Create

Made changes to split a line into multiple lines because it is too long.

    


Testing

Running Tests

  rspec spec/models/responses_spec.rb
  rspec spec/controllers/questionnaires_controller_spec.rb

GitHub links and Pull Request

Link to Expertiza repository: here

Link to the forked repository: here

Link to Pull Request: here