<?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=Qwang32</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=Qwang32"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Qwang32"/>
	<updated>2026-06-14T23:07:54Z</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/E1793._Help_students_find_teams_to_join&amp;diff=114148</id>
		<title>CSC/ECE 517 Fall 2017/E1793. Help students find teams to join</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1793._Help_students_find_teams_to_join&amp;diff=114148"/>
		<updated>2017-12-02T02:27:15Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: /* Test Plan */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
For team-based assignments, it always takes time to find suitable team members. We already have bidding, which could help you to join in a team with other team members hold similar bidding preferences. However, you may not be satisfied with automated team formation and want to switch to another team. In this project, we will build a new feature to help students find teams to join.&lt;br /&gt;
&lt;br /&gt;
Currently, there are 2 ways to find other students to join your team:&lt;br /&gt;
&lt;br /&gt;
1.If your team is not full, you could invite people by inputting his/her UnityID. It will send an invitation to certain user. If s/he accept your invitation,s/he will leave original team and join your team.&lt;br /&gt;
2.You could create an advisement by clicking “Your team” link and then clicking “Create” link under “Advertisement for teammates” section. Then your advertisement will appear the last column of the signup sheet page with a horn icon. In this way, all classmates could see your advisement. Someone could send a request to join your team. If you accept their request, s/he will leave original team and join in your team. &lt;br /&gt;
&lt;br /&gt;
It would be better for students who do not have team yet or whose team is not full yet to be able to see a list of students who don’t already have teams. So too for instructors.&lt;br /&gt;
&lt;br /&gt;
== Task Description ==&lt;br /&gt;
&lt;br /&gt;
Fix the second way to find other students to join your team.&lt;br /&gt;
&lt;br /&gt;
Currently, after you create an advertisement, the horn icon does not appear in the the last column of the signup sheet.&lt;br /&gt;
&lt;br /&gt;
For student end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have a team with invitation links in student_teams#view page&lt;br /&gt;
You could invite students to your team by clicking invitation links. If s/he accept your invitation,s/he will leave original team and join in your team. It will be more straightforward than typing UnityID.&lt;br /&gt;
&lt;br /&gt;
For instructor end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have team in teams#list page&lt;br /&gt;
&lt;br /&gt;
Write feature tests to verify your modifications:&lt;br /&gt;
&lt;br /&gt;
Create team_invitation_spec.rb file in spec/features folder&lt;br /&gt;
&lt;br /&gt;
==Modified Files ==&lt;br /&gt;
1)app/views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
2)app/views/join_team_requests/_list_received.erb&lt;br /&gt;
&lt;br /&gt;
3)app/controllers/join_team_requests_controller.rb&lt;br /&gt;
&lt;br /&gt;
4)app/models/join_team_request.rb&lt;br /&gt;
&lt;br /&gt;
5)app/models/sign_up_sheet.rb&lt;br /&gt;
&lt;br /&gt;
6)app/views/sign_up_sheet/_table_line.html.erb&lt;br /&gt;
&lt;br /&gt;
7)app/views/sign_up_sheet/_all_actions.html.erb&lt;br /&gt;
&lt;br /&gt;
8)app/views/teams/list.html.erb&lt;br /&gt;
&lt;br /&gt;
9)spec/features/team_invitation_spec.rb&lt;br /&gt;
&lt;br /&gt;
=='''Approach taken to resolve the issues'''==&lt;br /&gt;
&lt;br /&gt;
===Issue1===&lt;br /&gt;
When someone creates an ad ,the horny icon does not  appear in the the last column of the signup sheet.the code that show horny icon is below:&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;% if SignUpSheet.has_teammate_ads?(topic.id) %&amp;gt;&lt;br /&gt;
 &amp;lt;%= link_to image_tag('ad.png', :border =&amp;gt; 0, :title =&amp;gt; 'Ad', :align =&amp;gt; 'middle', :style =&amp;gt; 'width: 24px; height:24px'), :controller=&amp;gt;'sign_up_sheet', &lt;br /&gt;
 :action=&amp;gt; 'show_team',assignment_id=&amp;gt;@assignment.id, :id=&amp;gt;topic.id%&amp;gt;&lt;br /&gt;
 &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After debugging ,I found even if you create an ad before ,the if block of code will not be executed. Therefore something must be wrong with has_teammate_ads? method.Then I rewrite that method.Signupteam has an attribute:advertise_for_partner ,it will be set true after team members create an ad for their team.i  return this attribute to the has_teammate_ads? method and the horny icon appears.This method code is below:&lt;br /&gt;
   def self.has_teammate_ads?(topic_id)&lt;br /&gt;
     @ads_exsit=false&lt;br /&gt;
     @result=SignedUpTeam.where(&amp;quot;topic_id = ?&amp;quot;, topic_id.to_s)&lt;br /&gt;
     @result.each do |result|&lt;br /&gt;
      team=result.team&lt;br /&gt;
      @ads_exsit=team.advertise_for_partner&lt;br /&gt;
      end&lt;br /&gt;
      @ads_exsit&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
===Issue2===&lt;br /&gt;
I found currently when you sent join team request in the ad,the team received that request would see invite and decline button on their student teams#view page.if they are willing to add the student who send request as member ,they have to send invitation and wait for reply. This is not reasonable and convenient. what should happen is that when the student send join team request,the team members could see accept and decline button ,After they click accept button ,the student would leave his/her original team and join the new team. &lt;br /&gt;
What I have done is that I  add accept method in join team request controller.The method is below:&lt;br /&gt;
 &lt;br /&gt;
   def accept&lt;br /&gt;
      unless JoinTeamRequest.accept_invite(params[:team_id], @inviter_userid,  @invited_userid, @assignment_id)&lt;br /&gt;
      flash[:error] = 'The system failed to add you to the team that invited you.'&lt;br /&gt;
     end&lt;br /&gt;
     redirect_to view_student_teams_path student_id: params[:student_id]&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here @invited_userid is acquired from the student who sent join team request. inviter_userid is from the team members who received the request.  team_id is invited student's team id.&lt;br /&gt;
&lt;br /&gt;
===Issue3===&lt;br /&gt;
&lt;br /&gt;
We build a new feature to help students find teams to join. Students who do not have teams or whose team is not full are able to see a list of students who do not haven a team.The code for this issue is showed below:&lt;br /&gt;
  &amp;lt;% if @student.user_id!=nil %&amp;gt;&lt;br /&gt;
  &amp;lt;%if @student.assignment.max_team_size &amp;gt; 1 %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @student.team==nil|| !@student.team.full? %&amp;gt;&lt;br /&gt;
  Students without teams&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;%if @student.team==nil&lt;br /&gt;
             @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
           else&lt;br /&gt;
           @teamid=@student.team.id&lt;br /&gt;
         end%&amp;gt;&lt;br /&gt;
          &amp;lt;td&amp;gt;&amp;lt;%=  form_tag :controller =&amp;gt; 'invitations', :action =&amp;gt; 'create' do %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'team_id', @teamid %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'student_id', @student.id %&amp;gt;&lt;br /&gt;
             &amp;lt;%= hidden_field_tag 'session[:dummy][:assignment_id]', @student.parent_id %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'user[name]',  participant.user.name%&amp;gt;&lt;br /&gt;
              &amp;lt;%=  submit_tag &amp;quot;invite&amp;quot;%&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
               &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
           &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;/table&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, if @student.assignment.max_team_size &amp;gt; 1 we then execute the below code ,this is because our new feature is only available when assignment team size greater than 1.we use  if @student.team==nil|| !@student.team.full?  to allow students who have no team or whose team is not full to see this list.Next we found all participants ,we use if statement to sample those who has no team , their role are students and they won't see themselves if they do not have teams.We then put these sampled participants on the list .these achieved by the code below&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
We also need to add invitation links next to students without teams,before adding links , we need to know whether  students who want to invite the students on the list have team or not .if they do not have their own teams. we will automatically create a team for them ,this mechanism is called team lazy initialization.the code for this part is below&lt;br /&gt;
       &amp;lt;%if @student.team==nil&lt;br /&gt;
       @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
       else&lt;br /&gt;
       @teamid=@student.team.id&lt;br /&gt;
       end%&amp;gt;&lt;br /&gt;
       def check_inviterteam_before_invitation&lt;br /&gt;
      @student = AssignmentParticipant.find(params[:student_id])&lt;br /&gt;
      @team_id=params[:team_id]&lt;br /&gt;
      if @team_id==&amp;quot;0&amp;quot;&lt;br /&gt;
      team = AssignmentTeam.create_team_and_node(@student.parent_id)&lt;br /&gt;
      user = User.find(@student.user_id)&lt;br /&gt;
      # create TeamsUser and TeamUserNode&lt;br /&gt;
      teamuser = ApplicationController.helpers.create_team_users(user, team.id)&lt;br /&gt;
      @team_id=team.id&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
Finally we just use form_tag to create invite button which is related to invitation controller and create method in it .&lt;br /&gt;
&lt;br /&gt;
===Issue4===&lt;br /&gt;
we also display a list of students who do not have team in teams#list page for instructor.This part of code is showed below.&lt;br /&gt;
      &amp;lt;% @assignment.participants.each do |participant| %&amp;gt; &amp;lt;!--start team member content--&amp;gt;&lt;br /&gt;
      &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
       &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
       &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
     &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Screenshots of new feature==&lt;br /&gt;
1)The horny icons appear in the the last column of the signup sheet.&lt;br /&gt;
[[File:30.png]]&lt;br /&gt;
[[File:31.png]]&lt;br /&gt;
&lt;br /&gt;
2)Received request  from advertisement  and the changed team members after clicking accept button.&lt;br /&gt;
[[File:32.png]]&lt;br /&gt;
[[File:33.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3)For student end ,the list of students who do not have a team with invitation links&lt;br /&gt;
[[File:35.png]]&lt;br /&gt;
&lt;br /&gt;
4)For instructor end ,the list of students who do not have a team.&lt;br /&gt;
[[File:36.png]]&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
The last part of this project is to test all the modifications. And in order to test the new features we created a new Rspec file, which is spec/features/team_invitation_spec.rb. The codes below are all tests we created.&lt;br /&gt;
&lt;br /&gt;
Some edge cases: &lt;br /&gt;
&lt;br /&gt;
1) The owner of a team can accept or decline the request sent by other students, if the team is already full, the team will remain the same even the request is accepted.&lt;br /&gt;
 context 'when team owner declining the invitation' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Decline']&amp;quot;).click&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  page.should have_no_content('student2064')&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when team owner accepting the invitation' do&lt;br /&gt;
  context 'when the team is not full' do&lt;br /&gt;
   it 'makes requester joins the team' do&lt;br /&gt;
   allow_any_instance_of(TeamController).to receive(full?).and_return(false)&lt;br /&gt;
   find(&amp;quot;input[type=submit][value='Accept']&amp;quot;).click&lt;br /&gt;
   stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
   visit '/student_task/list'&lt;br /&gt;
   click_link 'TestAssignment'&lt;br /&gt;
   visit '/student_task/list'&lt;br /&gt;
   click_link 'TestAssignment'&lt;br /&gt;
   click_link 'Your team'&lt;br /&gt;
   page.should have_content('student2064')&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when the team is already full' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(true)&lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Accept']&amp;quot;).click&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  page.should have_no_content('student2064')&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
2) In the student_team#view page, students should see a list of students who don't have teams and can be invited at present, if the invitation is declined, the team remains the same. And the invitee can only join the team when it is not full.&lt;br /&gt;
&lt;br /&gt;
 context 'on student_teams#view page (student end)' do&lt;br /&gt;
  before(:each) do&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
 end&lt;br /&gt;
 it 'shows a list of students who do not have a team' do&lt;br /&gt;
  expect(page).to have_content('student2064')&lt;br /&gt;
  expect(page).to have_selector(&amp;quot;input[typ1e=submit][value='invite']&amp;quot;)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when invitee declining the invitation' do&lt;br /&gt;
  it 'makes team members remain the same as before' do &lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Decline']&amp;quot;).click&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when invitee accepting the invitation' do&lt;br /&gt;
  context 'when the team is not full' do&lt;br /&gt;
  it 'makes invitee joins the team' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(false)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when the team is already full' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(true)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
3) Testing advertisement features.&lt;br /&gt;
 context 'advertisement feature' do&lt;br /&gt;
  before(:each) do&lt;br /&gt;
  # team owner creates an advertisement from student_teams#view page&lt;br /&gt;
  @owner = User.find_by(name: &amp;quot;student2064&amp;quot;)&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  click_link 'Create'&lt;br /&gt;
  fill_in 'comments_for_advertisement', with: 'advertisement1'&lt;br /&gt;
  click_button 'Create'&lt;br /&gt;
  expect(page).to have_content 'advertisement1'&lt;br /&gt;
  expect(page).to have_content 'Delete'&lt;br /&gt;
  @user = User.find_by(name: &amp;quot;student2065&amp;quot;)&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Signup sheet'&lt;br /&gt;
  horn = find(:xpath, &amp;quot;//a[contains(@href,'show_team/2?')]&amp;quot;)&lt;br /&gt;
  horn.click&lt;br /&gt;
  expect(page).to have_content 'Request invitation'&lt;br /&gt;
  click_link 'Request invitation'&lt;br /&gt;
  fill_in 'comments_', with: 'request_demo'&lt;br /&gt;
  expect(page).to have_selector(&amp;quot;input[type=submit][value='Create']&amp;quot;)&lt;br /&gt;
  create(:join_team_request)&lt;br /&gt;
  # team owner is able to accept or decline the invitation&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  # expect(page).to have_selector(&amp;quot;input[typ1e=submit][value='Accept']&amp;quot;)&lt;br /&gt;
  # expect(page).to have_selector(&amp;quot;input[type=submit][value='Decline']&amp;quot;)&lt;br /&gt;
 end&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1793._Help_students_find_teams_to_join&amp;diff=114146</id>
		<title>CSC/ECE 517 Fall 2017/E1793. Help students find teams to join</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1793._Help_students_find_teams_to_join&amp;diff=114146"/>
		<updated>2017-12-02T02:24:49Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
For team-based assignments, it always takes time to find suitable team members. We already have bidding, which could help you to join in a team with other team members hold similar bidding preferences. However, you may not be satisfied with automated team formation and want to switch to another team. In this project, we will build a new feature to help students find teams to join.&lt;br /&gt;
&lt;br /&gt;
Currently, there are 2 ways to find other students to join your team:&lt;br /&gt;
&lt;br /&gt;
1.If your team is not full, you could invite people by inputting his/her UnityID. It will send an invitation to certain user. If s/he accept your invitation,s/he will leave original team and join your team.&lt;br /&gt;
2.You could create an advisement by clicking “Your team” link and then clicking “Create” link under “Advertisement for teammates” section. Then your advertisement will appear the last column of the signup sheet page with a horn icon. In this way, all classmates could see your advisement. Someone could send a request to join your team. If you accept their request, s/he will leave original team and join in your team. &lt;br /&gt;
&lt;br /&gt;
It would be better for students who do not have team yet or whose team is not full yet to be able to see a list of students who don’t already have teams. So too for instructors.&lt;br /&gt;
&lt;br /&gt;
== Task Description ==&lt;br /&gt;
&lt;br /&gt;
Fix the second way to find other students to join your team.&lt;br /&gt;
&lt;br /&gt;
Currently, after you create an advertisement, the horn icon does not appear in the the last column of the signup sheet.&lt;br /&gt;
&lt;br /&gt;
For student end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have a team with invitation links in student_teams#view page&lt;br /&gt;
You could invite students to your team by clicking invitation links. If s/he accept your invitation,s/he will leave original team and join in your team. It will be more straightforward than typing UnityID.&lt;br /&gt;
&lt;br /&gt;
For instructor end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have team in teams#list page&lt;br /&gt;
&lt;br /&gt;
Write feature tests to verify your modifications:&lt;br /&gt;
&lt;br /&gt;
Create team_invitation_spec.rb file in spec/features folder&lt;br /&gt;
&lt;br /&gt;
==Modified Files ==&lt;br /&gt;
1)app/views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
2)app/views/join_team_requests/_list_received.erb&lt;br /&gt;
&lt;br /&gt;
3)app/controllers/join_team_requests_controller.rb&lt;br /&gt;
&lt;br /&gt;
4)app/models/join_team_request.rb&lt;br /&gt;
&lt;br /&gt;
5)app/models/sign_up_sheet.rb&lt;br /&gt;
&lt;br /&gt;
6)app/views/sign_up_sheet/_table_line.html.erb&lt;br /&gt;
&lt;br /&gt;
7)app/views/sign_up_sheet/_all_actions.html.erb&lt;br /&gt;
&lt;br /&gt;
8)app/views/teams/list.html.erb&lt;br /&gt;
&lt;br /&gt;
9)spec/features/team_invitation_spec.rb&lt;br /&gt;
&lt;br /&gt;
=='''Approach taken to resolve the issues'''==&lt;br /&gt;
&lt;br /&gt;
===Issue1===&lt;br /&gt;
When someone creates an ad ,the horny icon does not  appear in the the last column of the signup sheet.the code that show horny icon is below:&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;% if SignUpSheet.has_teammate_ads?(topic.id) %&amp;gt;&lt;br /&gt;
 &amp;lt;%= link_to image_tag('ad.png', :border =&amp;gt; 0, :title =&amp;gt; 'Ad', :align =&amp;gt; 'middle', :style =&amp;gt; 'width: 24px; height:24px'), :controller=&amp;gt;'sign_up_sheet', &lt;br /&gt;
 :action=&amp;gt; 'show_team',assignment_id=&amp;gt;@assignment.id, :id=&amp;gt;topic.id%&amp;gt;&lt;br /&gt;
 &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After debugging ,I found even if you create an ad before ,the if block of code will not be executed. Therefore something must be wrong with has_teammate_ads? method.Then I rewrite that method.Signupteam has an attribute:advertise_for_partner ,it will be set true after team members create an ad for their team.i  return this attribute to the has_teammate_ads? method and the horny icon appears.This method code is below:&lt;br /&gt;
   def self.has_teammate_ads?(topic_id)&lt;br /&gt;
     @ads_exsit=false&lt;br /&gt;
     @result=SignedUpTeam.where(&amp;quot;topic_id = ?&amp;quot;, topic_id.to_s)&lt;br /&gt;
     @result.each do |result|&lt;br /&gt;
      team=result.team&lt;br /&gt;
      @ads_exsit=team.advertise_for_partner&lt;br /&gt;
      end&lt;br /&gt;
      @ads_exsit&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
