User:Ptrived

From Expertiza_Wiki
Jump to navigation Jump to search

E1614. Refactoring Response Controller

This page provide the details of the changes done as part of the refactoring response controller project(E1614).

A brief overview of Expertiza

Expertiza project is a platform to create reusable learning objects through peer review. It is an open source project which uses Ruby on Rails framework.

Project Statement

The main aim of this project was to refactor the response_controller. The following tasks were completed as part of refactoring in this project:

  • Moving variable declaration to right places.
  • Removing unused variables.
  • Fixing code duplication.
  • Replacing if else block with switch statements.
  • Remove the unreachable code.

About Response Controller

The Response controller is responsible for the CRUD(Create, Read, Update and Delete) operations on responses. The users can fill out a questionnaire such as review rubric or feedback on the partner's contribution. So the ResponseController handles the various operations on the responses, which are objects that Expertiza creates when you fill out a questionnaire.

Changes done as part of refactoring

Moving variable declaration to right places

There were two variables "msg" and "error_msg" in the create method which were declared and initialized to blank strings and then again assigned some string value before it was used in the method. So we moved the variable declaration to the place were it was used and removed the unnecessary variable declaration. Below if the code snippet for the changes done:

 def create
    @map = ResponseMap.find(params[:id]) #assignment/review/metareview id is in params id

    set_all_responses

    #to save the response for ReviewResponseMap, a questionnaire_id is wrapped in the params
    if params[:review][:questionnaire_id]
      @questionnaire = Questionnaire.find(params[:review][:questionnaire_id])
      @round = params[:review][:round]
    else
      @round=nil
    end

    # create the response
    if params[:isSubmit].eql?('Yes')
      is_submitted = true
    else
      is_submitted = false
    end
    @response = Response.create(:map_id => @map.id, :additional_comment => params[:review][:comments],:round => @round, :is_submitted => is_submitted)#,:version_num=>@version)

    #Change the order for displaying questions for editing response views.
    questions=sort_questions(@questionnaire.questions)

    if params[:responses]
       create_answers(params, questions)
    end

    #@map.save
    msg = "Your response was successfully saved."
    error_msg="Error"
    @response.email();
    redirect_to :controller => 'response', :action => 'saving', :id => @map.map_id, :return => params[:return], :msg => msg, :error_msg => error_msg, :save_options => params[:save_options]
  end