CSC/ECE 517 Fall 2015/oss E1559 rrz: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 51: Line 51:
|-
|-
| config/routes.rb ||170 ||  
| config/routes.rb ||170 ||  
def index                                               
  resources :join_team_requests do
    @join_team_requests = JoinTeamRequest.all                   
     collection do
     respond_to do |format|                                     
       get :edit
       format.html # index.html.erb                               
    end
      format.xml  { render :xml => @join_team_requests }     
  end
end  
|| resources :join_team_requests do
||def index
     collection do
     @join_team_request = JoinTeamRequest.all
      post :decline
     render_request # index.html.erb
      get :edit
end
     end
  end
|-
|-
|    ||
|    ||

Revision as of 17:23, 31 October 2015

E1559: Refactoring JoinTeamRequestsController and InvitationController classes

This page gives a breif description on the expertiza based oss project that aimed at refactoring JoinTeamRequestController.rb and InvitationController.rb classes so that they follow the DRY principle and Ruby on Rails coding practices.

Code Refactoring

Code refactoring is process of changing the code to make it more maintainable, without changing the functionality of the code. Some of the reasons for performing refactoring are:

  • To remove duplicate code.
  • To make the code more maintainable.
  • To divide functionality of the class

Introduction to Expertiza

Expertiza is a web application where students can submit and review learning objects like code, writings, etc. It gives scope for creation of reusable learning objects. Students submit assignments, which can than graded through peer reviews. The Expertiza project is supported by the National Science Foundation.

Problem Statement

Classes involved

JoinTeamRequestsController and InvitationController

What the controller does

invitation_controller.rb is used by a user to invite other users to join his/her team. It performs validation before creating a request. Now, invited user can accept or reject the request. join_team_requests_controller.rb is used when user decides to join a team.

What's wrong with the code

The invitation_controller.rb is doing the required task but it is difficult to understand the code, hence it becomes difficult to maintain the code. And the functions in the accept and create method can be broken down into separate methods.

In join_team_request_controller.rb has duplicate code in various methods which can be removed by creating a separate method for this common code.

What needs to be done

invitation_controller.rb

  • Rename to Invitations_Controller.rb, as is not in accordance with current naming convention.
  • Add comments explaining what each method does, and comments on how important variables are used as currently there are no comments.
  • Refactor create and accept methods. Shorten and clarify them by adding private methods, as create and accept methods currently have a lot of code.
  • Change the find_by_sql call(s) to Rails (Active Record) statements.
  • Make sure that it can be used by a user with a TA or instructor account, if they are participating in this assignment.
  • Change grammatically wrong or awkward flash messages.

join_team_requests_controller.rb

  • Add comments to the code.
  • Remove duplicate code.
  • Update create and accept method to share code.
  • Decline and destroy method should check for successful operation before returning.
  • Change grammatically wrong or awkward flash messages.

Implementation

Renamed invitation_controller.rb to invitations_controller.rb

The controller renaming had to be incorporated in different files of the project. The table contain the file names along with the lines before and after the changes.

File Name Line no. Before After
config/routes.rb 160 resources :invitation do resources :invitations do
config/routes.rb 170
 resources :join_team_requests do
   collection do
     get :edit
   end
 end
resources :join_team_requests do
   collection do
     post :decline
     get :edit
   end
 end
app/views/student_teams/view.html.erb 151 {:controller => 'invitation', :action => 'cancel', :inv_id => inv.id, :student_id => @student.id} %> {:controller => 'invitations', :action => 'cancel', :inv_id => inv.id, :student_id => @student.id} %>
app/views/student_teams/view.html.erb 193 {:controller => 'invitation', :action => 'accept', :inv_id => inv.id, :student_id => @student.id, :team_id => @team_id}, {:controller => 'invitations', :action => 'accept', :inv_id => inv.id, :student_id => @student.id, :team_id => @team_id},
app/views/student_teams/view.html.erb 198 {:controller => 'invitation', :action => 'decline', :inv_id => inv.id, :student_id => @student.id} %> {:controller => 'invitations', :action => 'decline', :inv_id => inv.id, :student_id => @student.id} %>
app/views/student_teams/view.html.erb 217 <%= form_tag :controller => 'invitation', :action => 'create' do %> <%= form_tag :controller => 'invitations', :action => 'create' do %>
app/controllers/invitations_controller.rb 1 class InvitationController < ApplicationController class InvitationsController < ApplicationController

JoinTeamRequestController.rb removed duplicate code

Before After
def index
   @join_team_requests = JoinTeamRequest.all                     
   respond_to do |format|                                       
     format.html # index.html.erb                                
     format.xml  { render :xml => @join_team_requests }       
end 
def index
   @join_team_request = JoinTeamRequest.all
   render_request # index.html.erb
end
def show # searches the join team requests for a particular id
    @join_team_request = JoinTeamRequest.find(params[:id]) 
    respond_to do |format|
      format.html # show.html.erb
     format.xml  { render :xml => @join_team_request }
   end
 end

def new # create a new join team request entry instance

    @join_team_request = JoinTeamRequest.new
   respond_to do |format|
     format.html # new.html.erb
     format.xml  { render :xml => @join_team_request }
   end

end

private def render_request
     respond_to do |format|
     format.html 
     format.xml  { render :xml => @join_team_request } 
    end
  end

def show # searches the join team requests for a particular id

    @join_team_request = JoinTeamRequest.find(params[:id]) 
     render_request # show.html.erb
  end

def new # create a new join team request entry instance

    @join_team_request = JoinTeamRequest.new
    render_request # new.html.erb
  end