===Issue2===&lt;br /&gt;
I found currently when you sent join team request in the ad,the team received that request would see invite and decline button on their student teams#view page.if they are willing to add the student who send request as member ,they have to send invitation and wait for reply. This is not reasonable and convenient. what should happen is that when the student send join team request,the team members could see accept and decline button ,After they click accept button ,the student would leave his/her original team and join the new team. &lt;br /&gt;
What I have done is that I  add accept method in join team request controller.The method is below:&lt;br /&gt;
 &lt;br /&gt;
   def accept&lt;br /&gt;
      unless JoinTeamRequest.accept_invite(params[:team_id], @inviter_userid,  @invited_userid, @assignment_id)&lt;br /&gt;
      flash[:error] = 'The system failed to add you to the team that invited you.'&lt;br /&gt;
     end&lt;br /&gt;
     redirect_to view_student_teams_path student_id: params[:student_id]&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here @invited_userid is acquired from the student who sent join team request. inviter_userid is from the team members who received the request.  team_id is invited student's team id.&lt;br /&gt;
&lt;br /&gt;
===Issue3===&lt;br /&gt;
&lt;br /&gt;
We build a new feature to help students find teams to join. Students who do not have teams or whose team is not full are able to see a list of students who do not haven a team.The code for this issue is showed below:&lt;br /&gt;
  &amp;lt;% if @student.user_id!=nil %&amp;gt;&lt;br /&gt;
  &amp;lt;%if @student.assignment.max_team_size &amp;gt; 1 %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @student.team==nil|| !@student.team.full? %&amp;gt;&lt;br /&gt;
  Students without teams&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;%if @student.team==nil&lt;br /&gt;
             @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
           else&lt;br /&gt;
           @teamid=@student.team.id&lt;br /&gt;
         end%&amp;gt;&lt;br /&gt;
          &amp;lt;td&amp;gt;&amp;lt;%=  form_tag :controller =&amp;gt; 'invitations', :action =&amp;gt; 'create' do %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'team_id', @teamid %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'student_id', @student.id %&amp;gt;&lt;br /&gt;
             &amp;lt;%= hidden_field_tag 'session[:dummy][:assignment_id]', @student.parent_id %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'user[name]',  participant.user.name%&amp;gt;&lt;br /&gt;
              &amp;lt;%=  submit_tag &amp;quot;invite&amp;quot;%&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
               &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
           &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;/table&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, if @student.assignment.max_team_size &amp;gt; 1 we then execute the below code ,this is because our new feature is only available when assignment team size greater than 1.we use  if @student.team==nil|| !@student.team.full?  to allow students who have no team or whose team is not full to see this list.Next we found all participants ,we use if statement to sample those who has no team , their role are students and they won't see themselves if they do not have teams.We then put these sampled participants on the list .these achieved by the code below&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
We also need to add invitation links next to students without teams,before adding links , we need to know whether  students who want to invite the students on the list have team or not .if they do not have their own teams. we will automatically create a team for them ,this mechanism is called team lazy initialization.the code for this part is below&lt;br /&gt;
       &amp;lt;%if @student.team==nil&lt;br /&gt;
       @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
       else&lt;br /&gt;
       @teamid=@student.team.id&lt;br /&gt;
       end%&amp;gt;&lt;br /&gt;
       def check_inviterteam_before_invitation&lt;br /&gt;
      @student = AssignmentParticipant.find(params[:student_id])&lt;br /&gt;
      @team_id=params[:team_id]&lt;br /&gt;
      if @team_id==&amp;quot;0&amp;quot;&lt;br /&gt;
      team = AssignmentTeam.create_team_and_node(@student.parent_id)&lt;br /&gt;
      user = User.find(@student.user_id)&lt;br /&gt;
      # create TeamsUser and TeamUserNode&lt;br /&gt;
      teamuser = ApplicationController.helpers.create_team_users(user, team.id)&lt;br /&gt;
      @team_id=team.id&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
Finally we just use form_tag to create invite button which is related to invitation controller and create method in it .&lt;br /&gt;
&lt;br /&gt;
===Issue4===&lt;br /&gt;
we also display a list of students who do not have team in teams#list page for instructor.This part of code is showed below.&lt;br /&gt;
      &amp;lt;% @assignment.participants.each do |participant| %&amp;gt; &amp;lt;!--start team member content--&amp;gt;&lt;br /&gt;
      &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
       &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
       &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
     &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Screenshots of new feature==&lt;br /&gt;
1)The horny icons appear in the the last column of the signup sheet.&lt;br /&gt;
[[File:30.png]]&lt;br /&gt;
[[File:31.png]]&lt;br /&gt;
&lt;br /&gt;
2)Received request  from advertisement  and the changed team members after clicking accept button.&lt;br /&gt;
[[File:32.png]]&lt;br /&gt;
[[File:33.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3)For student end ,the list of students who do not have a team with invitation links&lt;br /&gt;
[[File:35.png]]&lt;br /&gt;
&lt;br /&gt;
4)For instructor end ,the list of students who do not have a team.&lt;br /&gt;
[[File:36.png]]&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
The last part of this project is to test all the modifications. And in order to test the new features we created a new Rspec file, which is spec/features/team_invitation_spec.rb. The codes below are all tests we created.&lt;br /&gt;
&lt;br /&gt;
1) The owner of a team can accept or decline the request sent by other students, if the team is already full, the team will remain the same even the request is accepted.&lt;br /&gt;
 context 'when team owner declining the invitation' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Decline']&amp;quot;).click&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  page.should have_no_content('student2064')&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when team owner accepting the invitation' do&lt;br /&gt;
  context 'when the team is not full' do&lt;br /&gt;
   it 'makes requester joins the team' do&lt;br /&gt;
   allow_any_instance_of(TeamController).to receive(full?).and_return(false)&lt;br /&gt;
   find(&amp;quot;input[type=submit][value='Accept']&amp;quot;).click&lt;br /&gt;
   stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
   visit '/student_task/list'&lt;br /&gt;
   click_link 'TestAssignment'&lt;br /&gt;
   visit '/student_task/list'&lt;br /&gt;
   click_link 'TestAssignment'&lt;br /&gt;
   click_link 'Your team'&lt;br /&gt;
   page.should have_content('student2064')&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when the team is already full' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(true)&lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Accept']&amp;quot;).click&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  page.should have_no_content('student2064')&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
2) In the student_team#view page, students should see a list of students who don't have teams and can be invited at present, if the invitation is declined, the team remains the same. And the invitee can only join the team when it is not full.&lt;br /&gt;
&lt;br /&gt;
 context 'on student_teams#view page (student end)' do&lt;br /&gt;
  before(:each) do&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
 end&lt;br /&gt;
 it 'shows a list of students who do not have a team' do&lt;br /&gt;
  expect(page).to have_content('student2064')&lt;br /&gt;
  expect(page).to have_selector(&amp;quot;input[typ1e=submit][value='invite']&amp;quot;)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when invitee declining the invitation' do&lt;br /&gt;
  it 'makes team members remain the same as before' do &lt;br /&gt;
  find(&amp;quot;input[type=submit][value='Decline']&amp;quot;).click&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when invitee accepting the invitation' do&lt;br /&gt;
  context 'when the team is not full' do&lt;br /&gt;
  it 'makes invitee joins the team' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(false)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 context 'when the team is already full' do&lt;br /&gt;
  it 'makes team members remain the same as before' do&lt;br /&gt;
  allow_any_instance_of(TeamController).to receive(full?).and_return(true)&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
3) Testing advertisement features.&lt;br /&gt;
 context 'advertisement feature' do&lt;br /&gt;
  before(:each) do&lt;br /&gt;
  # team owner creates an advertisement from student_teams#view page&lt;br /&gt;
  @owner = User.find_by(name: &amp;quot;student2064&amp;quot;)&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  click_link 'Create'&lt;br /&gt;
  fill_in 'comments_for_advertisement', with: 'advertisement1'&lt;br /&gt;
  click_button 'Create'&lt;br /&gt;
  expect(page).to have_content 'advertisement1'&lt;br /&gt;
  expect(page).to have_content 'Delete'&lt;br /&gt;
  @user = User.find_by(name: &amp;quot;student2065&amp;quot;)&lt;br /&gt;
  stub_current_user(@user, @user.role.name, @user.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Signup sheet'&lt;br /&gt;
  horn = find(:xpath, &amp;quot;//a[contains(@href,'show_team/2?')]&amp;quot;)&lt;br /&gt;
  horn.click&lt;br /&gt;
  expect(page).to have_content 'Request invitation'&lt;br /&gt;
  click_link 'Request invitation'&lt;br /&gt;
  fill_in 'comments_', with: 'request_demo'&lt;br /&gt;
  expect(page).to have_selector(&amp;quot;input[type=submit][value='Create']&amp;quot;)&lt;br /&gt;
  create(:join_team_request)&lt;br /&gt;
  # team owner is able to accept or decline the invitation&lt;br /&gt;
  stub_current_user(@owner, @owner.role.name, @owner.role)&lt;br /&gt;
  visit '/student_task/list'&lt;br /&gt;
  click_link 'TestAssignment'&lt;br /&gt;
  click_link 'Your team'&lt;br /&gt;
  # expect(page).to have_selector(&amp;quot;input[typ1e=submit][value='Accept']&amp;quot;)&lt;br /&gt;
  # expect(page).to have_selector(&amp;quot;input[type=submit][value='Decline']&amp;quot;)&lt;br /&gt;
 end&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1793._Help_students_find_teams_to_join&amp;diff=114143</id>
		<title>CSC/ECE 517 Fall 2017/E1793. Help students find teams to join</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1793._Help_students_find_teams_to_join&amp;diff=114143"/>
		<updated>2017-12-02T01:55:57Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
For team-based assignments, it always takes time to find suitable team members. We already have bidding, which could help you to join in a team with other team members hold similar bidding preferences. However, you may not be satisfied with automated team formation and want to switch to another team. In this project, we will build a new feature to help students find teams to join.&lt;br /&gt;
&lt;br /&gt;
Currently, there are 2 ways to find other students to join your team:&lt;br /&gt;
&lt;br /&gt;
1.If your team is not full, you could invite people by inputting his/her UnityID. It will send an invitation to certain user. If s/he accept your invitation,s/he will leave original team and join your team.&lt;br /&gt;
2.You could create an advisement by clicking “Your team” link and then clicking “Create” link under “Advertisement for teammates” section. Then your advertisement will appear the last column of the signup sheet page with a horn icon. In this way, all classmates could see your advisement. Someone could send a request to join your team. If you accept their request, s/he will leave original team and join in your team. &lt;br /&gt;
&lt;br /&gt;
It would be better for students who do not have team yet or whose team is not full yet to be able to see a list of students who don’t already have teams. So too for instructors.&lt;br /&gt;
&lt;br /&gt;
== Task Description ==&lt;br /&gt;
&lt;br /&gt;
Fix the second way to find other students to join your team.&lt;br /&gt;
&lt;br /&gt;
Currently, after you create an advertisement, the horn icon does not appear in the the last column of the signup sheet.&lt;br /&gt;
&lt;br /&gt;
For student end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have a team with invitation links in student_teams#view page&lt;br /&gt;
You could invite students to your team by clicking invitation links. If s/he accept your invitation,s/he will leave original team and join in your team. It will be more straightforward than typing UnityID.&lt;br /&gt;
&lt;br /&gt;
For instructor end:&lt;br /&gt;
&lt;br /&gt;
Display a list of students who do not have team in teams#list page&lt;br /&gt;
&lt;br /&gt;
Write feature tests to verify your modifications:&lt;br /&gt;
&lt;br /&gt;
Create team_invitation_spec.rb file in spec/features folder&lt;br /&gt;
&lt;br /&gt;
==Modified Files ==&lt;br /&gt;
1)app/views/student_teams/view.html.erb&lt;br /&gt;
&lt;br /&gt;
2)app/views/join_team_requests/_list_received.erb&lt;br /&gt;
&lt;br /&gt;
3)app/controllers/join_team_requests_controller.rb&lt;br /&gt;
&lt;br /&gt;
4)app/models/join_team_request.rb&lt;br /&gt;
&lt;br /&gt;
5)app/models/sign_up_sheet.rb&lt;br /&gt;
&lt;br /&gt;
6)app/views/sign_up_sheet/_table_line.html.erb&lt;br /&gt;
&lt;br /&gt;
7)app/views/sign_up_sheet/_all_actions.html.erb&lt;br /&gt;
&lt;br /&gt;
8)app/views/teams/list.html.erb&lt;br /&gt;
&lt;br /&gt;
9)spec/features/team_invitation_spec.rb&lt;br /&gt;
&lt;br /&gt;
=='''Approach taken to resolve the issues'''==&lt;br /&gt;
&lt;br /&gt;
===Issue1===&lt;br /&gt;
When someone creates an ad ,the horny icon does not  appear in the the last column of the signup sheet.the code that show horny icon is below:&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;% if SignUpSheet.has_teammate_ads?(topic.id) %&amp;gt;&lt;br /&gt;
 &amp;lt;%= link_to image_tag('ad.png', :border =&amp;gt; 0, :title =&amp;gt; 'Ad', :align =&amp;gt; 'middle', :style =&amp;gt; 'width: 24px; height:24px'), :controller=&amp;gt;'sign_up_sheet', &lt;br /&gt;
 :action=&amp;gt; 'show_team',assignment_id=&amp;gt;@assignment.id, :id=&amp;gt;topic.id%&amp;gt;&lt;br /&gt;
 &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After debugging ,I found even if you create an ad before ,the if block of code will not be executed. Therefore something must be wrong with has_teammate_ads? method.Then I rewrite that method.Signupteam has an attribute:advertise_for_partner ,it will be set true after team members create an ad for their team.i  return this attribute to the has_teammate_ads? method and the horny icon appears.This method code is below:&lt;br /&gt;
   def self.has_teammate_ads?(topic_id)&lt;br /&gt;
     @ads_exsit=false&lt;br /&gt;
     @result=SignedUpTeam.where(&amp;quot;topic_id = ?&amp;quot;, topic_id.to_s)&lt;br /&gt;
     @result.each do |result|&lt;br /&gt;
      team=result.team&lt;br /&gt;
      @ads_exsit=team.advertise_for_partner&lt;br /&gt;
      end&lt;br /&gt;
      @ads_exsit&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
