<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Panand4</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Panand4"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Panand4"/>
	<updated>2026-07-03T15:06:03Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=111534</id>
		<title>CSC/ECE 517 Fall 2017/E1745 Refactor response controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=111534"/>
		<updated>2017-11-02T23:05:04Z</updated>

		<summary type="html">&lt;p&gt;Panand4: /* What all was tested */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.&lt;br /&gt;
&lt;br /&gt;
==='''About response_controller.rb'''===&lt;br /&gt;
The file response_controller.rb handles the operations on responses based on user permissions and redirects the user to the appropriate place after the action is complete. The responses here constitute of peer reviews/questionnaires/survey. The controller takes care of tasks such as creating, saving, editing, updating and deleting these responses.&lt;br /&gt;
&lt;br /&gt;
==='''Tasks accomplished'''===&lt;br /&gt;
1. Refactoring response_controller.rb&lt;br /&gt;
&lt;br /&gt;
Included:&lt;br /&gt;
* Breaking down large methods into smaller chunks of readable reusable code.&lt;br /&gt;
* Changing code to adhere to latest Ruby conventions&lt;br /&gt;
* Creating functions to reuse chunks of code that were previously written multiple times&lt;br /&gt;
&lt;br /&gt;
2. Testing response_controller.rb&lt;br /&gt;
&lt;br /&gt;
Included writing integration test cases to ensure that response_controller redirects to the right places with a given parameter set.&lt;br /&gt;
&lt;br /&gt;
==='''Pull request'''===&lt;br /&gt;
&lt;br /&gt;
The pull request for this task can be viewed at [https://github.com/expertiza/expertiza/pull/1025]&lt;br /&gt;
&lt;br /&gt;
==='''Refactoring Explained'''===&lt;br /&gt;
Without wasting much time, lets jump into the refactoring by describing code which existed previously followed by the changed code.&lt;br /&gt;
&lt;br /&gt;
1) action_allowed? method&lt;br /&gt;
&lt;br /&gt;
Previous Code :-&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      map = response.map&lt;br /&gt;
      assignment = response.map.reviewer.assignment&lt;br /&gt;
      # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
      if map.is_a? ReviewResponseMap&lt;br /&gt;
        reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
      else&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view case is extracted into a separate method and some common variables have been extracted out of the cases &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    response = user_id = nil&lt;br /&gt;
    action = params[:action]&lt;br /&gt;
    if %w(edit delete update view).include?(action)&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      user_id = response.map.reviewer.user_id if (response.map.reviewer)&lt;br /&gt;
    end&lt;br /&gt;
    case action&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      return edit_allowed?(response.map, user_id)&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def edit_allowed?(map, user_id)&lt;br /&gt;
    assignment = map.reviewer.assignment&lt;br /&gt;
    # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
    if map.is_a? ReviewResponseMap&lt;br /&gt;
      reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
      return current_user_id?(user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
    else&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This has extracted an independent functionality and enhanced readability apart from sticking to guidelines of short methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) Replaced find_by_map_id with find_by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by_map_id(params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Moving on with latest Rails conventions, function is being modified as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by(map_id: params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will avoid any unexpected behaviour when further upgrading the Rails framework.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) Replacing sorting technique&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort {|a, b| a.seq &amp;lt;=&amp;gt; b.seq }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
has been replaced with &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort_by(&amp;amp;:seq)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the way to sort based on an object attribute.&lt;br /&gt;
&lt;br /&gt;
4) Refactoring pending_surveys method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
    course_participants = CourseParticipant.where(user_id: session[:user].id)&lt;br /&gt;
    assignment_participants = AssignmentParticipant.where(user_id: session[:user].id)&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    if course_participants&lt;br /&gt;
      course_participants.each do |cp|&lt;br /&gt;
        survey_deployments = CourseSurveyDeployment.where(parent_id: cp.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; cp.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; cp.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the assignment survey deployments for this user&lt;br /&gt;
    if assignment_participants&lt;br /&gt;
      assignment_participants.each do |ap|&lt;br /&gt;
        survey_deployments = AssignmentSurveyDeployment.where(parent_id: ap.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; ap.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; ap.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method involved a lot of code which violates guideline of short methods. Moreover, the lines of code can be reduced along with readability enhanced as similar functionalities in multiple lines can be combined.&lt;br /&gt;
&lt;br /&gt;
53 lines of code have been reduced to 32.&lt;br /&gt;
The refactored method is given below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    [CourseParticipant, AssignmentParticipant].each do |item|&lt;br /&gt;
      # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
      participant_type = item.where(user_id: session[:user].id)&lt;br /&gt;
      next unless participant_type&lt;br /&gt;
      participant_type.each do |p|&lt;br /&gt;
        survey_deployment_type = participant_type == CourseParticipant ? AssignmentSurveyDeployment : CourseSurveyDeployment&lt;br /&gt;
        survey_deployments = survey_deployment_type.where(parent_id: p.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
              [&lt;br /&gt;
                'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
                'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
                'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
                'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
                'parent_id' =&amp;gt; p.parent_id,&lt;br /&gt;
                'participant_id' =&amp;gt; p.id,&lt;br /&gt;
                'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
              ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Test Plan and Execution'''===&lt;br /&gt;
23 integration tests were written to ensure the functionality works and also to make sure the refactoring does not break any of the existing functionality.&lt;br /&gt;
These tests check for the basic functionality of the controller response_controller.rb and whether these function calls redirect to the right place with the correct status code. Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
context 'when params action is view' do&lt;br /&gt;
      context 'when response_map is a ReviewResponseMap and current user is the instructor of current assignment' do&lt;br /&gt;
        it 'allows certain action' do&lt;br /&gt;
          params = {action: &amp;quot;view&amp;quot;, id: review_response.id}&lt;br /&gt;
          controller.params = params&lt;br /&gt;
          expect(controller.action_allowed?).to eq(true)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above given test checks that if the instructor wants to view a review, he is given permission to go ahead and view it. This is checked by setting params = {action: &amp;quot;view&amp;quot;, id: review_response.id}.&lt;br /&gt;
To do so, we set these params and expect the function action_allowed to return true.&lt;br /&gt;
The current user is set in the before(:each) part of the test file because that is used commonly by a number of test cases.&lt;br /&gt;
&lt;br /&gt;
===What all was tested===&lt;br /&gt;
We tested the existing functionality of the controller like view, edit, action_allowed, redirection, etc since we are changing only the implementation of the code. And a general rule of writing test cases is that changing the implementation should not have any effect on tests. Test case scenarios were provided by our mentor and our job was to write the integration tests for those scenarios. the test cases were pretty exhaustive. We didn't have to write extra cases for the parts we refactored as the refactored portions are an extraction of an existing function in smaller ones, in which case the existing function is already calling the new written code. So in a way writing the test cases for existing parts automatically tests the new refactored code.&lt;br /&gt;
&lt;br /&gt;
===Stubs and mocks===&lt;br /&gt;
We wrote a number of stubs for the test cases. We separated out stubs into two parts, the ones that are being commonly used by multiple tests and wrote them in the before(:each) part. The other uncommon ones are written in the respective test cases to avoid extra computation. Stubs allowed us to replicate the behaviour of the database without actually calling, thus avoiding cost of accessing the database and avoiding the pitfall of having a test case fail because of an incorrect database layer logic rather than the controller logic.&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017&amp;diff=109175</id>
		<title>CSC/ECE 517 Fall 2017</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017&amp;diff=109175"/>
		<updated>2017-10-24T21:37:17Z</updated>

		<summary type="html">&lt;p&gt;Panand4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Writing Assignment 2'''&lt;br /&gt;
*[[CSC/ECE 517 Fall 2017/E1779. Fix teammate advertisements and requests to join a team]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2017/E1773 Investigate and Fix Expertiza Production Version Runtime Exceptions.rb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2017/E1774 Metareview fixes and improvements.rb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2017/E1788 OSS project Maroon Heatmap fixes]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2017/E1781 Topic Management]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb]]&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109173</id>
		<title>CSC/ECE 517 Fall 2017/E1745 Refactor response controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109173"/>
		<updated>2017-10-24T21:32:32Z</updated>

		<summary type="html">&lt;p&gt;Panand4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.&lt;br /&gt;
&lt;br /&gt;
==='''Tasks operated by Team'''===&lt;br /&gt;
The tasks completed by the team consisted of refactoring response_controller.rb which is mainly responsible for handling the response for peer reviews/questionnaires/survey with tasks such as creating, saving, editing, updating, deletion. The functionality is further ensured stable by the team with 23 integration tests being written for the same.&lt;br /&gt;
&lt;br /&gt;
==='''Team Members'''===&lt;br /&gt;
1) Pavneet Singh Anand&lt;br /&gt;
&lt;br /&gt;
2) Dipansha Gupta&lt;br /&gt;
&lt;br /&gt;
3) Kshittiz Kumar&lt;br /&gt;
&lt;br /&gt;
==='''Refactoring Explained'''===&lt;br /&gt;
Without wasting much time, lets jump into the refactoring by describing code which existed previously followed by the changed code.&lt;br /&gt;
&lt;br /&gt;
1) action_allowed? method&lt;br /&gt;
&lt;br /&gt;
Previous Code :-&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      map = response.map&lt;br /&gt;
      assignment = response.map.reviewer.assignment&lt;br /&gt;
      # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
      if map.is_a? ReviewResponseMap&lt;br /&gt;
        reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
      else&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view case is extracted into a separate method and some common variables have been extracted out of the cases &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    response = user_id = nil&lt;br /&gt;
    action = params[:action]&lt;br /&gt;
    if %w(edit delete update view).include?(action)&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      user_id = response.map.reviewer.user_id if (response.map.reviewer)&lt;br /&gt;
    end&lt;br /&gt;
    case action&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      return edit_allowed?(response.map, user_id)&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def edit_allowed?(map, user_id)&lt;br /&gt;
    assignment = map.reviewer.assignment&lt;br /&gt;
    # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
    if map.is_a? ReviewResponseMap&lt;br /&gt;
      reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
      return current_user_id?(user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
    else&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This has extracted an independent functionality and enhanced readability apart from sticking to guidelines of short methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) Replaced find_by_map_id with find_by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by_map_id(params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Moving on with latest Rails conventions, function is being modified as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by(map_id: params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will avoid any unexpected behaviour when further upgrading the Rails framework.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) Replacing sorting technique&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort {|a, b| a.seq &amp;lt;=&amp;gt; b.seq }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
has been replaced with &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort_by(&amp;amp;:seq)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the way to sort based on an object attribute.&lt;br /&gt;
&lt;br /&gt;
4) Refactoring pending_surveys method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
    course_participants = CourseParticipant.where(user_id: session[:user].id)&lt;br /&gt;
    assignment_participants = AssignmentParticipant.where(user_id: session[:user].id)&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    if course_participants&lt;br /&gt;
      course_participants.each do |cp|&lt;br /&gt;
        survey_deployments = CourseSurveyDeployment.where(parent_id: cp.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; cp.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; cp.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the assignment survey deployments for this user&lt;br /&gt;
    if assignment_participants&lt;br /&gt;
      assignment_participants.each do |ap|&lt;br /&gt;
        survey_deployments = AssignmentSurveyDeployment.where(parent_id: ap.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; ap.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; ap.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method involved a lot of code which violates guideline of short methods. Moreover, the lines of code can be reduced along with readability enhanced as similar functionalities in multiple lines can be combined.&lt;br /&gt;
&lt;br /&gt;
53 lines of code have been reduced to 32.&lt;br /&gt;
The refactored method is given below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    [CourseParticipant, AssignmentParticipant].each do |item|&lt;br /&gt;
      # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
      participant_type = item.where(user_id: session[:user].id)&lt;br /&gt;
      next unless participant_type&lt;br /&gt;
      participant_type.each do |p|&lt;br /&gt;
        survey_deployment_type = participant_type == CourseParticipant ? AssignmentSurveyDeployment : CourseSurveyDeployment&lt;br /&gt;
        survey_deployments = survey_deployment_type.where(parent_id: p.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
              [&lt;br /&gt;
                'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
                'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
                'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
                'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
                'parent_id' =&amp;gt; p.parent_id,&lt;br /&gt;
                'participant_id' =&amp;gt; p.id,&lt;br /&gt;
                'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
              ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Integration Tests''===&lt;br /&gt;
23 integration tests were written to ensure the functionality works and also to make sure the refactoring does not break any of the existing functionality.&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109172</id>
		<title>CSC/ECE 517 Fall 2017/E1745 Refactor response controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109172"/>
		<updated>2017-10-24T21:30:34Z</updated>

		<summary type="html">&lt;p&gt;Panand4: /* Team Members */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.&lt;br /&gt;
&lt;br /&gt;
==='''Tasks operated by Team'''===&lt;br /&gt;
The tasks completed by the team consisted of refactoring response_controller.rb which is mainly responsible for handling the response for peer reviews/questionnaires/survey with tasks such as creating, saving, editing, updating, deletion. The functionality is further ensured stable by the team with 23 integration tests being written for the same.&lt;br /&gt;
&lt;br /&gt;
==='''Team Members'''===&lt;br /&gt;
1) Pavneet Singh Anand&lt;br /&gt;
&lt;br /&gt;
2) Dipansha Gupta&lt;br /&gt;
&lt;br /&gt;
3) Kshittiz Kumar&lt;br /&gt;
&lt;br /&gt;
==='''Refactoring Explained'''===&lt;br /&gt;
Without wasting much time, lets jump into the refactoring by describing code which existed previously followed by the changed code.&lt;br /&gt;
&lt;br /&gt;
1) action_allowed? method&lt;br /&gt;
&lt;br /&gt;
Previous Code :-&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      map = response.map&lt;br /&gt;
      assignment = response.map.reviewer.assignment&lt;br /&gt;
      # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
      if map.is_a? ReviewResponseMap&lt;br /&gt;
        reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
      else&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view case is extracted into a separate method and some common variables have been extracted out of the cases &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    response = user_id = nil&lt;br /&gt;
    action = params[:action]&lt;br /&gt;
    if %w(edit delete update view).include?(action)&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      user_id = response.map.reviewer.user_id if (response.map.reviewer)&lt;br /&gt;
    end&lt;br /&gt;
    case action&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      return edit_allowed?(response.map, user_id)&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def edit_allowed?(map, user_id)&lt;br /&gt;
    assignment = map.reviewer.assignment&lt;br /&gt;
    # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
    if map.is_a? ReviewResponseMap&lt;br /&gt;
      reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
      return current_user_id?(user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
    else&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This has extracted an independent functionality and enhanced readability apart from sticking to guidelines of short methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) Replaced find_by_map_id with find_by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by_map_id(params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Moving on with latest Rails conventions, function is being modified as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by(map_id: params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will avoid any unexpected behaviour when further upgrading the Rails framework.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) Replacing sorting technique&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort {|a, b| a.seq &amp;lt;=&amp;gt; b.seq }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
has been replaced with &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort_by(&amp;amp;:seq)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the way to sort based on an object attribute.&lt;br /&gt;
&lt;br /&gt;
4) Refactoring pending_surveys method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
    course_participants = CourseParticipant.where(user_id: session[:user].id)&lt;br /&gt;
    assignment_participants = AssignmentParticipant.where(user_id: session[:user].id)&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    if course_participants&lt;br /&gt;
      course_participants.each do |cp|&lt;br /&gt;
        survey_deployments = CourseSurveyDeployment.where(parent_id: cp.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; cp.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; cp.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the assignment survey deployments for this user&lt;br /&gt;
    if assignment_participants&lt;br /&gt;
      assignment_participants.each do |ap|&lt;br /&gt;
        survey_deployments = AssignmentSurveyDeployment.where(parent_id: ap.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; ap.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; ap.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method involved a lot of code which violates guideline of short methods. Moreover, the lines of code can be reduced along with readability enhanced as similar functionalities in multiple lines can be combined.&lt;br /&gt;
&lt;br /&gt;
53 lines of code have been reduced to 32.&lt;br /&gt;
The refactored method is given below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    [CourseParticipant, AssignmentParticipant].each do |item|&lt;br /&gt;
      # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
      participant_type = item.where(user_id: session[:user].id)&lt;br /&gt;
      next unless participant_type&lt;br /&gt;
      participant_type.each do |p|&lt;br /&gt;
        survey_deployment_type = participant_type == CourseParticipant ? AssignmentSurveyDeployment : CourseSurveyDeployment&lt;br /&gt;
        survey_deployments = survey_deployment_type.where(parent_id: p.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
              [&lt;br /&gt;
                'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
                'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
                'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
                'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
                'parent_id' =&amp;gt; p.parent_id,&lt;br /&gt;
                'participant_id' =&amp;gt; p.id,&lt;br /&gt;
                'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
              ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109169</id>
		<title>CSC/ECE 517 Fall 2017/E1745 Refactor response controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109169"/>
		<updated>2017-10-24T21:29:41Z</updated>

		<summary type="html">&lt;p&gt;Panand4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.&lt;br /&gt;
&lt;br /&gt;
==='''Tasks operated by Team'''===&lt;br /&gt;
The tasks completed by the team consisted of refactoring response_controller.rb which is mainly responsible for handling the response for peer reviews/questionnaires/survey with tasks such as creating, saving, editing, updating, deletion. The functionality is further ensured stable by the team with 23 integration tests being written for the same.&lt;br /&gt;
&lt;br /&gt;
==='''Team Members'''===&lt;br /&gt;
1) Pavneet Singh Anand&lt;br /&gt;
2) Dipansha Gupta&lt;br /&gt;
3) Kshittiz Kumar&lt;br /&gt;
&lt;br /&gt;
==='''Refactoring Explained'''===&lt;br /&gt;
Without wasting much time, lets jump into the refactoring by describing code which existed previously followed by the changed code.&lt;br /&gt;
&lt;br /&gt;
1) action_allowed? method&lt;br /&gt;
&lt;br /&gt;
Previous Code :-&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      map = response.map&lt;br /&gt;
      assignment = response.map.reviewer.assignment&lt;br /&gt;
      # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
      if map.is_a? ReviewResponseMap&lt;br /&gt;
        reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
      else&lt;br /&gt;
        return current_user_id?(response.map.reviewer.user_id)&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view case is extracted into a separate method and some common variables have been extracted out of the cases &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    response = user_id = nil&lt;br /&gt;
    action = params[:action]&lt;br /&gt;
    if %w(edit delete update view).include?(action)&lt;br /&gt;
      response = Response.find(params[:id])&lt;br /&gt;
      user_id = response.map.reviewer.user_id if (response.map.reviewer)&lt;br /&gt;
    end&lt;br /&gt;
    case action&lt;br /&gt;
    when 'edit' # If response has been submitted, no further editing allowed&lt;br /&gt;
      return false if response.is_submitted&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
      # Deny access to anyone except reviewer &amp;amp; author's team&lt;br /&gt;
    when 'delete', 'update'&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    when 'view'&lt;br /&gt;
      return edit_allowed?(response.map, user_id)&lt;br /&gt;
    else&lt;br /&gt;
      current_user&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def edit_allowed?(map, user_id)&lt;br /&gt;
    assignment = map.reviewer.assignment&lt;br /&gt;
    # if it is a review response map, all the members of reviewee team should be able to view the reponse (can be done from heat map)&lt;br /&gt;
    if map.is_a? ReviewResponseMap&lt;br /&gt;
      reviewee_team = AssignmentTeam.find(map.reviewee_id)&lt;br /&gt;
      return current_user_id?(user_id) || reviewee_team.has_user(current_user) || current_user.role.name == 'Administrator' || (current_user.role.name == 'Instructor' and assignment.instructor_id == current_user.id) || (current_user.role.name == 'Teaching Assistant' and TaMapping.exists?(ta_id: current_user.id, course_id: assignment.course.id))&lt;br /&gt;
    else&lt;br /&gt;
      return current_user_id?(user_id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This has extracted an independent functionality and enhanced readability apart from sticking to guidelines of short methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) Replaced find_by_map_id with find_by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by_map_id(params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Moving on with latest Rails conventions, function is being modified as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@map = Response.find_by(map_id: params[:id])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will avoid any unexpected behaviour when further upgrading the Rails framework.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) Replacing sorting technique&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort {|a, b| a.seq &amp;lt;=&amp;gt; b.seq }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
