User:Ashwin Bharadwaj Lakshmi Venkataramanan/Expertiza

Background
Expertiza provides a dashboard for all the Assignments corresponding to a course and provides absolute control to the Instructors and Teaching Assistant. In addition to Assignments, it encompasses peer reviews where in participants are allowed to provide feedback anonymously about each other's work thereby providing scope for better outcome.

Assignments in Expertiza have their association with many other components like Courses,Reviews,Participants and Scores. The Assignment controller in specific performs various other actions in addition to the CRUD operations applied on its model. These actions sew up the association of an assignment with others like courses etc. There exists an AssignmentForm model which acts as a wrapper for the Assignment and provides further control.

Motivation
This project in particular intends that the students collaborate with each other and work on making enhancements to the code base by applying the concepts of Rails,RSpec, DRY code,Test driven development etc. This provides an opportunity for students to contribute to an open source project and learn further about software deployment etc.

Currently, the Assignment controller holds methods which are deprecated and also has a few lines code in its Edit and Update methods which could be removed to make the code DRY. In addition to that, it houses a few actions where the code could be refactored to make it DRY and follow the Ruby style guide and Rails 4 syntax.

Tasks Identified

 * Refactor edit and update method
 * Remove irrelevant comments from the create action.
 * associate_assignment_with_course and remove_assignment_from_course logic should be moved to model.
 * Change to new redirect method rather using controller and action explicitly.
 * Refactor the update_due_dates from AssignmentForm. using the DRY principle
 * Refactor code to follow ruby style guide and Rails4 syntax.

Classes

 * controllers/assignments_controller.rb
 * models/Assignment.rb

Refactoring Edit function
Edit function contains many manipulations that need be refactored into separate functions. The following block contains manipulations such as checking if due_date name or description is not defined.

This part of the code is moved into a separate function.

There are lots of redundant code in the following snippet.And, constants are hard coded inside the code rather than defining constants.

Redundant code is removed and made it more efficient and understandable.

Hard coded constants are removed and necessary constants are defined in the deadline_helper.rb helper file.

And, manipulations such as checking if metareview, drop_topic,singn_up and team_formation are allowed need to be refactored into separate functions.

The following snippet constructs a string that need to be shown to the user when rubrics are not set by the instructor.

Adding code such as above directly into the controller action made the edit function bulkier. The above snippet is also refactored into a separate function.

Refactoring out into separate functions makes the code more readable and understandable.

Refactoring Update function
Update function of the Assignment controller helps in updating the resources after editing it. The following code retrieves the data of the user logged in. The above statement checks the cookies every time, when we want to retrieve information about current user. As it is inefficient and wrong, we need to use helper functions to retrieve such information.

Explicit URL creation using controller name and controller action is refactored in such a way that helper functions create the URL when required.

Remove irrelevant comments from the create action
The Create action has a few comments which are irrelevant and make the method bulky. These could be safely removed from the code.

associate_assignment_with_course moved to model
This is currently defined as an action in AssignmentsController. The action logic could be moved to Model by passing the current user object to the corresponding method we create in the Model. The method in the model would then return a list of courses which would be used in the View corresponding to this action.

Inside controller action,

Inside model,

remove_assignment_from_course moved to model
All the current logic here, except for the "save" and "redirect_to" calls could be moved to the model. There is a need to create a method in the Model with the same name and then pass an assignment object. All operations can be performed inside this method and then the user can be redirected back to the assignment list page. Inside controller action, Inside model,

Change to new redirect method rather using controller and action explicitly.
According to Ruby style guidelines explicit use of action and controller in redirect calls should be avoided. This helps in easy refactoring in case action names are changed in future. All the redirect calls in assignment controller have been modified to use url path helpers. In controller assignment and action toggle_access, redirect call has been changes as foloows

Refactor the update_due_dates from AssignmentForm using the DRY principle
There exist variables which are declared and initialized but are not used in any part of the code. Such variables could be safely removed without impacting the functionality of the action. Conditional statements which check whether due_date object is present can be avoided. The same piece of code is used at various points of code making it redundant and hence can be converted to a method. There exist dead code which tries to remove due_dates not in update or new list from database. There can never be such a case, so the code can be safely removed.

Impact Analysis

 * Tested using Selenium IDE, the associated objects, assignment and course before and after the change. Testing confirms that the objects and their interactions have not changed.
 * Addition of new methods do not coincide with the performance of currently existent methods.
 * Removal of existing code does not lead to reduced/loss of functionality.

Affected Classes
Changed existent classes
 * controllers/assignments_controller.rb
 * controllers/application_controller.rb
 * models/Assignment.rb
 * models/User.rb
 * views/tree_display/actions/_assignments_actions.html.erb

Running the Project Locally
The project could be run locally by cloning the Github repository expertiza and then running the following commands sequentially.

Testing using Selenium IDE
Selenium IDE is implemented as a Firefox extension. It allows user to record edit and debug test cases. It has a recording feature, which records user actions as they are performed and then exports them as a reusable script in one of many programming languages that can be later executed. The UI automation helps in testing quickly yet rigorously. Major test cases identified are following  Loading assignments Creating assignments Editing assignments Updating due dates for assignments Associating assignments to a course Deleting assignments  The test cases were manually run and recorded once. Then the test cases were re run with changed parameters on the deployed version of the application.

Future Work
The tasks accomplish only a part of the refactoring and as always with any software application there is scope for even more. The following are a few which were identified as part of this project.
 * There exists lot of redundant code which code be refactored into methods.
 * Unused variables and deprecated methods could be ridden off from the code base.
 * The test cases are written only for the methods that came under the scope of this project. But there is room for implementing test cases for the other actions too.
 * In an MVC framework, most of the business logic should be within the Model. Work needs to be done on this front to make it more close to this convention of Rails.