===Issue2===&lt;br /&gt;
I found currently when you sent join team request in the ad,the team received that request would see invite and decline button on their student teams#view page.if they are willing to add the student who send request as member ,they have to send invitation and wait for reply. This is not reasonable and convenient. what should happen is that when the student send join team request,the team members could see accept and decline button ,After they click accept button ,the student would leave his/her original team and join the new team. &lt;br /&gt;
What I have done is that I  add accept method in join team request controller.The method is below:&lt;br /&gt;
 &lt;br /&gt;
   def accept&lt;br /&gt;
      unless JoinTeamRequest.accept_invite(params[:team_id], @inviter_userid,  @invited_userid, @assignment_id)&lt;br /&gt;
      flash[:error] = 'The system failed to add you to the team that invited you.'&lt;br /&gt;
     end&lt;br /&gt;
     redirect_to view_student_teams_path student_id: params[:student_id]&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here @invited_userid is acquired from the student who sent join team request. inviter_userid is from the team members who received the request.  team_id is invited student's team id.&lt;br /&gt;
&lt;br /&gt;
===Issue3===&lt;br /&gt;
&lt;br /&gt;
We build a new feature to help students find teams to join. Students who do not have teams or whose team is not full are able to see a list of students who do not haven a team.The code for this issue is showed below:&lt;br /&gt;
  &amp;lt;% if @student.user_id!=nil %&amp;gt;&lt;br /&gt;
  &amp;lt;%if @student.assignment.max_team_size &amp;gt; 1 %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @student.team==nil|| !@student.team.full? %&amp;gt;&lt;br /&gt;
  Students without teams&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;%if @student.team==nil&lt;br /&gt;
             @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
           else&lt;br /&gt;
           @teamid=@student.team.id&lt;br /&gt;
         end%&amp;gt;&lt;br /&gt;
          &amp;lt;td&amp;gt;&amp;lt;%=  form_tag :controller =&amp;gt; 'invitations', :action =&amp;gt; 'create' do %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'team_id', @teamid %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'student_id', @student.id %&amp;gt;&lt;br /&gt;
             &amp;lt;%= hidden_field_tag 'session[:dummy][:assignment_id]', @student.parent_id %&amp;gt;&lt;br /&gt;
              &amp;lt;%= hidden_field_tag 'user[name]',  participant.user.name%&amp;gt;&lt;br /&gt;
              &amp;lt;%=  submit_tag &amp;quot;invite&amp;quot;%&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
               &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
            &amp;lt;% end %&amp;gt;&lt;br /&gt;
           &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
         &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;/table&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, if @student.assignment.max_team_size &amp;gt; 1 we then execute the below code ,this is because our new feature is only available when assignment team size greater than 1.we use  if @student.team==nil|| !@student.team.full?  to allow students who have no team or whose team is not full to see this list.Next we found all participants ,we use if statement to sample those who has no team , their role are students and they won't see themselves if they do not have teams.We then put these sampled participants on the list .these achieved by the code below&lt;br /&gt;
   &amp;lt;%@student.assignment.participants.each do |participant| %&amp;gt; &lt;br /&gt;
   &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
   &amp;lt;% if participant.user.name!=@student.name  %&amp;gt;&lt;br /&gt;
   &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
We also need to add invitation links next to students without teams,before adding links , we need to know whether  students who want to invite the students on the list have team or not .if they do not have their own teams. we will automatically create a team for them ,this mechanism is called team lazy initialization.the code for this part is below&lt;br /&gt;
       &amp;lt;%if @student.team==nil&lt;br /&gt;
       @teamid=&amp;quot;0&amp;quot;&lt;br /&gt;
       else&lt;br /&gt;
       @teamid=@student.team.id&lt;br /&gt;
       end%&amp;gt;&lt;br /&gt;
       def check_inviterteam_before_invitation&lt;br /&gt;
      @student = AssignmentParticipant.find(params[:student_id])&lt;br /&gt;
      @team_id=params[:team_id]&lt;br /&gt;
      if @team_id==&amp;quot;0&amp;quot;&lt;br /&gt;
      team = AssignmentTeam.create_team_and_node(@student.parent_id)&lt;br /&gt;
      user = User.find(@student.user_id)&lt;br /&gt;
      # create TeamsUser and TeamUserNode&lt;br /&gt;
      teamuser = ApplicationController.helpers.create_team_users(user, team.id)&lt;br /&gt;
      @team_id=team.id&lt;br /&gt;
      end&lt;br /&gt;
      end&lt;br /&gt;
Finally we just use form_tag to create invite button which is related to invitation controller and create method in it .&lt;br /&gt;
&lt;br /&gt;
===Issue4===&lt;br /&gt;
we also display a list of students who do not have team in teams#list page for instructor.This part of code is showed below.&lt;br /&gt;
      &amp;lt;% @assignment.participants.each do |participant| %&amp;gt; &amp;lt;!--start team member content--&amp;gt;&lt;br /&gt;
      &amp;lt;% if participant.team==nil  %&amp;gt;&lt;br /&gt;
       &amp;lt;% if User.find(participant.user_id).role.name==&amp;quot;Student&amp;quot;  %&amp;gt;&lt;br /&gt;
       &amp;lt;td&amp;gt;&amp;lt;%= participant.user.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
       &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
     &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Screenshots of new feature==&lt;br /&gt;
1)The horny icons appear in the the last column of the signup sheet.&lt;br /&gt;
[[File:30.png]]&lt;br /&gt;
[[File:31.png]]&lt;br /&gt;
&lt;br /&gt;
2)Received request  from advertisement  and the changed team members after clicking accept button.&lt;br /&gt;
[[File:32.png]]&lt;br /&gt;
[[File:33.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3)For student end ,the list of students who do not have a team with invitation links&lt;br /&gt;
[[File:35.png]]&lt;br /&gt;
&lt;br /&gt;
4)For instructor end ,the list of students who do not have a team.&lt;br /&gt;
[[File:36.png]]&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
The last part of this project is to test all the modifications. And in order to test the new features we created a new Rspec file, which is spec/features/team_invitation_spec.rb.&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111204</id>
		<title>CSC/ECE 517 Fall 2017/E1747 Refactor sign up sheet controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111204"/>
		<updated>2017-11-02T14:40:19Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: /* Rspec test plan */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Introduction of Expertiza''' ==&lt;br /&gt;
Expertiza is a web application where students can form their teams, submit their projects, peer-review other's work, view their grades and so on. Expertiza is open-source, maintained by students and faculty in NC State university.&lt;br /&gt;
&lt;br /&gt;
== '''Why refactoring?''' ==&lt;br /&gt;
Code Refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.&lt;br /&gt;
Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility. Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may also resolve hidden, dormant, or undiscovered bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.&lt;br /&gt;
&lt;br /&gt;
== '''Test Last Development''' ==&lt;br /&gt;
Test last development is a traditional and most common approach of testing where testing is done only after implementation code has been written. But test driven development, test first coding or TDD is a relatively new concept where test cases are written before writing the implementation code.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction of RSpec''' ==&lt;br /&gt;
RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring. It contains its own mocking framework that is fully integrated into the framework based upon JMock. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec gem which consists of 3 other gems namely rspec-core , rspec-expectation and rspec-mock.&lt;br /&gt;
&lt;br /&gt;
== '''Problem Statement''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment. sign_up_sheet_controller.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These method need to be broken down into simpler and more specific methods that are easier to read or understand. Also, the few instances of code duplication that exist should be removed.&lt;br /&gt;
&lt;br /&gt;
=='''Tasks'''==&lt;br /&gt;
&lt;br /&gt;
* Refactor &amp;quot;list&amp;quot;, &amp;quot;save_topic_deadlines&amp;quot; method&lt;br /&gt;
** 1.Split into several simpler methods and assign reasonable names &lt;br /&gt;
** 2.Extract duplicated code into separate methods&lt;br /&gt;
* Use find_by instead of dynamic method&lt;br /&gt;
* Complete the pending tests in sign_up_sheet_controller_spec.rb, and write integration tests for newly-created methods. Refactor corresponding methods, and only then finish pending tests.&lt;br /&gt;
&lt;br /&gt;
== '''Refactor''' ==&lt;br /&gt;
'''Refactor &amp;quot;list&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
The 'list' method is a long method which is fairly complex and hard to read. It is not following &amp;quot;good Ruby and Rails coding practices&amp;quot;.  In &amp;quot;list&amp;quot; method we found that &amp;quot;signed_up_topics&amp;quot; occurred too many times, here we split it into three different methods, named as &amp;quot;find_signed_up_topics&amp;quot; ,&amp;quot;find_selected_topics&amp;quot; and &amp;quot;list&amp;quot;. The changes are showed below.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority) &lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      @bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
      signed_up_topics &amp;amp;= @sign_up_topics&lt;br /&gt;
      @sign_up_topics -= signed_up_topics&lt;br /&gt;
      @bids = signed_up_topics&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
      # sign up again unless the former was a waitlisted topic&lt;br /&gt;
      # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
      users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)&lt;br /&gt;
      @selected_topics = if users_team.empty?&lt;br /&gt;
                           nil&lt;br /&gt;
                         else&lt;br /&gt;
                           # TODO: fix this; cant use 0&lt;br /&gt;
                           SignedUpTeam.find_user_signup_topics(@assignment.id, users_team[0].t_id)&lt;br /&gt;
                         end&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
 #Operating on the student bids, return signed up topics.&lt;br /&gt;
   def find_signed_up_topics student_bids&lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      student_bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
     # signed_up_topics &amp;amp;= sign_up_topics&lt;br /&gt;
      return signed_up_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 #Operating on assignments, return selected topics.&lt;br /&gt;
   def find_selected_topics assignment&lt;br /&gt;
    # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
    # sign up again unless the former was a waitlisted topic&lt;br /&gt;
    # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
    users_team = SignedUpTeam.find_team_users(assignment.id, session[:user].id)&lt;br /&gt;
    selected_topics = if users_team.empty?&lt;br /&gt;
                         nil&lt;br /&gt;
                       else&lt;br /&gt;
                         # TODO: fix this; cant use 0&lt;br /&gt;
                         SignedUpTeam.find_user_signup_topics(assignment.id, users_team.first.t_id)&lt;br /&gt;
                       end&lt;br /&gt;
    return selected_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = find_signed_up_topics @student_bids&lt;br /&gt;
      @sign_up_topics = @bids&lt;br /&gt;
    else&lt;br /&gt;
      @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      @selected_topics = find_selected_topics @assignment&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
'''Refactor &amp;quot;save_topic_deadlines&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
We found the &amp;quot;save_topic_deadlines&amp;quot; method is not hard to read, but it's a little bit long and some of the codes can be reused. Here The &amp;quot;save_topic_deadlines&amp;quot; method is modified and two new methods were created, named as &amp;quot;get_instance_variable_assignment&amp;quot; and &amp;quot;get_instance_variable_topic&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if instance_variable_get('@topic_' + deadline_type + '_due_date') == instance_variable_get('@assignment_' + deadline_type + '_due_date')&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].flag,&lt;br /&gt;
              threshold:                   instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].deadline_name,&lt;br /&gt;
              description_url:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if get_instance_varibale_topic == get_instance_variable_assignment&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        get_instance_variable_assignment[i - 1].flag,&lt;br /&gt;
              threshold:                   get_instance_variable_assignment[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              get_instance_variable_assignment[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               get_instance_variable_assignment[i - 1].deadline_name,&lt;br /&gt;
              description_url:             get_instance_variable_assignment[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   #refactor duplicate code present in both create params&lt;br /&gt;
  def get_instance_variable_assignment&lt;br /&gt;
    instance_variable_get('@assignment_' + deadline_type + '_due_dates')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def get_instance_varibale_topic&lt;br /&gt;
    instance_variable_get('@topic_' + deadline_type + '_due_date')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test plan''' ==&lt;br /&gt;
