CSC/ECE 517 Spring 2021 - E2107. Refactor grades controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

Problem Statement

Expertiza has a functionality that allows students and instructors to receive and give grades, respectively. This functionality is implemented in the grades_controller.rb file. This controller currently violates some essential Rails design principles including having too much calculation and having methods' names that violate Ruby naming convention.

Project Task

Here are the refactoring steps needed to be taken in order to fix the grades_controller.rb .

  • Create a new private method to encapsulate the population of the view model for all the necessary data (lines 102-107).
  • Simplify the logic surrounding the error flashing. The nested logic seems unnecessary (lines 190-192).
  • In line 300:
    • Increase readability by fixing whitespace according to the common Ruby Programming guidelines.
    • Return is not needed here.
    • Remove space between ‘!’ and its argument.
    • Use meaningful variable names and break up the expression.
  • Move these functions into grades_helper.rb and change their names to be more Ruby-like:
    • calculate_all_penalties
    • calculate_penalty_attributes
    • mean (also cease the usage of the assignment)
    • calculate_average_vector
    • build_score_vector
    • Remove_negative_scores_and_build_charts
  • The previous programmer used update_attribute, which is concerning, as validations are ignored (Line 153 and 219).

Refactoring Grades Controller

  • Comment changed that was wrong ("head" into "heat")

  • Line 102-107 (New Private Method made to encapsulate this)

The private method is called instead

Private method details

  • Line 190-192

Simplify Logic

  • Line 300

Boolean expression clean up with variables 
and white spaces/return statement removed

  • Lines 153/219
153 function removed

219 function removed

Testing Code

To test this controller, run the following commands to verify your changes do not break any core functions.

rspec spec/controllers/grades_controller_spec.rb

Important views for using refactored functions

app/views/student_task/view.html.erb app/views/grades/_teammate_reviews.html.erb app/views/grades/_author_feedbacks.html.erb app/views/grades/_tabbed_participant.html.erb app/views/grades/_scores.html.erb app/views/grades/_teammate.html.erb app/views/grades/_reviews.html.erb app/views/grades/_review_table.html.erb app/views/grades/_metareviews.html.erb app/views/grades/_participant.html.erb app/views/grades/_tabbed_reviews.html.erb app/views/grades/_submissions.html.erb app/views/student_task/view.html.erb app/views/grades/_view_heatgrid.html.erb app/views/grades/view_team.html.erb app/views/grades/_heat_grid_tab.html.erb app/views/grades/_participant.html.erb app/views/assignments/list_submissions.html.erb