has been replaced with &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
questions.sort_by(&amp;amp;:seq)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the way to sort based on an object attribute.&lt;br /&gt;
&lt;br /&gt;
4) Refactoring pending_surveys method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
    course_participants = CourseParticipant.where(user_id: session[:user].id)&lt;br /&gt;
    assignment_participants = AssignmentParticipant.where(user_id: session[:user].id)&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    if course_participants&lt;br /&gt;
      course_participants.each do |cp|&lt;br /&gt;
        survey_deployments = CourseSurveyDeployment.where(parent_id: cp.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; cp.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; cp.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the assignment survey deployments for this user&lt;br /&gt;
    if assignment_participants&lt;br /&gt;
      assignment_participants.each do |ap|&lt;br /&gt;
        survey_deployments = AssignmentSurveyDeployment.where(parent_id: ap.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
          [&lt;br /&gt;
            'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
            'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
            'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
            'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
            'parent_id' =&amp;gt; ap.parent_id,&lt;br /&gt;
            'participant_id' =&amp;gt; ap.id,&lt;br /&gt;
            'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
          ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method involved a lot of code which violates guideline of short methods. Moreover, the lines of code can be reduced along with readability enhanced as similar functionalities in multiple lines can be combined.&lt;br /&gt;
&lt;br /&gt;
53 lines of code have been reduced to 32.&lt;br /&gt;
The refactored method is given below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def pending_surveys&lt;br /&gt;
    unless session[:user] # Check for a valid user&lt;br /&gt;
      redirect_to '/'&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Get all the course survey deployments for this user&lt;br /&gt;
    @surveys = []&lt;br /&gt;
    [CourseParticipant, AssignmentParticipant].each do |item|&lt;br /&gt;
      # Get all the participant(course or assignment) entries for this user&lt;br /&gt;
      participant_type = item.where(user_id: session[:user].id)&lt;br /&gt;
      next unless participant_type&lt;br /&gt;
      participant_type.each do |p|&lt;br /&gt;
        survey_deployment_type = participant_type == CourseParticipant ? AssignmentSurveyDeployment : CourseSurveyDeployment&lt;br /&gt;
        survey_deployments = survey_deployment_type.where(parent_id: p.parent_id)&lt;br /&gt;
        next unless survey_deployments&lt;br /&gt;
        survey_deployments.each do |survey_deployment|&lt;br /&gt;
          next unless survey_deployment &amp;amp;&amp;amp; Time.now &amp;gt; survey_deployment.start_date &amp;amp;&amp;amp; Time.now &amp;lt; survey_deployment.end_date&lt;br /&gt;
          @surveys &amp;lt;&amp;lt;&lt;br /&gt;
              [&lt;br /&gt;
                'survey' =&amp;gt; Questionnaire.find(survey_deployment.questionnaire_id),&lt;br /&gt;
                'survey_deployment_id' =&amp;gt; survey_deployment.id,&lt;br /&gt;
                'start_date' =&amp;gt; survey_deployment.start_date,&lt;br /&gt;
                'end_date' =&amp;gt; survey_deployment.end_date,&lt;br /&gt;
                'parent_id' =&amp;gt; p.parent_id,&lt;br /&gt;
                'participant_id' =&amp;gt; p.id,&lt;br /&gt;
                'global_survey_id' =&amp;gt; survey_deployment.global_survey_id&lt;br /&gt;
              ]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109167</id>
		<title>CSC/ECE 517 Fall 2017/E1745 Refactor response controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1745_Refactor_response_controller.rb&amp;diff=109167"/>
		<updated>2017-10-24T20:51:11Z</updated>

		<summary type="html">&lt;p&gt;Panand4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Expertiza&lt;br /&gt;
&lt;br /&gt;
Expertiza is an open source webapp built on Ruby on Rails stack. It provides a platform to students with various features like peer-reviewing projects, submitting work, form teams, viewing grades etc. The project is being built and maintained by students and faculty at NCSU.&lt;/div&gt;</summary>
		<author><name>Panand4</name></author>
	</entry>
</feed>