1.Complete pending test in sign_up_sheet_controller_spec.rb.&lt;br /&gt;
&lt;br /&gt;
2.Write integration test for newly created methods.&lt;br /&gt;
&lt;br /&gt;
3.Write test for refactor methods.&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test cases''' ==&lt;br /&gt;
The most important two tests are about &amp;quot;list&amp;quot; method and &amp;quot;save_topic_deadlines&amp;quot; method. Here we wrote 27 examples of test and all of them are passed.&lt;br /&gt;
 describe SignUpSheetController do&lt;br /&gt;
  let(:assignment) { build(:assignment, id: 1, instructor_id: 6, due_dates: [due_date], microtask: true, staggered_deadline: true) }&lt;br /&gt;
  let(:instructor) { build(:instructor, id: 6) }&lt;br /&gt;
  let(:student) { build(:student, id: 8) }&lt;br /&gt;
  let(:participant) { build(:participant, id: 1, user_id: 6, assignment: assignment) }&lt;br /&gt;
  let(:topic) { build(:topic, id: 1) }&lt;br /&gt;
  let(:signed_up_team) { build(:signed_up_team, team: team, topic: topic) }&lt;br /&gt;
  let(:signed_up_team2) { build(:signed_up_team, team_id: 2, is_waitlisted: true) }&lt;br /&gt;
  let(:team) { build(:assignment_team, id: 1, assignment: assignment) }&lt;br /&gt;
  let(:due_date) { build(:assignment_due_date, deadline_type_id: 1) }&lt;br /&gt;
  let(:due_date2) { build(:assignment_due_date, deadline_type_id: 2) }&lt;br /&gt;
  let(:bid) { Bid.new(topic_id: 1, priority: 1) }&lt;br /&gt;
  let(:team_user) { build(:team_user) }&lt;br /&gt;
   before(:each) do&lt;br /&gt;
    allow(Assignment).to receive(:find).with('1').and_return(assignment)&lt;br /&gt;
    allow(Assignment).to receive(:find).with(1).and_return(assignment)&lt;br /&gt;
    stub_current_user(instructor, instructor.role.name, instructor.role)&lt;br /&gt;
    allow(SignUpTopic).to receive(:find).with('1').and_return(topic)&lt;br /&gt;
    allow(Participant).to receive(:find_by).with(id: '1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with(1).and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: student.id, parent_id: 1).and_return(participant)&lt;br /&gt;
    allow(Team).to receive(:find).with('1').and_return(team)&lt;br /&gt;
    allow(TeamsUser).to receive(:find_by).with(team_id: 1).and_return(team_user)&lt;br /&gt;
    allow(team_user).to receive(:user).and_return(student)&lt;br /&gt;
    allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
    allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
    allow(participant.team).to receive(:hyperlinks).and_return([])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#new' do&lt;br /&gt;
    it 'builds a new sign up topic and renders sign_up_sheet#new page' do&lt;br /&gt;
      params = {id: 1}&lt;br /&gt;
      get :new, params&lt;br /&gt;
      expect(response).to render_template(:new)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#create' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 0, category: 'test', id: 1}} }&lt;br /&gt;
      context 'when new topic can be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and redirects to assignment#edit page' do&lt;br /&gt;
          session[:user] = participant&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(true)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
  context 'when new topic cannot be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and renders sign_up_sheet#new page' do&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(false)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to render_template(:new)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
   context 'when topic can be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 120, category: 'test', id: 1}} }&lt;br /&gt;
      it 'updates the existing topic and redirects to sign_up_sheet#add_signup_topics_staggered page' do&lt;br /&gt;
        new_topic = build(:topic, topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1)&lt;br /&gt;
        allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(new_topic)&lt;br /&gt;
        post :create, params&lt;br /&gt;
        expect(response).to redirect_to('/sign_up_sheet/add_signup_topics_staggered?id=' + params[:id].to_s)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 describe '#destroy' do&lt;br /&gt;
    let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
    context 'when topic can be found' do&lt;br /&gt;
      it 'redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :destroy, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
   context 'when topic cannot be found' do&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :destroy, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be deleted.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#edit' do&lt;br /&gt;
    let(:params) { {id: 1} }&lt;br /&gt;
    it 'renders sign_up_sheet#edit page' do&lt;br /&gt;
      get :edit, params&lt;br /&gt;
      expect(response).to render_template(:edit)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#update' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :update, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be updated.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic can be found' do&lt;br /&gt;
      let(:params) do&lt;br /&gt;
        {&lt;br /&gt;
          id: 1, assignment_id: 1,&lt;br /&gt;
          topic: {topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1, micropayment: 0, description: 'test', link: 'test'}&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
      it 'updates current topic and redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :update, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#list' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when current assignment is intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#intelligent_topic_selection page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_filled).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_waitlisted).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(assignment_id: assignment.id, private_to: nil).and_return([topic])&lt;br /&gt;
        allow(assignment).to receive(:max_team_size).and_return(1)&lt;br /&gt;
        allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(true)&lt;br /&gt;
        allow(Bid).to receive(:where).with(team_id: team.try(:id)).and_return(bids)&lt;br /&gt;
        allow(bids).to receive(:order).with(:priority).and_return(bids)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_by).with(id: bid.topic_id).and_return(topic)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(7).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(6).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(1).and_return(due_date)&lt;br /&gt;
        allow(assignment).to receive(:staggered_deadline).and_return(true)&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(1, student.id).and_return([])&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:intelligent_topic_selection)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when current assignment is not intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(false)&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:list)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#sign_up' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when SignUpSheet.signup_team method return nil' do&lt;br /&gt;
      it 'shows an error flash message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
        get :sign_up, params, session&lt;br /&gt;
        expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You've already signed up for a topic!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#signup_as_instructor_action' do&lt;br /&gt;
    let(:params) { {username: '1'} }&lt;br /&gt;
    context 'when user cannot be found' do&lt;br /&gt;
      it 'shows an flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(User).to receive(:find_by).with(any_args).and_return(nil)&lt;br /&gt;
        get :signup_as_instructor_action, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;That student does not exist!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
     context 'when user can be found' do&lt;br /&gt;
      context 'when an assignment_participant can be found' do&lt;br /&gt;
        context 'when creating team related objects successfully' do&lt;br /&gt;
          it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(signed_up_team)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:success]).to eq(&amp;quot;You have successfully signed up the student for the topic!&amp;quot;)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
         context 'when creating team related objects unsuccessfully' do&lt;br /&gt;
          it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:error]).to eq(&amp;quot;The student has already signed up for a topic!&amp;quot;)&lt;br /&gt;
            expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
       context 'when an assignment_participant cannot be found' do&lt;br /&gt;
        it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
          allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
          allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
          get :signup_as_instructor_action&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student is not registered for the assignment!&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;You have already submitted your work, so you are not allowed to drop your topic.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop your topic after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: student} }&lt;br /&gt;
      it 'shows a flash success message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).and_return(signed_up_team2)&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        delete :delete_signup, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped your topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup_as_instructor' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup_as_instructor, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student has already submitted their work, so you are not allowed to remove them.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup_as_instructor, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop a student after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: instructor} }&lt;br /&gt;
      it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).with(any_args).with(no_args).and_return(signed_up_team2)&lt;br /&gt;
        delete :delete_signup_as_instructor, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped the student from the topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#set_priority' do&lt;br /&gt;
    let(:params) { {participant_id: '1', id: 1, topic: [1], assignment_id: 1} }&lt;br /&gt;
    let(:team_id) { participant.team.try(:id) }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    it 'sets priority of bidding topic and redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(AssignmentParticipant).to receive(:find_by).with(id: params[:participant_id]).and_return(participant)&lt;br /&gt;
      allow(SignUpTopic).to receive_message_chain(:find, :assignment).with(params[:topic].first).with(no_args).and_return(assignment)&lt;br /&gt;
      allow(Bid).to receive(:where).with(any_args).and_return(bids)&lt;br /&gt;
      allow(bid).to receive(:topic_id).and_return(1)&lt;br /&gt;
      allow(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      expect(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      get :set_priority, params&lt;br /&gt;
      expect(response).to redirect_to action: 'list', assignment_id: params[:assignment_id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#save_topic_deadlines' do&lt;br /&gt;
    let(:params) { {assignment_id: 1, due_date: {}} }&lt;br /&gt;
    let(:topics) { [topic] }&lt;br /&gt;
    context 'when topic_due_date cannot be found' do&lt;br /&gt;
      it 'creates a new topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return nil&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(TopicDueDate).to receive(:create).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic_due_date can be found' do&lt;br /&gt;
      it 'updates the existing topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return([due_date])&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(due_date).to receive(:update_attributes).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#show_team' do&lt;br /&gt;
    let(:params) { {id: '1', assignment_id: 1} }&lt;br /&gt;
    it 'renders show_team page' do&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([signed_up_team])&lt;br /&gt;
      get :show_team, params&lt;br /&gt;
      expect(response).to render_template(:show_team)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#switch_original_topic_to_approved_suggested_topic' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    it 'redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(TeamsUser).to receive(:team_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:topic_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignUpTopic).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([])&lt;br /&gt;
      get :switch_original_topic_to_approved_suggested_topic, params, session&lt;br /&gt;
      expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== '''Reference''' ==&lt;br /&gt;
[https://www.dropbox.com/s/nr3fhk3wbxt4qsr/录音_480p.mov?dl=0]video about Refactor&lt;br /&gt;
&lt;br /&gt;
[https://www.dropbox.com/s/l734csf16l4vghs/test_finish_480.MOV?dl=0]video about test&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111203</id>
		<title>CSC/ECE 517 Fall 2017/E1747 Refactor sign up sheet controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111203"/>
		<updated>2017-11-02T14:40:05Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Introduction of Expertiza''' ==&lt;br /&gt;
