CSC/ECE 517 Fall 2014/oss E1462 nms

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza - Refactoring SurveyResponsesController

Introduction

Expertiza<ref name="expertiza>Expertiza http://wikis.lib.ncsu.edu/index.php/Expertiza</ref> is an open source web portal for the use of both students and professors. Expertiza allows us to create reusable learning objects through peer review. It allows project submission, team creation and the submission of almost any document type, including URLs and wiki pages. Students can access their pending and finished assignments, they can do peer reviews on several topics and projects. It is developed on Ruby on Rails platform. More information on Expertiza can be found here. The source code has been forked and cloned for making modifications to the to survey responses controller.

This wiki provides an insight into our contributions to the OSS Expertiza application, focusing on Refactoring the SurveyResponses Controller.

Project Description

This class creates surveys and records the responses which can be viewed. On submitting the responses, the scores and comments are posted.

Classes : SurveyResponsesController.rb
What it does : Creates surveys, submitting surveys, views responses, posts scores and comments.
What has to be changed :

  • Pluralize the class SurveyResponseController to SurveyResponsesController
  • Changing declarations of Arrays and Hashes,removing commented out code
  • Use of routing helpers instead of hardcoded URLs
  • Move active record queries to the model or another class
  • Reducing the number of instance variables per action to one

Modification to Existing Code

Pluralize the SurveyResponseController class to SurveyResponsesController

  • A new survey_responses_controller.rb file is added : As per Ruby on Rails convention, controller names get pluralized while model names are singular. So, the controller name becomes survey_responses_controller instead of survey_response_controller for SurveyResponse modelclass.

Changing declarations of Arrays and Hashes as per Ruby 1.9 standards

  • Modified declarations of Arrays and Hashes

Before Refactoring : survey_responses_controller.rb

#view_responses
  @responses = Array.new
    
#view_responses
  this_response_survey = Hash.new  

After Refactoring :

#view_responses
  @responses = []
    
#view_responses
  this_response_survey = {}

Move active record queries to the model or another class

Controllers are best at parsing the inputs, they call the appropriate models, and then format the outputs. It is desirable to have a skinny controller responsible for parsing inputs and models doing actual validation. A bunch of Active Record queries that existed in survey_responses_controller have been moved to SurveyResponse model.

Before Refactoring: survey_responses_controller.rb

#view_responses
     if !params[:course_eval]
      surveylist = SurveyResponse.where( ["assignment_id = ? and survey_id = ?", params[:id], survey.id])
    else
      surveylist = SurveyResponse.where( ["survey_deployment_id = ? and survey_id = ?", params[:id], survey.id])

After Refactoring: survey_responses_controller.rb SurveyResponse.get_survey_list and SurveyResponse.get_survey_list_with_deploy_id methods have been created in SurveyResponse model for active record operations.

     #view_responses
     if !params[:course_eval]
      survey_list = SurveyResponse.get_survey_list(params[:id], survey.id)
     else
      survey_list = SurveyResponse.get_survey_list_with_deploy_id( params[:id], survey.id)

Reducing the number of instance variables per action to one

It desirable not to have more than one instance variables in a controller action as it indicates increased coupling. Our goal should be reducing coupling as much as possible and view should have direct access to as few instance variables as possible.

Before Refactoring: survey_responses_controller.rb

#submit
     if !params[:survey_deployment_id]
    @surveys = SurveyHelper::get_assigned_surveys(@assignment_id)

After Refactoring: survey_responses_controller.rb

#submit
     if !params[:survey_deployment_id]
    surveys = SurveyHelper::get_assigned_surveys(@assignment_id)

Removing comments and cleaning up code

Comments and extra spaces have been removed and conventions have been followed to enhance readability of the code.

Testing

On VCL session there are 2 instances running. On port 3000 of host http://152.46.18.142/ the original instance is setup and on port 3000 of host the refactored instance is setup. In below images we have shown that the output after refactoring is same.

Note for testing : It is recommended that in order to test the survey response controller functionality a user should have courses evaluation surveys pending.

View of Refactored Begin Survey View of Original Begin Survey
View of Re-factored Course Evaluation Survey View of Original Course Evaluation Survey
View of Re-factored View Responses View of Original View Responses
View of Re-factored Pending Course Evaluations View of Original Pending Course Evaluations

References

Git Repository

<references/>