Expertiza is a web application where students can form their teams, submit their projects, peer-review other's work, view their grades and so on. Expertiza is open-source, maintained by students and faculty in NC State university.&lt;br /&gt;
&lt;br /&gt;
== '''Why refactoring?''' ==&lt;br /&gt;
Code Refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.&lt;br /&gt;
Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility. Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may also resolve hidden, dormant, or undiscovered bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.&lt;br /&gt;
&lt;br /&gt;
== '''Test Last Development''' ==&lt;br /&gt;
Test last development is a traditional and most common approach of testing where testing is done only after implementation code has been written. But test driven development, test first coding or TDD is a relatively new concept where test cases are written before writing the implementation code.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction of RSpec''' ==&lt;br /&gt;
RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring. It contains its own mocking framework that is fully integrated into the framework based upon JMock. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec gem which consists of 3 other gems namely rspec-core , rspec-expectation and rspec-mock.&lt;br /&gt;
&lt;br /&gt;
== '''Problem Statement''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment. sign_up_sheet_controller.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These method need to be broken down into simpler and more specific methods that are easier to read or understand. Also, the few instances of code duplication that exist should be removed.&lt;br /&gt;
&lt;br /&gt;
=='''Tasks'''==&lt;br /&gt;
&lt;br /&gt;
* Refactor &amp;quot;list&amp;quot;, &amp;quot;save_topic_deadlines&amp;quot; method&lt;br /&gt;
** 1.Split into several simpler methods and assign reasonable names &lt;br /&gt;
** 2.Extract duplicated code into separate methods&lt;br /&gt;
* Use find_by instead of dynamic method&lt;br /&gt;
* Complete the pending tests in sign_up_sheet_controller_spec.rb, and write integration tests for newly-created methods. Refactor corresponding methods, and only then finish pending tests.&lt;br /&gt;
&lt;br /&gt;
== '''Refactor''' ==&lt;br /&gt;
'''Refactor &amp;quot;list&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
The 'list' method is a long method which is fairly complex and hard to read. It is not following &amp;quot;good Ruby and Rails coding practices&amp;quot;.  In &amp;quot;list&amp;quot; method we found that &amp;quot;signed_up_topics&amp;quot; occurred too many times, here we split it into three different methods, named as &amp;quot;find_signed_up_topics&amp;quot; ,&amp;quot;find_selected_topics&amp;quot; and &amp;quot;list&amp;quot;. The changes are showed below.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority) &lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      @bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
      signed_up_topics &amp;amp;= @sign_up_topics&lt;br /&gt;
      @sign_up_topics -= signed_up_topics&lt;br /&gt;
      @bids = signed_up_topics&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
      # sign up again unless the former was a waitlisted topic&lt;br /&gt;
      # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
      users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)&lt;br /&gt;
      @selected_topics = if users_team.empty?&lt;br /&gt;
                           nil&lt;br /&gt;
                         else&lt;br /&gt;
                           # TODO: fix this; cant use 0&lt;br /&gt;
                           SignedUpTeam.find_user_signup_topics(@assignment.id, users_team[0].t_id)&lt;br /&gt;
                         end&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
 #Operating on the student bids, return signed up topics.&lt;br /&gt;
   def find_signed_up_topics student_bids&lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      student_bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
     # signed_up_topics &amp;amp;= sign_up_topics&lt;br /&gt;
      return signed_up_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 #Operating on assignments, return selected topics.&lt;br /&gt;
   def find_selected_topics assignment&lt;br /&gt;
    # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
    # sign up again unless the former was a waitlisted topic&lt;br /&gt;
    # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
    users_team = SignedUpTeam.find_team_users(assignment.id, session[:user].id)&lt;br /&gt;
    selected_topics = if users_team.empty?&lt;br /&gt;
                         nil&lt;br /&gt;
                       else&lt;br /&gt;
                         # TODO: fix this; cant use 0&lt;br /&gt;
                         SignedUpTeam.find_user_signup_topics(assignment.id, users_team.first.t_id)&lt;br /&gt;
                       end&lt;br /&gt;
    return selected_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = find_signed_up_topics @student_bids&lt;br /&gt;
      @sign_up_topics = @bids&lt;br /&gt;
    else&lt;br /&gt;
      @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      @selected_topics = find_selected_topics @assignment&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
'''Refactor &amp;quot;save_topic_deadlines&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
We found the &amp;quot;save_topic_deadlines&amp;quot; method is not hard to read, but it's a little bit long and some of the codes can be reused. Here The &amp;quot;save_topic_deadlines&amp;quot; method is modified and two new methods were created, named as &amp;quot;get_instance_variable_assignment&amp;quot; and &amp;quot;get_instance_variable_topic&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if instance_variable_get('@topic_' + deadline_type + '_due_date') == instance_variable_get('@assignment_' + deadline_type + '_due_date')&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].flag,&lt;br /&gt;
              threshold:                   instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].deadline_name,&lt;br /&gt;
              description_url:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if get_instance_varibale_topic == get_instance_variable_assignment&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        get_instance_variable_assignment[i - 1].flag,&lt;br /&gt;
              threshold:                   get_instance_variable_assignment[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              get_instance_variable_assignment[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               get_instance_variable_assignment[i - 1].deadline_name,&lt;br /&gt;
              description_url:             get_instance_variable_assignment[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   #refactor duplicate code present in both create params&lt;br /&gt;
  def get_instance_variable_assignment&lt;br /&gt;
    instance_variable_get('@assignment_' + deadline_type + '_due_dates')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def get_instance_varibale_topic&lt;br /&gt;
    instance_variable_get('@topic_' + deadline_type + '_due_date')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test plan''' ==&lt;br /&gt;
1.Complete pending test in sign_up_sheet_controller_spec.rb.&lt;br /&gt;
2.Write integration test for newly created methods.&lt;br /&gt;
3.Write test for refactor methods.&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test cases''' ==&lt;br /&gt;
The most important two tests are about &amp;quot;list&amp;quot; method and &amp;quot;save_topic_deadlines&amp;quot; method. Here we wrote 27 examples of test and all of them are passed.&lt;br /&gt;
 describe SignUpSheetController do&lt;br /&gt;
  let(:assignment) { build(:assignment, id: 1, instructor_id: 6, due_dates: [due_date], microtask: true, staggered_deadline: true) }&lt;br /&gt;
  let(:instructor) { build(:instructor, id: 6) }&lt;br /&gt;
  let(:student) { build(:student, id: 8) }&lt;br /&gt;
  let(:participant) { build(:participant, id: 1, user_id: 6, assignment: assignment) }&lt;br /&gt;
  let(:topic) { build(:topic, id: 1) }&lt;br /&gt;
  let(:signed_up_team) { build(:signed_up_team, team: team, topic: topic) }&lt;br /&gt;
  let(:signed_up_team2) { build(:signed_up_team, team_id: 2, is_waitlisted: true) }&lt;br /&gt;
  let(:team) { build(:assignment_team, id: 1, assignment: assignment) }&lt;br /&gt;
  let(:due_date) { build(:assignment_due_date, deadline_type_id: 1) }&lt;br /&gt;
  let(:due_date2) { build(:assignment_due_date, deadline_type_id: 2) }&lt;br /&gt;
  let(:bid) { Bid.new(topic_id: 1, priority: 1) }&lt;br /&gt;
  let(:team_user) { build(:team_user) }&lt;br /&gt;
   before(:each) do&lt;br /&gt;
    allow(Assignment).to receive(:find).with('1').and_return(assignment)&lt;br /&gt;
    allow(Assignment).to receive(:find).with(1).and_return(assignment)&lt;br /&gt;
    stub_current_user(instructor, instructor.role.name, instructor.role)&lt;br /&gt;
    allow(SignUpTopic).to receive(:find).with('1').and_return(topic)&lt;br /&gt;
    allow(Participant).to receive(:find_by).with(id: '1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with(1).and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: student.id, parent_id: 1).and_return(participant)&lt;br /&gt;
    allow(Team).to receive(:find).with('1').and_return(team)&lt;br /&gt;
    allow(TeamsUser).to receive(:find_by).with(team_id: 1).and_return(team_user)&lt;br /&gt;
    allow(team_user).to receive(:user).and_return(student)&lt;br /&gt;
    allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
    allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
    allow(participant.team).to receive(:hyperlinks).and_return([])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#new' do&lt;br /&gt;
    it 'builds a new sign up topic and renders sign_up_sheet#new page' do&lt;br /&gt;
      params = {id: 1}&lt;br /&gt;
      get :new, params&lt;br /&gt;
      expect(response).to render_template(:new)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#create' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 0, category: 'test', id: 1}} }&lt;br /&gt;
      context 'when new topic can be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and redirects to assignment#edit page' do&lt;br /&gt;
          session[:user] = participant&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(true)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
  context 'when new topic cannot be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and renders sign_up_sheet#new page' do&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(false)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to render_template(:new)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
   context 'when topic can be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 120, category: 'test', id: 1}} }&lt;br /&gt;
      it 'updates the existing topic and redirects to sign_up_sheet#add_signup_topics_staggered page' do&lt;br /&gt;
        new_topic = build(:topic, topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1)&lt;br /&gt;
        allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(new_topic)&lt;br /&gt;
        post :create, params&lt;br /&gt;
        expect(response).to redirect_to('/sign_up_sheet/add_signup_topics_staggered?id=' + params[:id].to_s)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 describe '#destroy' do&lt;br /&gt;
    let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
    context 'when topic can be found' do&lt;br /&gt;
      it 'redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :destroy, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
   context 'when topic cannot be found' do&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :destroy, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be deleted.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#edit' do&lt;br /&gt;
    let(:params) { {id: 1} }&lt;br /&gt;
    it 'renders sign_up_sheet#edit page' do&lt;br /&gt;
      get :edit, params&lt;br /&gt;
      expect(response).to render_template(:edit)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#update' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :update, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be updated.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic can be found' do&lt;br /&gt;
      let(:params) do&lt;br /&gt;
        {&lt;br /&gt;
          id: 1, assignment_id: 1,&lt;br /&gt;
          topic: {topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1, micropayment: 0, description: 'test', link: 'test'}&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
      it 'updates current topic and redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :update, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#list' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when current assignment is intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#intelligent_topic_selection page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_filled).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_waitlisted).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(assignment_id: assignment.id, private_to: nil).and_return([topic])&lt;br /&gt;
        allow(assignment).to receive(:max_team_size).and_return(1)&lt;br /&gt;
        allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(true)&lt;br /&gt;
        allow(Bid).to receive(:where).with(team_id: team.try(:id)).and_return(bids)&lt;br /&gt;
        allow(bids).to receive(:order).with(:priority).and_return(bids)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_by).with(id: bid.topic_id).and_return(topic)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(7).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(6).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(1).and_return(due_date)&lt;br /&gt;
        allow(assignment).to receive(:staggered_deadline).and_return(true)&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(1, student.id).and_return([])&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:intelligent_topic_selection)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when current assignment is not intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(false)&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:list)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#sign_up' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when SignUpSheet.signup_team method return nil' do&lt;br /&gt;
      it 'shows an error flash message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
        get :sign_up, params, session&lt;br /&gt;
        expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You've already signed up for a topic!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#signup_as_instructor_action' do&lt;br /&gt;
    let(:params) { {username: '1'} }&lt;br /&gt;
    context 'when user cannot be found' do&lt;br /&gt;
      it 'shows an flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(User).to receive(:find_by).with(any_args).and_return(nil)&lt;br /&gt;
        get :signup_as_instructor_action, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;That student does not exist!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
     context 'when user can be found' do&lt;br /&gt;
      context 'when an assignment_participant can be found' do&lt;br /&gt;
        context 'when creating team related objects successfully' do&lt;br /&gt;
          it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(signed_up_team)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:success]).to eq(&amp;quot;You have successfully signed up the student for the topic!&amp;quot;)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
         context 'when creating team related objects unsuccessfully' do&lt;br /&gt;
          it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:error]).to eq(&amp;quot;The student has already signed up for a topic!&amp;quot;)&lt;br /&gt;
            expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
       context 'when an assignment_participant cannot be found' do&lt;br /&gt;
        it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
          allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
          allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
          get :signup_as_instructor_action&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student is not registered for the assignment!&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;You have already submitted your work, so you are not allowed to drop your topic.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop your topic after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: student} }&lt;br /&gt;
      it 'shows a flash success message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).and_return(signed_up_team2)&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        delete :delete_signup, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped your topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup_as_instructor' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup_as_instructor, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student has already submitted their work, so you are not allowed to remove them.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup_as_instructor, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop a student after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: instructor} }&lt;br /&gt;
      it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).with(any_args).with(no_args).and_return(signed_up_team2)&lt;br /&gt;
        delete :delete_signup_as_instructor, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped the student from the topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#set_priority' do&lt;br /&gt;
    let(:params) { {participant_id: '1', id: 1, topic: [1], assignment_id: 1} }&lt;br /&gt;
    let(:team_id) { participant.team.try(:id) }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    it 'sets priority of bidding topic and redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(AssignmentParticipant).to receive(:find_by).with(id: params[:participant_id]).and_return(participant)&lt;br /&gt;
      allow(SignUpTopic).to receive_message_chain(:find, :assignment).with(params[:topic].first).with(no_args).and_return(assignment)&lt;br /&gt;
      allow(Bid).to receive(:where).with(any_args).and_return(bids)&lt;br /&gt;
      allow(bid).to receive(:topic_id).and_return(1)&lt;br /&gt;
      allow(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      expect(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      get :set_priority, params&lt;br /&gt;
      expect(response).to redirect_to action: 'list', assignment_id: params[:assignment_id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#save_topic_deadlines' do&lt;br /&gt;
    let(:params) { {assignment_id: 1, due_date: {}} }&lt;br /&gt;
    let(:topics) { [topic] }&lt;br /&gt;
    context 'when topic_due_date cannot be found' do&lt;br /&gt;
      it 'creates a new topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return nil&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(TopicDueDate).to receive(:create).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic_due_date can be found' do&lt;br /&gt;
      it 'updates the existing topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return([due_date])&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(due_date).to receive(:update_attributes).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#show_team' do&lt;br /&gt;
    let(:params) { {id: '1', assignment_id: 1} }&lt;br /&gt;
    it 'renders show_team page' do&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([signed_up_team])&lt;br /&gt;
      get :show_team, params&lt;br /&gt;
      expect(response).to render_template(:show_team)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#switch_original_topic_to_approved_suggested_topic' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    it 'redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(TeamsUser).to receive(:team_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:topic_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignUpTopic).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([])&lt;br /&gt;
      get :switch_original_topic_to_approved_suggested_topic, params, session&lt;br /&gt;
      expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== '''Reference''' ==&lt;br /&gt;
[https://www.dropbox.com/s/nr3fhk3wbxt4qsr/录音_480p.mov?dl=0]video about Refactor&lt;br /&gt;
&lt;br /&gt;
[https://www.dropbox.com/s/l734csf16l4vghs/test_finish_480.MOV?dl=0]video about test&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111202</id>
		<title>CSC/ECE 517 Fall 2017/E1747 Refactor sign up sheet controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111202"/>
		<updated>2017-11-02T14:36:48Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Introduction of Expertiza''' ==&lt;br /&gt;
Expertiza is a web application where students can form their teams, submit their projects, peer-review other's work, view their grades and so on. Expertiza is open-source, maintained by students and faculty in NC State university.&lt;br /&gt;
&lt;br /&gt;
== '''Why refactoring?''' ==&lt;br /&gt;
Code Refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.&lt;br /&gt;
Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility. Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may also resolve hidden, dormant, or undiscovered bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.&lt;br /&gt;
&lt;br /&gt;
== '''Test Last Development''' ==&lt;br /&gt;
Test last development is a traditional and most common approach of testing where testing is done only after implementation code has been written. But test driven development, test first coding or TDD is a relatively new concept where test cases are written before writing the implementation code.&lt;br /&gt;
&lt;br /&gt;
== '''Introduction of RSpec''' ==&lt;br /&gt;
RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring. It contains its own mocking framework that is fully integrated into the framework based upon JMock. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec gem which consists of 3 other gems namely rspec-core , rspec-expectation and rspec-mock.&lt;br /&gt;
&lt;br /&gt;
== '''Problem Statement''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment. sign_up_sheet_controller.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These method need to be broken down into simpler and more specific methods that are easier to read or understand. Also, the few instances of code duplication that exist should be removed.&lt;br /&gt;
&lt;br /&gt;
=='''Tasks'''==&lt;br /&gt;
&lt;br /&gt;
* Refactor &amp;quot;list&amp;quot;, &amp;quot;save_topic_deadlines&amp;quot; method&lt;br /&gt;
** 1.Split into several simpler methods and assign reasonable names &lt;br /&gt;
** 2.Extract duplicated code into separate methods&lt;br /&gt;
* Use find_by instead of dynamic method&lt;br /&gt;
* Complete the pending tests in sign_up_sheet_controller_spec.rb, and write integration tests for newly-created methods. Refactor corresponding methods, and only then finish pending tests.&lt;br /&gt;
&lt;br /&gt;
== '''Refactor''' ==&lt;br /&gt;
'''Refactor &amp;quot;list&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
The 'list' method is a long method which is fairly complex and hard to read. It is not following &amp;quot;good Ruby and Rails coding practices&amp;quot;.  In &amp;quot;list&amp;quot; method we found that &amp;quot;signed_up_topics&amp;quot; occurred too many times, here we split it into three different methods, named as &amp;quot;find_signed_up_topics&amp;quot; ,&amp;quot;find_selected_topics&amp;quot; and &amp;quot;list&amp;quot;. The changes are showed below.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority) &lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      @bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
      signed_up_topics &amp;amp;= @sign_up_topics&lt;br /&gt;
      @sign_up_topics -= signed_up_topics&lt;br /&gt;
      @bids = signed_up_topics&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
      # sign up again unless the former was a waitlisted topic&lt;br /&gt;
      # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
      users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)&lt;br /&gt;
      @selected_topics = if users_team.empty?&lt;br /&gt;
                           nil&lt;br /&gt;
                         else&lt;br /&gt;
                           # TODO: fix this; cant use 0&lt;br /&gt;
                           SignedUpTeam.find_user_signup_topics(@assignment.id, users_team[0].t_id)&lt;br /&gt;
                         end&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
 #Operating on the student bids, return signed up topics.&lt;br /&gt;
   def find_signed_up_topics student_bids&lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      student_bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
     # signed_up_topics &amp;amp;= sign_up_topics&lt;br /&gt;
      return signed_up_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 #Operating on assignments, return selected topics.&lt;br /&gt;
   def find_selected_topics assignment&lt;br /&gt;
    # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
    # sign up again unless the former was a waitlisted topic&lt;br /&gt;
    # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
    users_team = SignedUpTeam.find_team_users(assignment.id, session[:user].id)&lt;br /&gt;
    selected_topics = if users_team.empty?&lt;br /&gt;
                         nil&lt;br /&gt;
                       else&lt;br /&gt;
                         # TODO: fix this; cant use 0&lt;br /&gt;
                         SignedUpTeam.find_user_signup_topics(assignment.id, users_team.first.t_id)&lt;br /&gt;
                       end&lt;br /&gt;
    return selected_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = find_signed_up_topics @student_bids&lt;br /&gt;
      @sign_up_topics = @bids&lt;br /&gt;
    else&lt;br /&gt;
      @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      @selected_topics = find_selected_topics @assignment&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
'''Refactor &amp;quot;save_topic_deadlines&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
We found the &amp;quot;save_topic_deadlines&amp;quot; method is not hard to read, but it's a little bit long and some of the codes can be reused. Here The &amp;quot;save_topic_deadlines&amp;quot; method is modified and two new methods were created, named as &amp;quot;get_instance_variable_assignment&amp;quot; and &amp;quot;get_instance_variable_topic&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if instance_variable_get('@topic_' + deadline_type + '_due_date') == instance_variable_get('@assignment_' + deadline_type + '_due_date')&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].flag,&lt;br /&gt;
              threshold:                   instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].deadline_name,&lt;br /&gt;
              description_url:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if get_instance_varibale_topic == get_instance_variable_assignment&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        get_instance_variable_assignment[i - 1].flag,&lt;br /&gt;
              threshold:                   get_instance_variable_assignment[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              get_instance_variable_assignment[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               get_instance_variable_assignment[i - 1].deadline_name,&lt;br /&gt;
              description_url:             get_instance_variable_assignment[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   #refactor duplicate code present in both create params&lt;br /&gt;
  def get_instance_variable_assignment&lt;br /&gt;
    instance_variable_get('@assignment_' + deadline_type + '_due_dates')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def get_instance_varibale_topic&lt;br /&gt;
    instance_variable_get('@topic_' + deadline_type + '_due_date')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test''' ==&lt;br /&gt;
The most important two tests are about &amp;quot;list&amp;quot; method and &amp;quot;save_topic_deadlines&amp;quot; method. Here we wrote 27 examples of test and all of them are passed.&lt;br /&gt;
 describe SignUpSheetController do&lt;br /&gt;
  let(:assignment) { build(:assignment, id: 1, instructor_id: 6, due_dates: [due_date], microtask: true, staggered_deadline: true) }&lt;br /&gt;
  let(:instructor) { build(:instructor, id: 6) }&lt;br /&gt;
  let(:student) { build(:student, id: 8) }&lt;br /&gt;
  let(:participant) { build(:participant, id: 1, user_id: 6, assignment: assignment) }&lt;br /&gt;
  let(:topic) { build(:topic, id: 1) }&lt;br /&gt;
  let(:signed_up_team) { build(:signed_up_team, team: team, topic: topic) }&lt;br /&gt;
  let(:signed_up_team2) { build(:signed_up_team, team_id: 2, is_waitlisted: true) }&lt;br /&gt;
  let(:team) { build(:assignment_team, id: 1, assignment: assignment) }&lt;br /&gt;
  let(:due_date) { build(:assignment_due_date, deadline_type_id: 1) }&lt;br /&gt;
  let(:due_date2) { build(:assignment_due_date, deadline_type_id: 2) }&lt;br /&gt;
  let(:bid) { Bid.new(topic_id: 1, priority: 1) }&lt;br /&gt;
  let(:team_user) { build(:team_user) }&lt;br /&gt;
   before(:each) do&lt;br /&gt;
    allow(Assignment).to receive(:find).with('1').and_return(assignment)&lt;br /&gt;
    allow(Assignment).to receive(:find).with(1).and_return(assignment)&lt;br /&gt;
    stub_current_user(instructor, instructor.role.name, instructor.role)&lt;br /&gt;
    allow(SignUpTopic).to receive(:find).with('1').and_return(topic)&lt;br /&gt;
    allow(Participant).to receive(:find_by).with(id: '1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with(1).and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: student.id, parent_id: 1).and_return(participant)&lt;br /&gt;
    allow(Team).to receive(:find).with('1').and_return(team)&lt;br /&gt;
    allow(TeamsUser).to receive(:find_by).with(team_id: 1).and_return(team_user)&lt;br /&gt;
    allow(team_user).to receive(:user).and_return(student)&lt;br /&gt;
    allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
    allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
    allow(participant.team).to receive(:hyperlinks).and_return([])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#new' do&lt;br /&gt;
    it 'builds a new sign up topic and renders sign_up_sheet#new page' do&lt;br /&gt;
      params = {id: 1}&lt;br /&gt;
      get :new, params&lt;br /&gt;
      expect(response).to render_template(:new)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#create' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 0, category: 'test', id: 1}} }&lt;br /&gt;
      context 'when new topic can be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and redirects to assignment#edit page' do&lt;br /&gt;
          session[:user] = participant&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(true)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
  context 'when new topic cannot be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and renders sign_up_sheet#new page' do&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(false)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to render_template(:new)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
   context 'when topic can be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 120, category: 'test', id: 1}} }&lt;br /&gt;
      it 'updates the existing topic and redirects to sign_up_sheet#add_signup_topics_staggered page' do&lt;br /&gt;
        new_topic = build(:topic, topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1)&lt;br /&gt;
        allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(new_topic)&lt;br /&gt;
        post :create, params&lt;br /&gt;
        expect(response).to redirect_to('/sign_up_sheet/add_signup_topics_staggered?id=' + params[:id].to_s)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 describe '#destroy' do&lt;br /&gt;
    let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
    context 'when topic can be found' do&lt;br /&gt;
      it 'redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :destroy, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
   context 'when topic cannot be found' do&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :destroy, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be deleted.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#edit' do&lt;br /&gt;
    let(:params) { {id: 1} }&lt;br /&gt;
    it 'renders sign_up_sheet#edit page' do&lt;br /&gt;
      get :edit, params&lt;br /&gt;
      expect(response).to render_template(:edit)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#update' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :update, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be updated.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic can be found' do&lt;br /&gt;
      let(:params) do&lt;br /&gt;
        {&lt;br /&gt;
          id: 1, assignment_id: 1,&lt;br /&gt;
          topic: {topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1, micropayment: 0, description: 'test', link: 'test'}&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
      it 'updates current topic and redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :update, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#list' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when current assignment is intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#intelligent_topic_selection page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_filled).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_waitlisted).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(assignment_id: assignment.id, private_to: nil).and_return([topic])&lt;br /&gt;
        allow(assignment).to receive(:max_team_size).and_return(1)&lt;br /&gt;
        allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(true)&lt;br /&gt;
        allow(Bid).to receive(:where).with(team_id: team.try(:id)).and_return(bids)&lt;br /&gt;
        allow(bids).to receive(:order).with(:priority).and_return(bids)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_by).with(id: bid.topic_id).and_return(topic)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(7).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(6).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(1).and_return(due_date)&lt;br /&gt;
        allow(assignment).to receive(:staggered_deadline).and_return(true)&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(1, student.id).and_return([])&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:intelligent_topic_selection)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when current assignment is not intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(false)&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:list)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#sign_up' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when SignUpSheet.signup_team method return nil' do&lt;br /&gt;
      it 'shows an error flash message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
        get :sign_up, params, session&lt;br /&gt;
        expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You've already signed up for a topic!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#signup_as_instructor_action' do&lt;br /&gt;
    let(:params) { {username: '1'} }&lt;br /&gt;
    context 'when user cannot be found' do&lt;br /&gt;
      it 'shows an flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(User).to receive(:find_by).with(any_args).and_return(nil)&lt;br /&gt;
        get :signup_as_instructor_action, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;That student does not exist!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
     context 'when user can be found' do&lt;br /&gt;
      context 'when an assignment_participant can be found' do&lt;br /&gt;
        context 'when creating team related objects successfully' do&lt;br /&gt;
          it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(signed_up_team)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:success]).to eq(&amp;quot;You have successfully signed up the student for the topic!&amp;quot;)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
         context 'when creating team related objects unsuccessfully' do&lt;br /&gt;
          it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:error]).to eq(&amp;quot;The student has already signed up for a topic!&amp;quot;)&lt;br /&gt;
            expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
       context 'when an assignment_participant cannot be found' do&lt;br /&gt;
        it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
          allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
          allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
          get :signup_as_instructor_action&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student is not registered for the assignment!&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;You have already submitted your work, so you are not allowed to drop your topic.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop your topic after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: student} }&lt;br /&gt;
      it 'shows a flash success message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).and_return(signed_up_team2)&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        delete :delete_signup, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped your topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup_as_instructor' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup_as_instructor, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student has already submitted their work, so you are not allowed to remove them.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup_as_instructor, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop a student after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: instructor} }&lt;br /&gt;
      it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).with(any_args).with(no_args).and_return(signed_up_team2)&lt;br /&gt;
        delete :delete_signup_as_instructor, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped the student from the topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#set_priority' do&lt;br /&gt;
    let(:params) { {participant_id: '1', id: 1, topic: [1], assignment_id: 1} }&lt;br /&gt;
    let(:team_id) { participant.team.try(:id) }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    it 'sets priority of bidding topic and redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(AssignmentParticipant).to receive(:find_by).with(id: params[:participant_id]).and_return(participant)&lt;br /&gt;
      allow(SignUpTopic).to receive_message_chain(:find, :assignment).with(params[:topic].first).with(no_args).and_return(assignment)&lt;br /&gt;
      allow(Bid).to receive(:where).with(any_args).and_return(bids)&lt;br /&gt;
      allow(bid).to receive(:topic_id).and_return(1)&lt;br /&gt;
      allow(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      expect(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      get :set_priority, params&lt;br /&gt;
      expect(response).to redirect_to action: 'list', assignment_id: params[:assignment_id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#save_topic_deadlines' do&lt;br /&gt;
    let(:params) { {assignment_id: 1, due_date: {}} }&lt;br /&gt;
    let(:topics) { [topic] }&lt;br /&gt;
    context 'when topic_due_date cannot be found' do&lt;br /&gt;
      it 'creates a new topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return nil&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(TopicDueDate).to receive(:create).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic_due_date can be found' do&lt;br /&gt;
      it 'updates the existing topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return([due_date])&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(due_date).to receive(:update_attributes).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#show_team' do&lt;br /&gt;
    let(:params) { {id: '1', assignment_id: 1} }&lt;br /&gt;
    it 'renders show_team page' do&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([signed_up_team])&lt;br /&gt;
      get :show_team, params&lt;br /&gt;
      expect(response).to render_template(:show_team)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#switch_original_topic_to_approved_suggested_topic' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    it 'redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(TeamsUser).to receive(:team_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:topic_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignUpTopic).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([])&lt;br /&gt;
      get :switch_original_topic_to_approved_suggested_topic, params, session&lt;br /&gt;
      expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== '''Reference''' ==&lt;br /&gt;
[https://www.dropbox.com/s/nr3fhk3wbxt4qsr/录音_480p.mov?dl=0]video about Refactor&lt;br /&gt;
&lt;br /&gt;
[https://www.dropbox.com/s/l734csf16l4vghs/test_finish_480.MOV?dl=0]video about test&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111201</id>
		<title>CSC/ECE 517 Fall 2017/E1747 Refactor sign up sheet controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111201"/>
		<updated>2017-11-02T14:31:59Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: /* About sign_up_sheet_controller.rb */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Introduction of Expertiza''' ==&lt;br /&gt;
Expertiza is a web application where students can form their teams, submit their projects, peer-review other's work, view their grades and so on. Expertiza is open-source, maintained by students and faculty in NC State university.&lt;br /&gt;
&lt;br /&gt;
== '''Why refactoring?''' ==&lt;br /&gt;
Code Refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.&lt;br /&gt;
Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility. Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may also resolve hidden, dormant, or undiscovered bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.&lt;br /&gt;
&lt;br /&gt;
== '''Problem Statement''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment. sign_up_sheet_controller.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These method need to be broken down into simpler and more specific methods that are easier to read or understand. Also, the few instances of code duplication that exist should be removed.&lt;br /&gt;
&lt;br /&gt;
=='''Tasks'''==&lt;br /&gt;
&lt;br /&gt;
* Refactor &amp;quot;list&amp;quot;, &amp;quot;save_topic_deadlines&amp;quot; method&lt;br /&gt;
** 1.Split into several simpler methods and assign reasonable names &lt;br /&gt;
** 2.Extract duplicated code into separate methods&lt;br /&gt;
* Use find_by instead of dynamic method&lt;br /&gt;
* Complete the pending tests in sign_up_sheet_controller_spec.rb, and write integration tests for newly-created methods. Refactor corresponding methods, and only then finish pending tests.&lt;br /&gt;
&lt;br /&gt;
== '''Refactor''' ==&lt;br /&gt;
'''Refactor &amp;quot;list&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
The 'list' method is a long method which is fairly complex and hard to read. It is not following &amp;quot;good Ruby and Rails coding practices&amp;quot;.  In &amp;quot;list&amp;quot; method we found that &amp;quot;signed_up_topics&amp;quot; occurred too many times, here we split it into three different methods, named as &amp;quot;find_signed_up_topics&amp;quot; ,&amp;quot;find_selected_topics&amp;quot; and &amp;quot;list&amp;quot;. The changes are showed below.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority) &lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      @bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
      signed_up_topics &amp;amp;= @sign_up_topics&lt;br /&gt;
      @sign_up_topics -= signed_up_topics&lt;br /&gt;
      @bids = signed_up_topics&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
      # sign up again unless the former was a waitlisted topic&lt;br /&gt;
      # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
      users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)&lt;br /&gt;
      @selected_topics = if users_team.empty?&lt;br /&gt;
                           nil&lt;br /&gt;
                         else&lt;br /&gt;
                           # TODO: fix this; cant use 0&lt;br /&gt;
                           SignedUpTeam.find_user_signup_topics(@assignment.id, users_team[0].t_id)&lt;br /&gt;
                         end&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
 #Operating on the student bids, return signed up topics.&lt;br /&gt;
   def find_signed_up_topics student_bids&lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      student_bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
     # signed_up_topics &amp;amp;= sign_up_topics&lt;br /&gt;
      return signed_up_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 #Operating on assignments, return selected topics.&lt;br /&gt;
   def find_selected_topics assignment&lt;br /&gt;
    # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
    # sign up again unless the former was a waitlisted topic&lt;br /&gt;
    # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
    users_team = SignedUpTeam.find_team_users(assignment.id, session[:user].id)&lt;br /&gt;
    selected_topics = if users_team.empty?&lt;br /&gt;
                         nil&lt;br /&gt;
                       else&lt;br /&gt;
                         # TODO: fix this; cant use 0&lt;br /&gt;
                         SignedUpTeam.find_user_signup_topics(assignment.id, users_team.first.t_id)&lt;br /&gt;
                       end&lt;br /&gt;
    return selected_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = find_signed_up_topics @student_bids&lt;br /&gt;
      @sign_up_topics = @bids&lt;br /&gt;
    else&lt;br /&gt;
      @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      @selected_topics = find_selected_topics @assignment&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
'''Refactor &amp;quot;save_topic_deadlines&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
We found the &amp;quot;save_topic_deadlines&amp;quot; method is not hard to read, but it's a little bit long and some of the codes can be reused. Here The &amp;quot;save_topic_deadlines&amp;quot; method is modified and two new methods were created, named as &amp;quot;get_instance_variable_assignment&amp;quot; and &amp;quot;get_instance_variable_topic&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if instance_variable_get('@topic_' + deadline_type + '_due_date') == instance_variable_get('@assignment_' + deadline_type + '_due_date')&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].flag,&lt;br /&gt;
              threshold:                   instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].deadline_name,&lt;br /&gt;
              description_url:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if get_instance_varibale_topic == get_instance_variable_assignment&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        get_instance_variable_assignment[i - 1].flag,&lt;br /&gt;
              threshold:                   get_instance_variable_assignment[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              get_instance_variable_assignment[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               get_instance_variable_assignment[i - 1].deadline_name,&lt;br /&gt;
              description_url:             get_instance_variable_assignment[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   #refactor duplicate code present in both create params&lt;br /&gt;
  def get_instance_variable_assignment&lt;br /&gt;
    instance_variable_get('@assignment_' + deadline_type + '_due_dates')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def get_instance_varibale_topic&lt;br /&gt;
    instance_variable_get('@topic_' + deadline_type + '_due_date')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test''' ==&lt;br /&gt;
The most important two tests are about &amp;quot;list&amp;quot; method and &amp;quot;save_topic_deadlines&amp;quot; method. Here we wrote 27 examples of test and all of them are passed.&lt;br /&gt;
 describe SignUpSheetController do&lt;br /&gt;
  let(:assignment) { build(:assignment, id: 1, instructor_id: 6, due_dates: [due_date], microtask: true, staggered_deadline: true) }&lt;br /&gt;
  let(:instructor) { build(:instructor, id: 6) }&lt;br /&gt;
  let(:student) { build(:student, id: 8) }&lt;br /&gt;
  let(:participant) { build(:participant, id: 1, user_id: 6, assignment: assignment) }&lt;br /&gt;
  let(:topic) { build(:topic, id: 1) }&lt;br /&gt;
  let(:signed_up_team) { build(:signed_up_team, team: team, topic: topic) }&lt;br /&gt;
  let(:signed_up_team2) { build(:signed_up_team, team_id: 2, is_waitlisted: true) }&lt;br /&gt;
  let(:team) { build(:assignment_team, id: 1, assignment: assignment) }&lt;br /&gt;
  let(:due_date) { build(:assignment_due_date, deadline_type_id: 1) }&lt;br /&gt;
  let(:due_date2) { build(:assignment_due_date, deadline_type_id: 2) }&lt;br /&gt;
  let(:bid) { Bid.new(topic_id: 1, priority: 1) }&lt;br /&gt;
  let(:team_user) { build(:team_user) }&lt;br /&gt;
   before(:each) do&lt;br /&gt;
    allow(Assignment).to receive(:find).with('1').and_return(assignment)&lt;br /&gt;
    allow(Assignment).to receive(:find).with(1).and_return(assignment)&lt;br /&gt;
    stub_current_user(instructor, instructor.role.name, instructor.role)&lt;br /&gt;
    allow(SignUpTopic).to receive(:find).with('1').and_return(topic)&lt;br /&gt;
    allow(Participant).to receive(:find_by).with(id: '1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with(1).and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: student.id, parent_id: 1).and_return(participant)&lt;br /&gt;
    allow(Team).to receive(:find).with('1').and_return(team)&lt;br /&gt;
    allow(TeamsUser).to receive(:find_by).with(team_id: 1).and_return(team_user)&lt;br /&gt;
    allow(team_user).to receive(:user).and_return(student)&lt;br /&gt;
    allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
    allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
    allow(participant.team).to receive(:hyperlinks).and_return([])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#new' do&lt;br /&gt;
    it 'builds a new sign up topic and renders sign_up_sheet#new page' do&lt;br /&gt;
      params = {id: 1}&lt;br /&gt;
      get :new, params&lt;br /&gt;
      expect(response).to render_template(:new)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#create' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 0, category: 'test', id: 1}} }&lt;br /&gt;
      context 'when new topic can be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and redirects to assignment#edit page' do&lt;br /&gt;
          session[:user] = participant&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(true)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
  context 'when new topic cannot be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and renders sign_up_sheet#new page' do&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(false)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to render_template(:new)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
   context 'when topic can be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 120, category: 'test', id: 1}} }&lt;br /&gt;
      it 'updates the existing topic and redirects to sign_up_sheet#add_signup_topics_staggered page' do&lt;br /&gt;
        new_topic = build(:topic, topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1)&lt;br /&gt;
        allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(new_topic)&lt;br /&gt;
        post :create, params&lt;br /&gt;
        expect(response).to redirect_to('/sign_up_sheet/add_signup_topics_staggered?id=' + params[:id].to_s)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 describe '#destroy' do&lt;br /&gt;
    let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
    context 'when topic can be found' do&lt;br /&gt;
      it 'redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :destroy, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
   context 'when topic cannot be found' do&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :destroy, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be deleted.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#edit' do&lt;br /&gt;
    let(:params) { {id: 1} }&lt;br /&gt;
    it 'renders sign_up_sheet#edit page' do&lt;br /&gt;
      get :edit, params&lt;br /&gt;
      expect(response).to render_template(:edit)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#update' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :update, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be updated.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic can be found' do&lt;br /&gt;
      let(:params) do&lt;br /&gt;
        {&lt;br /&gt;
          id: 1, assignment_id: 1,&lt;br /&gt;
          topic: {topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1, micropayment: 0, description: 'test', link: 'test'}&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
      it 'updates current topic and redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :update, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#list' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when current assignment is intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#intelligent_topic_selection page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_filled).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_waitlisted).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(assignment_id: assignment.id, private_to: nil).and_return([topic])&lt;br /&gt;
        allow(assignment).to receive(:max_team_size).and_return(1)&lt;br /&gt;
        allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(true)&lt;br /&gt;
        allow(Bid).to receive(:where).with(team_id: team.try(:id)).and_return(bids)&lt;br /&gt;
        allow(bids).to receive(:order).with(:priority).and_return(bids)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_by).with(id: bid.topic_id).and_return(topic)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(7).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(6).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(1).and_return(due_date)&lt;br /&gt;
        allow(assignment).to receive(:staggered_deadline).and_return(true)&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(1, student.id).and_return([])&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:intelligent_topic_selection)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when current assignment is not intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(false)&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:list)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#sign_up' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when SignUpSheet.signup_team method return nil' do&lt;br /&gt;
      it 'shows an error flash message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
        get :sign_up, params, session&lt;br /&gt;
        expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You've already signed up for a topic!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#signup_as_instructor_action' do&lt;br /&gt;
    let(:params) { {username: '1'} }&lt;br /&gt;
    context 'when user cannot be found' do&lt;br /&gt;
      it 'shows an flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(User).to receive(:find_by).with(any_args).and_return(nil)&lt;br /&gt;
        get :signup_as_instructor_action, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;That student does not exist!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
     context 'when user can be found' do&lt;br /&gt;
      context 'when an assignment_participant can be found' do&lt;br /&gt;
        context 'when creating team related objects successfully' do&lt;br /&gt;
          it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(signed_up_team)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:success]).to eq(&amp;quot;You have successfully signed up the student for the topic!&amp;quot;)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
         context 'when creating team related objects unsuccessfully' do&lt;br /&gt;
          it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:error]).to eq(&amp;quot;The student has already signed up for a topic!&amp;quot;)&lt;br /&gt;
            expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
       context 'when an assignment_participant cannot be found' do&lt;br /&gt;
        it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
          allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
          allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
          get :signup_as_instructor_action&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student is not registered for the assignment!&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;You have already submitted your work, so you are not allowed to drop your topic.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop your topic after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: student} }&lt;br /&gt;
      it 'shows a flash success message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).and_return(signed_up_team2)&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        delete :delete_signup, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped your topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup_as_instructor' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup_as_instructor, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student has already submitted their work, so you are not allowed to remove them.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup_as_instructor, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop a student after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: instructor} }&lt;br /&gt;
      it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).with(any_args).with(no_args).and_return(signed_up_team2)&lt;br /&gt;
        delete :delete_signup_as_instructor, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped the student from the topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#set_priority' do&lt;br /&gt;
    let(:params) { {participant_id: '1', id: 1, topic: [1], assignment_id: 1} }&lt;br /&gt;
    let(:team_id) { participant.team.try(:id) }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    it 'sets priority of bidding topic and redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(AssignmentParticipant).to receive(:find_by).with(id: params[:participant_id]).and_return(participant)&lt;br /&gt;
      allow(SignUpTopic).to receive_message_chain(:find, :assignment).with(params[:topic].first).with(no_args).and_return(assignment)&lt;br /&gt;
      allow(Bid).to receive(:where).with(any_args).and_return(bids)&lt;br /&gt;
      allow(bid).to receive(:topic_id).and_return(1)&lt;br /&gt;
      allow(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      expect(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      get :set_priority, params&lt;br /&gt;
      expect(response).to redirect_to action: 'list', assignment_id: params[:assignment_id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#save_topic_deadlines' do&lt;br /&gt;
    let(:params) { {assignment_id: 1, due_date: {}} }&lt;br /&gt;
    let(:topics) { [topic] }&lt;br /&gt;
    context 'when topic_due_date cannot be found' do&lt;br /&gt;
      it 'creates a new topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return nil&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(TopicDueDate).to receive(:create).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic_due_date can be found' do&lt;br /&gt;
      it 'updates the existing topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return([due_date])&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(due_date).to receive(:update_attributes).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#show_team' do&lt;br /&gt;
    let(:params) { {id: '1', assignment_id: 1} }&lt;br /&gt;
    it 'renders show_team page' do&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([signed_up_team])&lt;br /&gt;
      get :show_team, params&lt;br /&gt;
      expect(response).to render_template(:show_team)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#switch_original_topic_to_approved_suggested_topic' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    it 'redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(TeamsUser).to receive(:team_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:topic_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignUpTopic).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([])&lt;br /&gt;
      get :switch_original_topic_to_approved_suggested_topic, params, session&lt;br /&gt;
      expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== '''Reference''' ==&lt;br /&gt;
[https://www.dropbox.com/s/nr3fhk3wbxt4qsr/录音_480p.mov?dl=0]video about Refactor&lt;br /&gt;
&lt;br /&gt;
[https://www.dropbox.com/s/l734csf16l4vghs/test_finish_480.MOV?dl=0]video about test&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111200</id>
		<title>CSC/ECE 517 Fall 2017/E1747 Refactor sign up sheet controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2017/E1747_Refactor_sign_up_sheet_controller.rb&amp;diff=111200"/>
		<updated>2017-11-02T14:31:28Z</updated>

		<summary type="html">&lt;p&gt;Qwang32: /* Problem Statement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Introduction of Expertiza''' ==&lt;br /&gt;
Expertiza is a web application where students can form their teams, submit their projects, peer-review other's work, view their grades and so on. Expertiza is open-source, maintained by students and faculty in NC State university.&lt;br /&gt;
&lt;br /&gt;
== '''About sign_up_sheet_controller.rb''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment.&lt;br /&gt;
&lt;br /&gt;
== '''Problem Statement''' ==&lt;br /&gt;
sign_up_sheet_controller.rb is the controller to handle topic assignment. sign_up_sheet_controller.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These method need to be broken down into simpler and more specific methods that are easier to read or understand. Also, the few instances of code duplication that exist should be removed.&lt;br /&gt;
&lt;br /&gt;
=='''Tasks'''==&lt;br /&gt;
&lt;br /&gt;
* Refactor &amp;quot;list&amp;quot;, &amp;quot;save_topic_deadlines&amp;quot; method&lt;br /&gt;
** 1.Split into several simpler methods and assign reasonable names &lt;br /&gt;
** 2.Extract duplicated code into separate methods&lt;br /&gt;
* Use find_by instead of dynamic method&lt;br /&gt;
* Complete the pending tests in sign_up_sheet_controller_spec.rb, and write integration tests for newly-created methods. Refactor corresponding methods, and only then finish pending tests.&lt;br /&gt;
&lt;br /&gt;
== '''Refactor''' ==&lt;br /&gt;
'''Refactor &amp;quot;list&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
The 'list' method is a long method which is fairly complex and hard to read. It is not following &amp;quot;good Ruby and Rails coding practices&amp;quot;.  In &amp;quot;list&amp;quot; method we found that &amp;quot;signed_up_topics&amp;quot; occurred too many times, here we split it into three different methods, named as &amp;quot;find_signed_up_topics&amp;quot; ,&amp;quot;find_selected_topics&amp;quot; and &amp;quot;list&amp;quot;. The changes are showed below.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority) &lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      @bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
      signed_up_topics &amp;amp;= @sign_up_topics&lt;br /&gt;
      @sign_up_topics -= signed_up_topics&lt;br /&gt;
      @bids = signed_up_topics&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
      # sign up again unless the former was a waitlisted topic&lt;br /&gt;
      # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
      users_team = SignedUpTeam.find_team_users(@assignment.id, session[:user].id)&lt;br /&gt;
      @selected_topics = if users_team.empty?&lt;br /&gt;
                           nil&lt;br /&gt;
                         else&lt;br /&gt;
                           # TODO: fix this; cant use 0&lt;br /&gt;
                           SignedUpTeam.find_user_signup_topics(@assignment.id, users_team[0].t_id)&lt;br /&gt;
                         end&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
 #Operating on the student bids, return signed up topics.&lt;br /&gt;
   def find_signed_up_topics student_bids&lt;br /&gt;
      signed_up_topics = []&lt;br /&gt;
      student_bids.each do |bid|&lt;br /&gt;
        sign_up_topic = SignUpTopic.find_by(id: bid.topic_id)&lt;br /&gt;
        signed_up_topics &amp;lt;&amp;lt; sign_up_topic if sign_up_topic&lt;br /&gt;
      end&lt;br /&gt;
     # signed_up_topics &amp;amp;= sign_up_topics&lt;br /&gt;
      return signed_up_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 #Operating on assignments, return selected topics.&lt;br /&gt;
   def find_selected_topics assignment&lt;br /&gt;
    # Find whether the user has signed up for any topics; if so the user won't be able to&lt;br /&gt;
    # sign up again unless the former was a waitlisted topic&lt;br /&gt;
    # if team assignment, then team id needs to be passed as parameter else the user's id&lt;br /&gt;
    users_team = SignedUpTeam.find_team_users(assignment.id, session[:user].id)&lt;br /&gt;
    selected_topics = if users_team.empty?&lt;br /&gt;
                         nil&lt;br /&gt;
                       else&lt;br /&gt;
                         # TODO: fix this; cant use 0&lt;br /&gt;
                         SignedUpTeam.find_user_signup_topics(assignment.id, users_team.first.t_id)&lt;br /&gt;
                       end&lt;br /&gt;
    return selected_topics&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   def list&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id].to_i)&lt;br /&gt;
    @assignment = @participant.assignment&lt;br /&gt;
    @slots_filled = SignUpTopic.find_slots_filled(@assignment.id)&lt;br /&gt;
    @slots_waitlisted = SignUpTopic.find_slots_waitlisted(@assignment.id)&lt;br /&gt;
    @show_actions = true&lt;br /&gt;
    @priority = 0&lt;br /&gt;
    @max_team_size = @assignment.max_team_size&lt;br /&gt;
    team_id = @participant.team.try(:id)&lt;br /&gt;
    @student_bids = team_id.nil? ? [] : Bid.where(team_id: team_id).order(:priority)&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      @bids = find_signed_up_topics @student_bids&lt;br /&gt;
      @sign_up_topics = @bids&lt;br /&gt;
    else&lt;br /&gt;
      @sign_up_topics = SignUpTopic.where(assignment_id: @assignment.id, private_to: nil)&lt;br /&gt;
    end&lt;br /&gt;
    @num_of_topics = @sign_up_topics.size&lt;br /&gt;
    @signup_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(7)&lt;br /&gt;
    @drop_topic_deadline = @assignment.due_dates.find_by_deadline_type_id(6)&lt;br /&gt;
    unless @assignment.due_dates.find_by_deadline_type_id(1).nil?&lt;br /&gt;
      if !@assignment.staggered_deadline? and @assignment.due_dates.find_by_deadline_type_id(1).due_at &amp;lt; Time.now&lt;br /&gt;
        @show_actions = false&lt;br /&gt;
      end&lt;br /&gt;
      @selected_topics = find_selected_topics @assignment&lt;br /&gt;
    end&lt;br /&gt;
    if @assignment.is_intelligent&lt;br /&gt;
      render 'sign_up_sheet/intelligent_topic_selection' and return&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
'''Refactor &amp;quot;save_topic_deadlines&amp;quot; method''' &lt;br /&gt;
&lt;br /&gt;
We found the &amp;quot;save_topic_deadlines&amp;quot; method is not hard to read, but it's a little bit long and some of the codes can be reused. Here The &amp;quot;save_topic_deadlines&amp;quot; method is modified and two new methods were created, named as &amp;quot;get_instance_variable_assignment&amp;quot; and &amp;quot;get_instance_variable_topic&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if instance_variable_get('@topic_' + deadline_type + '_due_date') == instance_variable_get('@assignment_' + deadline_type + '_due_date')&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].flag,&lt;br /&gt;
              threshold:                   instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].deadline_name,&lt;br /&gt;
              description_url:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      instance_variable_get('@topic_' + deadline_type + '_due_date'),&lt;br /&gt;
              submission_allowed_id:       instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  instance_variable_get('@assignment_' + deadline_type + '_due_dates')[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Modified Code:&lt;br /&gt;
   def save_topic_deadlines&lt;br /&gt;
    assignment = Assignment.find(params[:assignment_id])&lt;br /&gt;
    @assignment_submission_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 1 }&lt;br /&gt;
    @assignment_review_due_dates = assignment.due_dates.select {|due_date| due_date.deadline_type_id == 2 }&lt;br /&gt;
    due_dates = params[:due_date]&lt;br /&gt;
    topics = SignUpTopic.where(assignment_id: params[:assignment_id])&lt;br /&gt;
    review_rounds = assignment.num_review_rounds&lt;br /&gt;
    topics.each_with_index do |topic, index|&lt;br /&gt;
      for i in 1..review_rounds&lt;br /&gt;
        @topic_submission_due_date = due_dates[topics[index].id.to_s + '_submission_' + i.to_s + '_due_date']&lt;br /&gt;
        @topic_review_due_date = due_dates[topics[index].id.to_s + '_review_' + i.to_s + '_due_date']&lt;br /&gt;
        @assignment_submission_due_date = DateTime.parse(@assignment_submission_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        @assignment_review_due_date = DateTime.parse(@assignment_review_due_dates[i - 1].due_at.to_s).strftime(&amp;quot;%Y-%m-%d %H:%M&amp;quot;)&lt;br /&gt;
        %w(submission review).each do |deadline_type|&lt;br /&gt;
          deadline_type_id = DeadlineType.find_by_name(deadline_type).id&lt;br /&gt;
          next if get_instance_varibale_topic == get_instance_variable_assignment&lt;br /&gt;
          topic_due_date = TopicDueDate.where(parent_id: topic.id, deadline_type_id: deadline_type_id, round: i).first rescue nil&lt;br /&gt;
          if topic_due_date.nil? # create a new record&lt;br /&gt;
            TopicDueDate.create(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              deadline_type_id:            deadline_type_id,&lt;br /&gt;
              parent_id:                   topic.id,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              round:                       i,&lt;br /&gt;
              flag:                        get_instance_variable_assignment[i - 1].flag,&lt;br /&gt;
              threshold:                   get_instance_variable_assignment[i - 1].threshold,&lt;br /&gt;
              delayed_job_id:              get_instance_variable_assignment[i - 1].delayed_job_id,&lt;br /&gt;
              deadline_name:               get_instance_variable_assignment[i - 1].deadline_name,&lt;br /&gt;
              description_url:             get_instance_variable_assignment[i - 1].description_url,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id,&lt;br /&gt;
              type:                       'TopicDueDate'&lt;br /&gt;
            )&lt;br /&gt;
          else # update an existed record&lt;br /&gt;
            topic_due_date.update_attributes(&lt;br /&gt;
              due_at:                      get_instance_varibale_topic,&lt;br /&gt;
              submission_allowed_id:       get_instance_variable_assignment[i - 1].submission_allowed_id,&lt;br /&gt;
              review_allowed_id:           get_instance_variable_assignment[i - 1].review_allowed_id,&lt;br /&gt;
              review_of_review_allowed_id: get_instance_variable_assignment[i - 1].review_of_review_allowed_id,&lt;br /&gt;
              quiz_allowed_id:             get_instance_variable_assignment[i - 1].quiz_allowed_id,&lt;br /&gt;
              teammate_review_allowed_id:  get_instance_variable_assignment[i - 1].teammate_review_allowed_id&lt;br /&gt;
            )&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    redirect_to_assignment_edit(params[:assignment_id])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   #refactor duplicate code present in both create params&lt;br /&gt;
  def get_instance_variable_assignment&lt;br /&gt;
    instance_variable_get('@assignment_' + deadline_type + '_due_dates')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def get_instance_varibale_topic&lt;br /&gt;
    instance_variable_get('@topic_' + deadline_type + '_due_date')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== '''Rspec test''' ==&lt;br /&gt;
The most important two tests are about &amp;quot;list&amp;quot; method and &amp;quot;save_topic_deadlines&amp;quot; method. Here we wrote 27 examples of test and all of them are passed.&lt;br /&gt;
 describe SignUpSheetController do&lt;br /&gt;
  let(:assignment) { build(:assignment, id: 1, instructor_id: 6, due_dates: [due_date], microtask: true, staggered_deadline: true) }&lt;br /&gt;
  let(:instructor) { build(:instructor, id: 6) }&lt;br /&gt;
  let(:student) { build(:student, id: 8) }&lt;br /&gt;
  let(:participant) { build(:participant, id: 1, user_id: 6, assignment: assignment) }&lt;br /&gt;
  let(:topic) { build(:topic, id: 1) }&lt;br /&gt;
  let(:signed_up_team) { build(:signed_up_team, team: team, topic: topic) }&lt;br /&gt;
  let(:signed_up_team2) { build(:signed_up_team, team_id: 2, is_waitlisted: true) }&lt;br /&gt;
  let(:team) { build(:assignment_team, id: 1, assignment: assignment) }&lt;br /&gt;
  let(:due_date) { build(:assignment_due_date, deadline_type_id: 1) }&lt;br /&gt;
  let(:due_date2) { build(:assignment_due_date, deadline_type_id: 2) }&lt;br /&gt;
  let(:bid) { Bid.new(topic_id: 1, priority: 1) }&lt;br /&gt;
  let(:team_user) { build(:team_user) }&lt;br /&gt;
   before(:each) do&lt;br /&gt;
    allow(Assignment).to receive(:find).with('1').and_return(assignment)&lt;br /&gt;
    allow(Assignment).to receive(:find).with(1).and_return(assignment)&lt;br /&gt;
    stub_current_user(instructor, instructor.role.name, instructor.role)&lt;br /&gt;
    allow(SignUpTopic).to receive(:find).with('1').and_return(topic)&lt;br /&gt;
    allow(Participant).to receive(:find_by).with(id: '1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find).with(1).and_return(participant)&lt;br /&gt;
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: student.id, parent_id: 1).and_return(participant)&lt;br /&gt;
    allow(Team).to receive(:find).with('1').and_return(team)&lt;br /&gt;
    allow(TeamsUser).to receive(:find_by).with(team_id: 1).and_return(team_user)&lt;br /&gt;
    allow(team_user).to receive(:user).and_return(student)&lt;br /&gt;
    allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
    allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
    allow(participant.team).to receive(:hyperlinks).and_return([])&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#new' do&lt;br /&gt;
    it 'builds a new sign up topic and renders sign_up_sheet#new page' do&lt;br /&gt;
      params = {id: 1}&lt;br /&gt;
      get :new, params&lt;br /&gt;
      expect(response).to render_template(:new)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#create' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 0, category: 'test', id: 1}} }&lt;br /&gt;
      context 'when new topic can be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and redirects to assignment#edit page' do&lt;br /&gt;
          session[:user] = participant&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(true)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
  context 'when new topic cannot be saved successfully' do&lt;br /&gt;
        it 'sets up a new topic and renders sign_up_sheet#new page' do&lt;br /&gt;
          allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(nil)&lt;br /&gt;
          allow_any_instance_of(SignUpTopic).to receive(:save).and_return(false)&lt;br /&gt;
          post :create, params&lt;br /&gt;
          expect(response).to render_template(:new)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
   context 'when topic can be found' do&lt;br /&gt;
      let(:params) { {id: 1, topic: {topic_name: 'new topic', micropayment: 120, category: 'test', id: 1}} }&lt;br /&gt;
      it 'updates the existing topic and redirects to sign_up_sheet#add_signup_topics_staggered page' do&lt;br /&gt;
        new_topic = build(:topic, topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1)&lt;br /&gt;
        allow(SignUpTopic).to receive_message_chain(:where, :first).and_return(new_topic)&lt;br /&gt;
        post :create, params&lt;br /&gt;
        expect(response).to redirect_to('/sign_up_sheet/add_signup_topics_staggered?id=' + params[:id].to_s)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
 describe '#destroy' do&lt;br /&gt;
    let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
    context 'when topic can be found' do&lt;br /&gt;
      it 'redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :destroy, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
   context 'when topic cannot be found' do&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :destroy, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be deleted.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#edit' do&lt;br /&gt;
    let(:params) { {id: 1} }&lt;br /&gt;
    it 'renders sign_up_sheet#edit page' do&lt;br /&gt;
      get :edit, params&lt;br /&gt;
      expect(response).to render_template(:edit)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#update' do&lt;br /&gt;
    context 'when topic cannot be found' do&lt;br /&gt;
      let(:params) { {id: 1, assignment_id: 1} }&lt;br /&gt;
      it 'shows an error flash message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find).with('1').and_return(nil)&lt;br /&gt;
        get :update, params&lt;br /&gt;
        expect(flash[:error]).to eq(&amp;quot;The topic could not be updated.&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic can be found' do&lt;br /&gt;
      let(:params) do&lt;br /&gt;
        {&lt;br /&gt;
          id: 1, assignment_id: 1,&lt;br /&gt;
          topic: {topic_name: 'new topic', topic_identifier: '120', category: 'test', id: 1, micropayment: 0, description: 'test', link: 'test'}&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
      it 'updates current topic and redirects to assignment#edit page' do&lt;br /&gt;
        session[:user] = participant&lt;br /&gt;
        post :update, params&lt;br /&gt;
        expect(response).to redirect_to('/assignments/' + assignment.id.to_s + '/edit#tabs-5')&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#list' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when current assignment is intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#intelligent_topic_selection page' do&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_filled).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_slots_waitlisted).with(1).and_return(topic)&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(assignment_id: assignment.id, private_to: nil).and_return([topic])&lt;br /&gt;
        allow(assignment).to receive(:max_team_size).and_return(1)&lt;br /&gt;
        allow(participant).to receive(:team).and_return(team)&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(true)&lt;br /&gt;
        allow(Bid).to receive(:where).with(team_id: team.try(:id)).and_return(bids)&lt;br /&gt;
        allow(bids).to receive(:order).with(:priority).and_return(bids)&lt;br /&gt;
        allow(SignUpTopic).to receive(:find_by).with(id: bid.topic_id).and_return(topic)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(7).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(6).and_return(nil)&lt;br /&gt;
        allow(assignment.due_dates).to receive(:find_by_deadline_type_id).with(1).and_return(due_date)&lt;br /&gt;
        allow(assignment).to receive(:staggered_deadline).and_return(true)&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(1, student.id).and_return([])&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:intelligent_topic_selection)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when current assignment is not intelligent assignment and has submission duedate (deadline_type_id 1)' do&lt;br /&gt;
      it 'renders sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive(:is_intelligent).and_return(false)&lt;br /&gt;
        get :list, params, session&lt;br /&gt;
        expect(response).to render_template(:list)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#sign_up' do&lt;br /&gt;
    let(:params) { {id: '1'} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    context 'when SignUpSheet.signup_team method return nil' do&lt;br /&gt;
      it 'shows an error flash message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
        get :sign_up, params, session&lt;br /&gt;
        expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You've already signed up for a topic!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#signup_as_instructor_action' do&lt;br /&gt;
    let(:params) { {username: '1'} }&lt;br /&gt;
    context 'when user cannot be found' do&lt;br /&gt;
      it 'shows an flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(User).to receive(:find_by).with(any_args).and_return(nil)&lt;br /&gt;
        get :signup_as_instructor_action, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;That student does not exist!&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
     context 'when user can be found' do&lt;br /&gt;
      context 'when an assignment_participant can be found' do&lt;br /&gt;
        context 'when creating team related objects successfully' do&lt;br /&gt;
          it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(signed_up_team)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:success]).to eq(&amp;quot;You have successfully signed up the student for the topic!&amp;quot;)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
         context 'when creating team related objects unsuccessfully' do&lt;br /&gt;
          it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
            allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
            allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(true)&lt;br /&gt;
            allow(SignUpSheet).to receive(:signup_team).with(any_args).and_return(nil)&lt;br /&gt;
            get :signup_as_instructor_action&lt;br /&gt;
            expect(flash.now[:error]).to eq(&amp;quot;The student has already signed up for a topic!&amp;quot;)&lt;br /&gt;
            expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
       context 'when an assignment_participant cannot be found' do&lt;br /&gt;
        it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
          allow(User).to receive(:find_by).with(any_args).and_return(student)&lt;br /&gt;
          allow(AssignmentParticipant).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
          get :signup_as_instructor_action&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student is not registered for the assignment!&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;You have already submitted your work, so you are not allowed to drop your topic.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop your topic after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: student} }&lt;br /&gt;
      it 'shows a flash success message and redirects to sign_up_sheet#list page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).and_return(signed_up_team2)&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        delete :delete_signup, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped your topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to(action: 'list', id: params[:id])&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#delete_signup_as_instructor' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    context 'when either submitted files or hyperlinks of current team are not empty' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return(['file'])&lt;br /&gt;
        expect = proc do&lt;br /&gt;
          delete :delete_signup_as_instructor, params&lt;br /&gt;
          expect(flash.now[:error]).to eq(&amp;quot;The student has already submitted their work, so you are not allowed to remove them.&amp;quot;)&lt;br /&gt;
          expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
        end&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:hyperlinks).and_return(['link'])&lt;br /&gt;
        expect.call&lt;br /&gt;
        allow(participant.team).to receive(:submitted_files).and_return([])&lt;br /&gt;
        expect.call&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is not nil and its due date has already passed' do&lt;br /&gt;
      it 'shows a flash error message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(due_date).to receive(:due_at).and_return(Time.current - 1.day)&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return(due_date)&lt;br /&gt;
        delete :delete_signup_as_instructor, params&lt;br /&gt;
        expect(flash.now[:error]).to eq(&amp;quot;You cannot drop a student after the drop topic deadline!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when both submitted files and hyperlinks of current team are empty and drop topic deadline is nil' do&lt;br /&gt;
      let(:session) { {user: instructor} }&lt;br /&gt;
      it 'shows a flash success message and redirects to assignment#edit page' do&lt;br /&gt;
        allow(assignment).to receive_message_chain(:due_dates, :find_by_deadline_type_id).with(no_args).with(6).and_return nil&lt;br /&gt;
        allow(SignedUpTeam).to receive(:find_team_users).with(participant.assignment.id, session[:user].id).and_return([signed_up_team])&lt;br /&gt;
        allow(signed_up_team).to receive(:t_id).and_return(1)&lt;br /&gt;
        allow(SignedUpTeam).to receive_message_chain(:where, :first).with(any_args).with(no_args).and_return(signed_up_team2)&lt;br /&gt;
        delete :delete_signup_as_instructor, params, session&lt;br /&gt;
        expect(flash.now[:success]).to eq(&amp;quot;You have successfully dropped the student from the topic!&amp;quot;)&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: assignment.id&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#set_priority' do&lt;br /&gt;
    let(:params) { {participant_id: '1', id: 1, topic: [1], assignment_id: 1} }&lt;br /&gt;
    let(:team_id) { participant.team.try(:id) }&lt;br /&gt;
    let(:bids) { [bid] }&lt;br /&gt;
    it 'sets priority of bidding topic and redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(AssignmentParticipant).to receive(:find_by).with(id: params[:participant_id]).and_return(participant)&lt;br /&gt;
      allow(SignUpTopic).to receive_message_chain(:find, :assignment).with(params[:topic].first).with(no_args).and_return(assignment)&lt;br /&gt;
      allow(Bid).to receive(:where).with(any_args).and_return(bids)&lt;br /&gt;
      allow(bid).to receive(:topic_id).and_return(1)&lt;br /&gt;
      allow(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      expect(bids).to receive(:update_all).with(priority: Integer)&lt;br /&gt;
      get :set_priority, params&lt;br /&gt;
      expect(response).to redirect_to action: 'list', assignment_id: params[:assignment_id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#save_topic_deadlines' do&lt;br /&gt;
    let(:params) { {assignment_id: 1, due_date: {}} }&lt;br /&gt;
    let(:topics) { [topic] }&lt;br /&gt;
    context 'when topic_due_date cannot be found' do&lt;br /&gt;
      it 'creates a new topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return nil&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(TopicDueDate).to receive(:create).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
     context 'when topic_due_date can be found' do&lt;br /&gt;
      it 'updates the existing topic_due_date record and redirects to assignment#edit page' do&lt;br /&gt;
        allow(TopicDueDate).to receive(:where).with(any_args).and_return([due_date])&lt;br /&gt;
        allow(SignUpTopic).to receive(:where).with(any_args).and_return(topics)&lt;br /&gt;
        allow(assignment).to receive(:num_review_rounds).and_return(1)&lt;br /&gt;
        assignment.due_dates = assignment.due_dates.push(due_date2)&lt;br /&gt;
        allow(DeadlineType).to receive_message_chain(:find_by_name, :id).with(String).with(no_args).and_return(1)&lt;br /&gt;
        expect(due_date).to receive(:update_attributes).exactly(2).times.with(any_args)&lt;br /&gt;
        get :save_topic_deadlines, params&lt;br /&gt;
        expect(response).to redirect_to controller: 'assignments', action: 'edit', id: params[:assignment_id]&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#show_team' do&lt;br /&gt;
    let(:params) { {id: '1', assignment_id: 1} }&lt;br /&gt;
    it 'renders show_team page' do&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([signed_up_team])&lt;br /&gt;
      get :show_team, params&lt;br /&gt;
      expect(response).to render_template(:show_team)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
   describe '#switch_original_topic_to_approved_suggested_topic' do&lt;br /&gt;
    let(:params) { {id: 1, topic_id: 1} }&lt;br /&gt;
    let(:session) { {user: student} }&lt;br /&gt;
    it 'redirects to sign_up_sheet#list page' do&lt;br /&gt;
      allow(TeamsUser).to receive(:team_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:topic_id).with(any_args).and_return(1)&lt;br /&gt;
      allow(SignUpTopic).to receive(:exists?).with(any_args).and_return(false)&lt;br /&gt;
      allow(SignedUpTeam).to receive(:where).with(any_args).and_return([])&lt;br /&gt;
      get :switch_original_topic_to_approved_suggested_topic, params, session&lt;br /&gt;
      expect(response).to redirect_to action: 'list', id: params[:id]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== '''Reference''' ==&lt;br /&gt;
[https://www.dropbox.com/s/nr3fhk3wbxt4qsr/录音_480p.mov?dl=0]video about Refactor&lt;br /&gt;
&lt;br /&gt;
[https://www.dropbox.com/s/l734csf16l4vghs/test_finish_480.MOV?dl=0]video about test&lt;/div&gt;</summary>
		<author><name>Qwang32</name></author>
	</entry>
</feed>