<?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=Skunapa</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=Skunapa"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Skunapa"/>
	<updated>2026-05-17T01:01:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102389</id>
		<title>CSC/ECE 517 Spring 2016/Write automated tests for WebDriver</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102389"/>
		<updated>2016-04-28T04:01:34Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Design pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{student editor|course = [[Wikipedia:Wiki_Ed/North_Carolina_State_University/Object-Oriented_Design_and_Development_(Spring_2016)]] }}&lt;br /&gt;
&lt;br /&gt;
== Write automated tests for the WebDriver server ==&lt;br /&gt;
&lt;br /&gt;
ServoServo is an open source prototype web browser layout engine being developed by Mozilla, and it is written in Rust language. It is currently developed on 64bit OS X, 64bit Linux, Android, and Gonk (Firefox OS). The main idea is to create a highly parallel environment, in which different components can be handled by fine grained, isolated tasks. The different components can be rendering, HTML parsing, etc.&lt;br /&gt;
&lt;br /&gt;
== Background information: ==&lt;br /&gt;
&lt;br /&gt;
=== Python: ===&lt;br /&gt;
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Python supports multiple programming paradigms, including object oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library. Modules for creating graphical user interfaces, connecting to relational databases, pseudorandom number generators, arithmetic with arbitrary precision decimals,[77] manipulating regular expressions, and doing unit testing are also included.&lt;br /&gt;
&lt;br /&gt;
=== Servo: ===&lt;br /&gt;
Servo is a project to develop a new Web browser engine to help mitigate vulnerabilities associated with incorrect memory management and data races. Servo is written in Rust, a new language designed specifically with Servo's requirements in mind. Rust provides a task-parallel infrastructure and a strong type system that enforces memory safety and data race freedom.&lt;br /&gt;
&lt;br /&gt;
Servo is focused both on supporting a full Web browser, through the use of the purely HTML-based user interface Browser.html and on creating a solid, embeddable engine. Although Servo was originally a research project, it was implemented with the goal of having production-quality code and is in the process of shipping several of its components in the Firefox browser.&lt;br /&gt;
&lt;br /&gt;
[[File:ServoArchitecture.JPG]]&lt;br /&gt;
&lt;br /&gt;
The above diagram denotes several of the tasks that are associated with building the Servo web browser. Each of the boxes represents a task that is implemented in Rust and the lines indicates relationships between the tasks.&lt;br /&gt;
&lt;br /&gt;
=== WebDriver: ===&lt;br /&gt;
The WebDriver specification defines a communication protocol for automating interactions with a web browser that would traditionally require a human being (eg. clicking the mouse on a particular link; typing text into an input field; etc.) This protocol is being implemented by major browsers such as Firefox, Edge, and Chrome, and Servo also provides a WebDriver server. The goal of this project is both to submit a broad range of automated client tests using the Python WebDriver API that can be run against multiple browsers, and also to report on how many of the tests Servo currently passes.&lt;br /&gt;
&lt;br /&gt;
== Scope ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
* Compile Servo and ensure that it runs on &amp;lt;code&amp;gt;tests/html/about-mozilla.html&amp;lt;/code&amp;gt;&lt;br /&gt;
* To run a simple webdriver script using the python client against Servo.&lt;br /&gt;
* Create a&amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; that will import each python file in the directory and execute the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it by invoking the Servo process before running each test, and killing it after each test finishes.&lt;br /&gt;
* Create a &amp;lt;code&amp;gt;webdriver&amp;lt;/code&amp;gt; directory under &amp;lt;code&amp;gt;tests&amp;lt;/code&amp;gt; where the forthcoming tests will reside.&lt;br /&gt;
* Writing an automated test for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
* Writing tests that exercise the lower-level command API (eg. self.send_command(&amp;quot;POST&amp;quot;, &amp;quot;url&amp;quot;, body)) to test server capabilities:&lt;br /&gt;
* Writing a test with a simple get to a url that exists on the server, ensure that we get the right response type&lt;br /&gt;
* Sending a non-string as the URL and ensuring that we get the right kind of error message.&lt;br /&gt;
* Trying other kinds of non-conforming messages e.g. ones with extra fields (should be ignored) or no url field (should return the correct type of error)&lt;br /&gt;
* Testing that we are actually loading the correct page. This will require the use of some other webdriver functions e.g. to get an element's text, or take a screenshot&lt;br /&gt;
* Testing that Get always navigates the top level browsing context (by navigating to a page with an iframe, setting the current bc to that iframe, and ensuring that it is not just the iframe that is navigated).&lt;br /&gt;
* Trying Get with pages that return non-200 responses.&lt;br /&gt;
* Loading a page that loads slower than the timeout, and ensure we get the correct error message&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
Design patterns are not applicable as our task involved writing automated tests in python. However, the Implementation section below provides details of the steps the way it was implemented.&lt;br /&gt;
&lt;br /&gt;
We have implemented the test cases based on the input from the Mozilla team and there were several major tasks that were implemented.&lt;br /&gt;
&lt;br /&gt;
1. Execute tests on a standalone HTTP server: In this we wanted to test the functionality if a web page opens as expected on the wed driver engine. We have implemented this by giving a URL which opens a web page whose actual URL is different when the page opens. We matched the web page expected with the one that is opened to successfully execute the test case.&lt;br /&gt;
&lt;br /&gt;
2.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
The following steps were followed to meet the project requirements as per this github page.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: ===&lt;br /&gt;
We created a new &amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; file. It imports each python file in the directory and executes the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it. &lt;br /&gt;
&lt;br /&gt;
=== Step 2: ===&lt;br /&gt;
&lt;br /&gt;
We created tests in test.py and test1.py for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
===Step 1:===&lt;br /&gt;
We would create 3 different test.py files for each of the different tests that would reside in tests/webdriver folder.&lt;br /&gt;
&lt;br /&gt;
===Step 2:===&lt;br /&gt;
Creating a ServoProcess that would be used by the test.py files to start a servo session.&lt;br /&gt;
&lt;br /&gt;
===Step 3:===&lt;br /&gt;
Catching exceptions when the tests fail and displaying them in proper format based on the kind of different error.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
Following are the steps to run the automated tests for WebDriver:&lt;br /&gt;
 git clone &amp;lt;nowiki&amp;gt;https://github.com/krunal3103/servo&amp;lt;/nowiki&amp;gt; &lt;br /&gt;
Note: Follow the steps listed in the Readme.md file of this link to clone and build servo on your local machine.&lt;br /&gt;
 cd servo &lt;br /&gt;
&lt;br /&gt;
 ./mach test-webdriver &lt;br /&gt;
You will see that all tests pass as expected.&lt;br /&gt;
&lt;br /&gt;
== Pull Request ==&lt;br /&gt;
Here is our [https://github.com/servo/servo/pull/10113 pull request]. In the link you can see all code snippets and commits as well as integration test progression information.&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102387</id>
		<title>CSC/ECE 517 Spring 2016/Write automated tests for WebDriver</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102387"/>
		<updated>2016-04-28T00:58:43Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Servo: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{student editor|course = [[Wikipedia:Wiki_Ed/North_Carolina_State_University/Object-Oriented_Design_and_Development_(Spring_2016)]] }}&lt;br /&gt;
&lt;br /&gt;
== Write automated tests for the WebDriver server ==&lt;br /&gt;
&lt;br /&gt;
ServoServo is an open source prototype web browser layout engine being developed by Mozilla, and it is written in Rust language. It is currently developed on 64bit OS X, 64bit Linux, Android, and Gonk (Firefox OS). The main idea is to create a highly parallel environment, in which different components can be handled by fine grained, isolated tasks. The different components can be rendering, HTML parsing, etc.&lt;br /&gt;
&lt;br /&gt;
== Background information: ==&lt;br /&gt;
&lt;br /&gt;
=== Python: ===&lt;br /&gt;
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Python supports multiple programming paradigms, including object oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library. Modules for creating graphical user interfaces, connecting to relational databases, pseudorandom number generators, arithmetic with arbitrary precision decimals,[77] manipulating regular expressions, and doing unit testing are also included.&lt;br /&gt;
&lt;br /&gt;
=== Servo: ===&lt;br /&gt;
Servo is a project to develop a new Web browser engine to help mitigate vulnerabilities associated with incorrect memory management and data races. Servo is written in Rust, a new language designed specifically with Servo's requirements in mind. Rust provides a task-parallel infrastructure and a strong type system that enforces memory safety and data race freedom.&lt;br /&gt;
&lt;br /&gt;
Servo is focused both on supporting a full Web browser, through the use of the purely HTML-based user interface Browser.html and on creating a solid, embeddable engine. Although Servo was originally a research project, it was implemented with the goal of having production-quality code and is in the process of shipping several of its components in the Firefox browser.&lt;br /&gt;
&lt;br /&gt;
[[File:ServoArchitecture.JPG]]&lt;br /&gt;
&lt;br /&gt;
The above diagram denotes several of the tasks that are associated with building the Servo web browser. Each of the boxes represents a task that is implemented in Rust and the lines indicates relationships between the tasks.&lt;br /&gt;
&lt;br /&gt;
=== WebDriver: ===&lt;br /&gt;
The WebDriver specification defines a communication protocol for automating interactions with a web browser that would traditionally require a human being (eg. clicking the mouse on a particular link; typing text into an input field; etc.) This protocol is being implemented by major browsers such as Firefox, Edge, and Chrome, and Servo also provides a WebDriver server. The goal of this project is both to submit a broad range of automated client tests using the Python WebDriver API that can be run against multiple browsers, and also to report on how many of the tests Servo currently passes.&lt;br /&gt;
&lt;br /&gt;
== Scope ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
* Compile Servo and ensure that it runs on &amp;lt;code&amp;gt;tests/html/about-mozilla.html&amp;lt;/code&amp;gt;&lt;br /&gt;
* To run a simple webdriver script using the python client against Servo.&lt;br /&gt;
* Create a&amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; that will import each python file in the directory and execute the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it by invoking the Servo process before running each test, and killing it after each test finishes.&lt;br /&gt;
* Create a &amp;lt;code&amp;gt;webdriver&amp;lt;/code&amp;gt; directory under &amp;lt;code&amp;gt;tests&amp;lt;/code&amp;gt; where the forthcoming tests will reside.&lt;br /&gt;
* Writing an automated test for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
* Writing tests that exercise the lower-level command API (eg. self.send_command(&amp;quot;POST&amp;quot;, &amp;quot;url&amp;quot;, body)) to test server capabilities:&lt;br /&gt;
* Writing a test with a simple get to a url that exists on the server, ensure that we get the right response type&lt;br /&gt;
* Sending a non-string as the URL and ensuring that we get the right kind of error message.&lt;br /&gt;
* Trying other kinds of non-conforming messages e.g. ones with extra fields (should be ignored) or no url field (should return the correct type of error)&lt;br /&gt;
* Testing that we are actually loading the correct page. This will require the use of some other webdriver functions e.g. to get an element's text, or take a screenshot&lt;br /&gt;
* Testing that Get always navigates the top level browsing context (by navigating to a page with an iframe, setting the current bc to that iframe, and ensuring that it is not just the iframe that is navigated).&lt;br /&gt;
* Trying Get with pages that return non-200 responses.&lt;br /&gt;
* Loading a page that loads slower than the timeout, and ensure we get the correct error message&lt;br /&gt;
&lt;br /&gt;
== Design pattern ==&lt;br /&gt;
Design patterns are not applicable as our task involved writing automated tests in python. However, the Implementation section below provides details of the steps the way it was implemented.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
The following steps were followed to meet the project requirements as per this github page.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: ===&lt;br /&gt;
We created a new &amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; file. It imports each python file in the directory and executes the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it. &lt;br /&gt;
&lt;br /&gt;
=== Step 2: ===&lt;br /&gt;
&lt;br /&gt;
We created tests in test.py and test1.py for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
===Step 1:===&lt;br /&gt;
We would create 3 different test.py files for each of the different tests that would reside in tests/webdriver folder.&lt;br /&gt;
&lt;br /&gt;
===Step 2:===&lt;br /&gt;
Creating a ServoProcess that would be used by the test.py files to start a servo session.&lt;br /&gt;
&lt;br /&gt;
===Step 3:===&lt;br /&gt;
Catching exceptions when the tests fail and displaying them in proper format based on the kind of different error.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
Following are the steps to run the automated tests for WebDriver:&lt;br /&gt;
 git clone &amp;lt;nowiki&amp;gt;https://github.com/krunal3103/servo&amp;lt;/nowiki&amp;gt; &lt;br /&gt;
Note: Follow the steps listed in the Readme.md file of this link to clone and build servo on your local machine.&lt;br /&gt;
 cd servo &lt;br /&gt;
&lt;br /&gt;
 ./mach test-webdriver &lt;br /&gt;
You will see that all tests pass as expected.&lt;br /&gt;
&lt;br /&gt;
== Pull Request ==&lt;br /&gt;
Here is our [https://github.com/servo/servo/pull/10113 pull request]. In the link you can see all code snippets and commits as well as integration test progression information.&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102386</id>
		<title>CSC/ECE 517 Spring 2016/Write automated tests for WebDriver</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102386"/>
		<updated>2016-04-28T00:54:17Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Servo: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{student editor|course = [[Wikipedia:Wiki_Ed/North_Carolina_State_University/Object-Oriented_Design_and_Development_(Spring_2016)]] }}&lt;br /&gt;
&lt;br /&gt;
== Write automated tests for the WebDriver server ==&lt;br /&gt;
&lt;br /&gt;
ServoServo is an open source prototype web browser layout engine being developed by Mozilla, and it is written in Rust language. It is currently developed on 64bit OS X, 64bit Linux, Android, and Gonk (Firefox OS). The main idea is to create a highly parallel environment, in which different components can be handled by fine grained, isolated tasks. The different components can be rendering, HTML parsing, etc.&lt;br /&gt;
&lt;br /&gt;
== Background information: ==&lt;br /&gt;
&lt;br /&gt;
=== Python: ===&lt;br /&gt;
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Python supports multiple programming paradigms, including object oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library. Modules for creating graphical user interfaces, connecting to relational databases, pseudorandom number generators, arithmetic with arbitrary precision decimals,[77] manipulating regular expressions, and doing unit testing are also included.&lt;br /&gt;
&lt;br /&gt;
=== Servo: ===&lt;br /&gt;
[[File:ServoArchitecture.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== WebDriver: ===&lt;br /&gt;
The WebDriver specification defines a communication protocol for automating interactions with a web browser that would traditionally require a human being (eg. clicking the mouse on a particular link; typing text into an input field; etc.) This protocol is being implemented by major browsers such as Firefox, Edge, and Chrome, and Servo also provides a WebDriver server. The goal of this project is both to submit a broad range of automated client tests using the Python WebDriver API that can be run against multiple browsers, and also to report on how many of the tests Servo currently passes.&lt;br /&gt;
&lt;br /&gt;
== Scope ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
* Compile Servo and ensure that it runs on &amp;lt;code&amp;gt;tests/html/about-mozilla.html&amp;lt;/code&amp;gt;&lt;br /&gt;
* To run a simple webdriver script using the python client against Servo.&lt;br /&gt;
* Create a&amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; that will import each python file in the directory and execute the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it by invoking the Servo process before running each test, and killing it after each test finishes.&lt;br /&gt;
* Create a &amp;lt;code&amp;gt;webdriver&amp;lt;/code&amp;gt; directory under &amp;lt;code&amp;gt;tests&amp;lt;/code&amp;gt; where the forthcoming tests will reside.&lt;br /&gt;
* Writing an automated test for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
* Writing tests that exercise the lower-level command API (eg. self.send_command(&amp;quot;POST&amp;quot;, &amp;quot;url&amp;quot;, body)) to test server capabilities:&lt;br /&gt;
* Writing a test with a simple get to a url that exists on the server, ensure that we get the right response type&lt;br /&gt;
* Sending a non-string as the URL and ensuring that we get the right kind of error message.&lt;br /&gt;
* Trying other kinds of non-conforming messages e.g. ones with extra fields (should be ignored) or no url field (should return the correct type of error)&lt;br /&gt;
* Testing that we are actually loading the correct page. This will require the use of some other webdriver functions e.g. to get an element's text, or take a screenshot&lt;br /&gt;
* Testing that Get always navigates the top level browsing context (by navigating to a page with an iframe, setting the current bc to that iframe, and ensuring that it is not just the iframe that is navigated).&lt;br /&gt;
* Trying Get with pages that return non-200 responses.&lt;br /&gt;
* Loading a page that loads slower than the timeout, and ensure we get the correct error message&lt;br /&gt;
&lt;br /&gt;
== Design pattern ==&lt;br /&gt;
Design patterns are not applicable as our task involved writing automated tests in python. However, the Implementation section below provides details of the steps the way it was implemented.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
The following steps were followed to meet the project requirements as per this github page.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: ===&lt;br /&gt;
We created a new &amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; file. It imports each python file in the directory and executes the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it. &lt;br /&gt;
&lt;br /&gt;
=== Step 2: ===&lt;br /&gt;
&lt;br /&gt;
We created tests in test.py and test1.py for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
===Step 1:===&lt;br /&gt;
We would create 3 different test.py files for each of the different tests that would reside in tests/webdriver folder.&lt;br /&gt;
&lt;br /&gt;
===Step 2:===&lt;br /&gt;
Creating a ServoProcess that would be used by the test.py files to start a servo session.&lt;br /&gt;
&lt;br /&gt;
===Step 3:===&lt;br /&gt;
Catching exceptions when the tests fail and displaying them in proper format based on the kind of different error.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
Following are the steps to run the automated tests for WebDriver:&lt;br /&gt;
 git clone &amp;lt;nowiki&amp;gt;https://github.com/krunal3103/servo&amp;lt;/nowiki&amp;gt; &lt;br /&gt;
Note: Follow the steps listed in the Readme.md file of this link to clone and build servo on your local machine.&lt;br /&gt;
 cd servo &lt;br /&gt;
&lt;br /&gt;
 ./mach test-webdriver &lt;br /&gt;
You will see that all tests pass as expected.&lt;br /&gt;
&lt;br /&gt;
== Pull Request ==&lt;br /&gt;
Here is our [https://github.com/servo/servo/pull/10113 pull request]. In the link you can see all code snippets and commits as well as integration test progression information.&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102385</id>
		<title>CSC/ECE 517 Spring 2016/Write automated tests for WebDriver</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Write_automated_tests_for_WebDriver&amp;diff=102385"/>
		<updated>2016-04-28T00:54:01Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Background information: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{student editor|course = [[Wikipedia:Wiki_Ed/North_Carolina_State_University/Object-Oriented_Design_and_Development_(Spring_2016)]] }}&lt;br /&gt;
&lt;br /&gt;
== Write automated tests for the WebDriver server ==&lt;br /&gt;
&lt;br /&gt;
ServoServo is an open source prototype web browser layout engine being developed by Mozilla, and it is written in Rust language. It is currently developed on 64bit OS X, 64bit Linux, Android, and Gonk (Firefox OS). The main idea is to create a highly parallel environment, in which different components can be handled by fine grained, isolated tasks. The different components can be rendering, HTML parsing, etc.&lt;br /&gt;
&lt;br /&gt;
== Background information: ==&lt;br /&gt;
&lt;br /&gt;
=== Python: ===&lt;br /&gt;
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Python supports multiple programming paradigms, including object oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library. Modules for creating graphical user interfaces, connecting to relational databases, pseudorandom number generators, arithmetic with arbitrary precision decimals,[77] manipulating regular expressions, and doing unit testing are also included.&lt;br /&gt;
&lt;br /&gt;
=== Servo: ===&lt;br /&gt;
[[File:ServoArchitecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
=== WebDriver: ===&lt;br /&gt;
The WebDriver specification defines a communication protocol for automating interactions with a web browser that would traditionally require a human being (eg. clicking the mouse on a particular link; typing text into an input field; etc.) This protocol is being implemented by major browsers such as Firefox, Edge, and Chrome, and Servo also provides a WebDriver server. The goal of this project is both to submit a broad range of automated client tests using the Python WebDriver API that can be run against multiple browsers, and also to report on how many of the tests Servo currently passes.&lt;br /&gt;
&lt;br /&gt;
== Scope ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
* Compile Servo and ensure that it runs on &amp;lt;code&amp;gt;tests/html/about-mozilla.html&amp;lt;/code&amp;gt;&lt;br /&gt;
* To run a simple webdriver script using the python client against Servo.&lt;br /&gt;
* Create a&amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; that will import each python file in the directory and execute the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it by invoking the Servo process before running each test, and killing it after each test finishes.&lt;br /&gt;
* Create a &amp;lt;code&amp;gt;webdriver&amp;lt;/code&amp;gt; directory under &amp;lt;code&amp;gt;tests&amp;lt;/code&amp;gt; where the forthcoming tests will reside.&lt;br /&gt;
* Writing an automated test for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
* Writing tests that exercise the lower-level command API (eg. self.send_command(&amp;quot;POST&amp;quot;, &amp;quot;url&amp;quot;, body)) to test server capabilities:&lt;br /&gt;
* Writing a test with a simple get to a url that exists on the server, ensure that we get the right response type&lt;br /&gt;
* Sending a non-string as the URL and ensuring that we get the right kind of error message.&lt;br /&gt;
* Trying other kinds of non-conforming messages e.g. ones with extra fields (should be ignored) or no url field (should return the correct type of error)&lt;br /&gt;
* Testing that we are actually loading the correct page. This will require the use of some other webdriver functions e.g. to get an element's text, or take a screenshot&lt;br /&gt;
* Testing that Get always navigates the top level browsing context (by navigating to a page with an iframe, setting the current bc to that iframe, and ensuring that it is not just the iframe that is navigated).&lt;br /&gt;
* Trying Get with pages that return non-200 responses.&lt;br /&gt;
* Loading a page that loads slower than the timeout, and ensure we get the correct error message&lt;br /&gt;
&lt;br /&gt;
== Design pattern ==&lt;br /&gt;
Design patterns are not applicable as our task involved writing automated tests in python. However, the Implementation section below provides details of the steps the way it was implemented.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
===Initial Steps===&lt;br /&gt;
The following steps were followed to meet the project requirements as per this github page.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: ===&lt;br /&gt;
We created a new &amp;lt;code&amp;gt;mach&amp;lt;/code&amp;gt; command (&amp;lt;code&amp;gt;test-webdriver&amp;lt;/code&amp;gt;) in &amp;lt;code&amp;gt;python/servo/testing_commands.py&amp;lt;/code&amp;gt; file. It imports each python file in the directory and executes the &amp;lt;code&amp;gt;test&amp;lt;/code&amp;gt; method contained within it. &lt;br /&gt;
&lt;br /&gt;
=== Step 2: ===&lt;br /&gt;
&lt;br /&gt;
We created tests in test.py and test1.py for loading a particular URL, asserting that the resulting page is the expected url.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Subsequent Steps===&lt;br /&gt;
===Step 1:===&lt;br /&gt;
We would create 3 different test.py files for each of the different tests that would reside in tests/webdriver folder.&lt;br /&gt;
&lt;br /&gt;
===Step 2:===&lt;br /&gt;
Creating a ServoProcess that would be used by the test.py files to start a servo session.&lt;br /&gt;
&lt;br /&gt;
===Step 3:===&lt;br /&gt;
Catching exceptions when the tests fail and displaying them in proper format based on the kind of different error.&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
Following are the steps to run the automated tests for WebDriver:&lt;br /&gt;
 git clone &amp;lt;nowiki&amp;gt;https://github.com/krunal3103/servo&amp;lt;/nowiki&amp;gt; &lt;br /&gt;
Note: Follow the steps listed in the Readme.md file of this link to clone and build servo on your local machine.&lt;br /&gt;
 cd servo &lt;br /&gt;
&lt;br /&gt;
 ./mach test-webdriver &lt;br /&gt;
You will see that all tests pass as expected.&lt;br /&gt;
&lt;br /&gt;
== Pull Request ==&lt;br /&gt;
Here is our [https://github.com/servo/servo/pull/10113 pull request]. In the link you can see all code snippets and commits as well as integration test progression information.&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:ServoArchitecture.JPG&amp;diff=102384</id>
		<title>File:ServoArchitecture.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:ServoArchitecture.JPG&amp;diff=102384"/>
		<updated>2016-04-28T00:50:45Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101600</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101600"/>
		<updated>2016-04-01T22:10:57Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101599</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101599"/>
		<updated>2016-04-01T22:10:05Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;E1615. Refactoring the Review Mapping Controller&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101598</id>
		<title>Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101598"/>
		<updated>2016-04-01T22:07:49Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101597</id>
		<title>Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101597"/>
		<updated>2016-04-01T22:07:49Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101596</id>
		<title>/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101596"/>
		<updated>2016-04-01T22:07:49Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved /CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101595</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101595"/>
		<updated>2016-04-01T22:07:49Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved /CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101594</id>
		<title>Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101594"/>
		<updated>2016-04-01T22:07:35Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101593</id>
		<title>Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101593"/>
		<updated>2016-04-01T22:07:35Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to Talk:/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101592</id>
		<title>Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101592"/>
		<updated>2016-04-01T22:07:34Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to /CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101591</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101591"/>
		<updated>2016-04-01T22:07:34Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb to /CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User_talk:Skunapa&amp;diff=101590</id>
		<title>User talk:Skunapa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User_talk:Skunapa&amp;diff=101590"/>
		<updated>2016-04-01T22:07:08Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved User talk:Skunapa to Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101589</id>
		<title>Talk:CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101589"/>
		<updated>2016-04-01T22:07:08Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved User talk:Skunapa to Talk:Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Skunapa&amp;diff=101588</id>
		<title>User:Skunapa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Skunapa&amp;diff=101588"/>
		<updated>2016-04-01T22:07:08Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved User:Skunapa to Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb]]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101587</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101587"/>
		<updated>2016-04-01T22:07:08Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: moved User:Skunapa to Http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101578</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101578"/>
		<updated>2016-04-01T17:16:35Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=='''Testing UI'''==&lt;br /&gt;
&lt;br /&gt;
'''Peer review information:'''&lt;br /&gt;
* Instructor Login: &lt;br /&gt;
        Username: Instructor6&lt;br /&gt;
        Password: password&lt;br /&gt;
* Student login:&lt;br /&gt;
        Username: Student6384&lt;br /&gt;
        Password: password&lt;br /&gt;
&lt;br /&gt;
'''Steps for testing UI:'''&lt;br /&gt;
# Login as an instructor (Using Instructor6 will help you import the participants from other assignments).&lt;br /&gt;
# Navigate to &amp;quot;Manage-&amp;gt;Assignments&amp;quot;.&lt;br /&gt;
# Click on &amp;quot;New Public Assignment&amp;quot; for creating a new assignment.&lt;br /&gt;
# Create a new assignment by providing assignment name, selecting a course, submission directory (Give any name) and description URL.&lt;br /&gt;
# Select &amp;quot;has teams?&amp;quot; and provide the team size. Click on create to create a new assignment.&lt;br /&gt;
## After that, click on review strategy and limit the number of reviews per submission.&lt;br /&gt;
## Click on &amp;quot;Due dates&amp;quot; and update date for submission and review. Adjust the review date and time in order to test the reviews.&lt;br /&gt;
## Click on &amp;quot;save&amp;quot;. A new assignment is created and it can be viewed in &amp;quot;Manage-&amp;gt;Assignments&amp;quot; section.&lt;br /&gt;
# In order to add participants, there are two methods to add students to the assignment.&lt;br /&gt;
## Click on &amp;quot;add participants&amp;quot; against the assignment. Enter the user login to add the student. Add atleast 6 participants so that the review mapping can be seen.&lt;br /&gt;
## Click on any previous assignment &amp;quot;add participants&amp;quot; and export the students list (I used wiki assignment).&lt;br /&gt;
## Click on your assignment &amp;quot;add participants&amp;quot; and import the students using the export file.&lt;br /&gt;
# Go back to &amp;quot;Assignments&amp;quot; section and click against &amp;quot;create teams&amp;quot;.&lt;br /&gt;
## After clicking on &amp;quot;create teams&amp;quot;, Click on &amp;quot;create teams&amp;quot; in the directed page.&lt;br /&gt;
### Teams can be formed either manually or automatically.&lt;br /&gt;
# Login as a student to submit the links for assignment (Valid hyperlink must be provided in the submission link). Add submission for atleast 5 students in order to check the automatic review mapping. (Password for student is password)&lt;br /&gt;
# After submitting the links for some students, Log in as an instructor to change the assignment phase to review phase.&lt;br /&gt;
## To change the review phase period, Go to assignments section and click on edit. Click on due dates and change the review due date.&lt;br /&gt;
# Now, login as a student and go to others work. Click on &amp;quot;Request a submission to review&amp;quot; and check whether a review is assigned automatically. If no assignments are submitted, then this cannot be tested.&lt;br /&gt;
# For teammate review report, author feedback report and review report, click against &amp;quot;review report&amp;quot; and all the review reports can be seen by selecting it in the drop down menu.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101540</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101540"/>
		<updated>2016-03-30T20:29:15Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101539</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101539"/>
		<updated>2016-03-30T20:28:39Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Expertiza '''==&lt;br /&gt;
&lt;br /&gt;
The Expertiza is a software project to create reusable learning objects through peer review. It is a Ruby on Rails based application which can be run on Windows, Linux and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
Main Features of Expertiza are:&lt;br /&gt;
* Allows students to work in groups to improve others learning experiences&lt;br /&gt;
* The work done by students/team is subjeted to multiple reviews , minimizing plagiarism&lt;br /&gt;
* Reviewing is done by studnets, which allows instructor/TA to spend less time in grading &lt;br /&gt;
* Large classes can also be handled easily with the help of Expertiza&lt;br /&gt;
&lt;br /&gt;
=='''About Review mapping controller'''==&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Problem Statement'''===&lt;br /&gt;
&lt;br /&gt;
The main aim of this project is to&lt;br /&gt;
&lt;br /&gt;
* Refactor reporting method)(response_report) which is big &lt;br /&gt;
* Use more efficient function sample instead of shuffle for random number selection&lt;br /&gt;
* Remove unused and assigned variables &lt;br /&gt;
* Simplify Automatic_review_mapping_strategy&lt;br /&gt;
&lt;br /&gt;
=='''Code Improvements'''==&lt;br /&gt;
&lt;br /&gt;
==='''Unused variables and arguments'''===&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replace the unused variable with &amp;quot;_&amp;quot; to represent it as unused variable but allow them in arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
==='''Use sample instead of shuffle'''===&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Cyclomatic complexity of automatic_review_mapping_strategy method'''===&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101327</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101327"/>
		<updated>2016-03-24T03:33:39Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the code that is moved out of automatic_review_mapping_strategy as peer_review_strategy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
private&lt;br /&gt;
	def peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
		iterator = 0&lt;br /&gt;
		teams.each do |team|&lt;br /&gt;
		  selected_participants = Array.new&lt;br /&gt;
		  if !team.equal? teams.last&lt;br /&gt;
			#need to even out the # of reviews for teams&lt;br /&gt;
			while selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
			  num_participants_this_team = TeamsUser.where(team_id: team.id).size&lt;br /&gt;
			  #If there are some submitters or reviewers in this team, they are not treated as normal participants.&lt;br /&gt;
			  #They should be removed from 'num_participants_this_team'&lt;br /&gt;
			  TeamsUser.where(team_id: team.id).each do |team_user|&lt;br /&gt;
				temp_participant = Participant.where(user_id: team_user.user_id, parent_id: assignment_id).first&lt;br /&gt;
				num_participants_this_team -= 1 if temp_participant.can_review == false or temp_participant.can_submit == false&lt;br /&gt;
			  end&lt;br /&gt;
			  #if all outstanding participants are already in selected_participants, just break the loop.&lt;br /&gt;
			  break if selected_participants.size == participants.size - num_participants_this_team&lt;br /&gt;
&lt;br /&gt;
			  # generate random number&lt;br /&gt;
			  if iterator == 0&lt;br /&gt;
				rand_num = rand(0..num_participants-1)&lt;br /&gt;
			  else&lt;br /&gt;
				min_value = participants_hash.values.min&lt;br /&gt;
				#get the temp array including indices of participants, each participant has minimum review number in hash table.&lt;br /&gt;
				participants_with_min_assigned_reviews = Array.new&lt;br /&gt;
				participants.each do |participant|&lt;br /&gt;
				  participants_with_min_assigned_reviews &amp;lt;&amp;lt; participants.index(participant) if participants_hash[participant.id] == min_value&lt;br /&gt;
				end&lt;br /&gt;
				#if participants_with_min_assigned_reviews is blank &lt;br /&gt;
				if_condition_1 = participants_with_min_assigned_reviews.empty?&lt;br /&gt;
				#or only one element in participants_with_min_assigned_reviews, prohibit one student to review his/her own artifact&lt;br /&gt;
				if_condition_2 = (participants_with_min_assigned_reviews.size == 1 and TeamsUser.exists?(team_id: team.id, user_id: participants[participants_with_min_assigned_reviews[0]].user_id))&lt;br /&gt;
				if if_condition_1 or if_condition_2&lt;br /&gt;
				  #use original method to get random number&lt;br /&gt;
				  rand_num = rand(0..num_participants-1)&lt;br /&gt;
				else&lt;br /&gt;
				  #rand_num should be the position of this participant in original array&lt;br /&gt;
				  rand_num = participants_with_min_assigned_reviews[rand(0..participants_with_min_assigned_reviews.size-1)]&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			  # prohibit one student to review his/her own artifact&lt;br /&gt;
			  next if TeamsUser.exists?(team_id: team.id, user_id: participants[rand_num].user_id)&lt;br /&gt;
&lt;br /&gt;
			  if_condition_1 = (participants_hash[participants[rand_num].id] &amp;lt; student_review_num)&lt;br /&gt;
			  if_condition_2 = (!selected_participants.include? participants[rand_num].id)&lt;br /&gt;
			  if if_condition_1 and if_condition_2&lt;br /&gt;
				# selected_participants cannot include duplicate num&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participants[rand_num].id&lt;br /&gt;
				participants_hash[participants[rand_num].id] += 1&lt;br /&gt;
			  end &lt;br /&gt;
			  # remove students who have already been assigned enough num of reviews out of participants array&lt;br /&gt;
			  participants.each do |participant|&lt;br /&gt;
				if participants_hash[participant.id] == student_review_num&lt;br /&gt;
				  participants.delete_at(rand_num)&lt;br /&gt;
				  num_participants -= 1&lt;br /&gt;
				end&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  else&lt;br /&gt;
			#review num for last team can be different from other teams.&lt;br /&gt;
			#prohibit one student to review his/her own artifact and selected_participants cannot include duplicate num&lt;br /&gt;
			participants.each do |participant| &lt;br /&gt;
			  # avoid last team receives too many peer reviews&lt;br /&gt;
			  if !TeamsUser.exists?(team_id: team.id, user_id: participant.user_id) and selected_participants.size &amp;lt; num_reviews_per_team&lt;br /&gt;
				selected_participants &amp;lt;&amp;lt; participant.id &lt;br /&gt;
				participants_hash[participant.id] += 1&lt;br /&gt;
			  end&lt;br /&gt;
			end&lt;br /&gt;
		  end&lt;br /&gt;
&lt;br /&gt;
		  begin&lt;br /&gt;
			selected_participants.each {|index| ReviewResponseMap.where(:reviewee_id =&amp;gt; team.id, :reviewer_id =&amp;gt; index, :reviewed_object_id =&amp;gt; assignment_id).first_or_create}&lt;br /&gt;
		  rescue&lt;br /&gt;
			flash[:error] = &amp;quot;Automatic assignment of reviewer failed.&amp;quot;&lt;br /&gt;
		  end&lt;br /&gt;
		  iterator += 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The method is made private so that it can only be called with in the controller and cannot directly be called through a view.&lt;br /&gt;
&lt;br /&gt;
The complexity of the original method reduced after breaking it and can be easily readable now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def automatic_review_mapping_strategy(assignment_id, participants, teams, student_review_num=0, submission_review_num=0)&lt;br /&gt;
    participants_hash = {}&lt;br /&gt;
    participants.each { |participant| participants_hash[participant.id] = 0 }&lt;br /&gt;
    #calculate reviewers for each team&lt;br /&gt;
    num_participants = participants.size&lt;br /&gt;
    if student_review_num != 0 and submission_review_num == 0&lt;br /&gt;
      num_reviews_per_team = (participants.size * student_review_num * 1.0 / teams.size).round&lt;br /&gt;
      exact_num_of_review_needed = participants.size * student_review_num&lt;br /&gt;
    elsif student_review_num == 0 and submission_review_num != 0&lt;br /&gt;
      num_reviews_per_team = submission_review_num&lt;br /&gt;
      student_review_num = (teams.size * submission_review_num * 1.0 / participants.size).round&lt;br /&gt;
      exact_num_of_review_needed = teams.size * submission_review_num&lt;br /&gt;
    end&lt;br /&gt;
    #Exception detection: If instructor want to assign too many reviews done by each student, there will be an error msg.&lt;br /&gt;
    if student_review_num &amp;gt;= teams.size&lt;br /&gt;
      flash[:error] = 'You cannot set the number of reviews done by each student to be greater than or equal to total number of teams [or &amp;quot;participants&amp;quot; if it is an individual assignment].'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    peer_review_strategy(teams, num_participants, student_review_num, participants, participants_hash)&lt;br /&gt;
&lt;br /&gt;
    # after assigning peer reviews for each team, if there are still some peer reviewers not obtain enough peer review, just assign them to valid teams&lt;br /&gt;
    if ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and created_at &amp;gt; ? and calibrate_to = ?&amp;quot;, assignment_id, @@time_create_last_review_mapping_record, 0]).size &amp;lt; exact_num_of_review_needed&lt;br /&gt;
      participants_with_insufficient_review_num = Array.new&lt;br /&gt;
      participants_hash.each do |participant_id, review_num|&lt;br /&gt;
        participants_with_insufficient_review_num &amp;lt;&amp;lt; participant_id if review_num &amp;lt; student_review_num&lt;br /&gt;
      end&lt;br /&gt;
      unsorted_teams_hash = {}&lt;br /&gt;
      ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, assignment_id, 0]).each do |response_map|&lt;br /&gt;
        unless unsorted_teams_hash.has_key? (response_map.reviewee_id)&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] = 1 &lt;br /&gt;
        else&lt;br /&gt;
          unsorted_teams_hash[response_map.reviewee_id] += 1&lt;br /&gt;
        end&lt;br /&gt;
      end &lt;br /&gt;
      teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
      participants_with_insufficient_review_num.each do |participant_id|&lt;br /&gt;
        teams_hash.each do |team_id, num_review_received|&lt;br /&gt;
          unless TeamsUser.exists?(team_id: team_id, user_id: Participant.find(participant_id).user_id)&lt;br /&gt;
            ReviewResponseMap.where(:reviewee_id =&amp;gt; team_id, :reviewer_id =&amp;gt; participant_id, :reviewed_object_id =&amp;gt; assignment_id).first_or_create&lt;br /&gt;
            teams_hash[team_id] += 1&lt;br /&gt;
            teams_hash = teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
            break&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    @@time_create_last_review_mapping_record = ReviewResponseMap.where(reviewed_object_id: assignment_id).last.created_at&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101326</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101326"/>
		<updated>2016-03-24T03:29:55Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the method which assigns reviews to teams automatically is divided into a separate method called peer_review_strategy.&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101325</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101325"/>
		<updated>2016-03-24T03:29:44Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the method which assigns reviews to teams automatically is divided into a separate method called peer_review_strategy.&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101324</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101324"/>
		<updated>2016-03-24T03:24:23Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the method which assigns reviews to teams automatically is divided into a separate method called peer_review_strategy.&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same code after moving the methods to their respective models looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
      when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
        @review_user= params[:user]&lt;br /&gt;
        #If review response is required call review_response_report method in review_response_map model&lt;br /&gt;
        @reviewers = ReviewResponseMap.review_response_report(@id, @assignment,@type, @review_user)&lt;br /&gt;
        @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
        @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
      when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
        #If review report for feedback is required call feedback_response_report method in feedback_review_response_map model&lt;br /&gt;
        @reviewers = FeedbackResponseMap.feedback_response_report(@id, @type)&lt;br /&gt;
      when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
        #If review report for teammate is required call teammate_response_report method in teammate_review_response_map model&lt;br /&gt;
        @reviewers = TeammateReviewResponseMap.teammate_response_report(@id)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101323</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101323"/>
		<updated>2016-03-24T03:22:47Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the method which assigns reviews to teams automatically is divided into a separate method called peer_review_strategy.&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates reports based on the input of one of the three of 'ReviewResponseMap', 'FeedbackResponseMap', 'TeammateReviewResponseMap' and 'Calibration'. Calibration is a special case and should be handled in the controller itself. But the other three are models that are subclasses of ResponseMap model. The report is generated for each of the three by calling the ResponseMap model and obtaining the values by querying the database. Moving the code to the models and calling the methods in the model through the controller makes more sense than writing the code in controller.&lt;br /&gt;
&lt;br /&gt;
The following is the code snippet from the original method which contains the calls to ResponseMap model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
case @type&lt;br /&gt;
    when &amp;quot;ReviewResponseMap&amp;quot;&lt;br /&gt;
      if params[:user].nil?&lt;br /&gt;
        # This is not a search, so find all reviewers for this assignment&lt;br /&gt;
        response_maps_with_distinct_participant_id = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ? and calibrate_to = ?&amp;quot;, @id, @type, 0])&lt;br /&gt;
        @reviewers = []&lt;br /&gt;
        response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|&lt;br /&gt;
          @reviewers &amp;lt;&amp;lt; (AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id))&lt;br /&gt;
        end&lt;br /&gt;
        @reviewers = Participant.sort_by_name(@reviewers)&lt;br /&gt;
      else&lt;br /&gt;
        # This is a search, so find reviewers by user's full name&lt;br /&gt;
        user = User.select(&amp;quot;DISTINCT id&amp;quot;).where([&amp;quot;fullname LIKE ?&amp;quot;, '%'+params[:user][:fullname]+'%'])&lt;br /&gt;
        @reviewers = AssignmentParticipant.where([&amp;quot;user_id IN (?) and parent_id = ?&amp;quot;, user, @assignment.id])&lt;br /&gt;
      end&lt;br /&gt;
      #  @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature&lt;br /&gt;
      # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature&lt;br /&gt;
      @review_scores = @assignment.compute_reviews_hash&lt;br /&gt;
      @avg_and_ranges= @assignment.compute_avg_and_ranges_hash&lt;br /&gt;
    when &amp;quot;FeedbackResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'FeedbackResponseMap' and &lt;br /&gt;
      #reviewed_object_id in (select id from responses where &lt;br /&gt;
      #map_id in (select id from response_maps where reviewed_object_id = 722 and type = 'ReviewResponseMap'))&lt;br /&gt;
      @review_response_map_ids = ResponseMap.select(&amp;quot;id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'ReviewResponseMap'])&lt;br /&gt;
      @response_ids = Response.select(&amp;quot;id&amp;quot;).where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id IN (?) and type = ?&amp;quot;, @response_ids, @type])&lt;br /&gt;
    when &amp;quot;TeammateReviewResponseMap&amp;quot;&lt;br /&gt;
      #Example query&lt;br /&gt;
      #SELECT distinct reviewer_id FROM response_maps where type = 'TeammateReviewResponseMap' and reviewed_object_id = 711&lt;br /&gt;
      @reviewers = ResponseMap.select(&amp;quot;DISTINCT reviewer_id&amp;quot;).where([&amp;quot;reviewed_object_id = ? and type = ?&amp;quot;, @id, 'TeammateReviewResponseMap'])&lt;br /&gt;
    when &amp;quot;Calibration&amp;quot;&lt;br /&gt;
      participant = AssignmentParticipant.where(parent_id: params[:id], user_id: session[:user].id).first rescue nil&lt;br /&gt;
      if participant.nil?&lt;br /&gt;
        participant = AssignmentParticipant.create(parent_id: params[:id], user_id: session[:user].id, can_submit: 1, can_review: 1, can_take_quiz: 1, handle: 'handle')&lt;br /&gt;
      end&lt;br /&gt;
      @assignment = Assignment.find(params[:id])&lt;br /&gt;
      @review_questionnaire_ids = ReviewQuestionnaire.select(&amp;quot;id&amp;quot;)&lt;br /&gt;
      @assignment_questionnaire = AssignmentQuestionnaire.where([&amp;quot;assignment_id = ? and questionnaire_id IN (?)&amp;quot;, params[:id], @review_questionnaire_ids]).first&lt;br /&gt;
      @questions = @assignment_questionnaire.questionnaire.questions.select{|q| q.type == 'Criterion' or q.type == 'Scale'}&lt;br /&gt;
      @calibration_response_maps = ReviewResponseMap.where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, params[:id], 1])&lt;br /&gt;
      @review_response_map_ids = ReviewResponseMap.select('id').where([&amp;quot;reviewed_object_id = ? and calibrate_to = ?&amp;quot;, params[:id], 0])&lt;br /&gt;
      @responses = Response.where([&amp;quot;map_id IN (?)&amp;quot;, @review_response_map_ids])&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101316</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101316"/>
		<updated>2016-03-24T03:07:20Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''3. Cyclomatic complexity of automatic_review_mapping_strategy method'''&lt;br /&gt;
&lt;br /&gt;
The method automatic_review_mapping_strategy handles the number of reviews assigned to a individual and also the number of reviews that can be done with in a team. The method is very long and has many nested if statements due to which the complexity of the method is very high. Instead of a single method handling all the parts of the strategy, it is divided into several parts due to which the code is more readable and also the complexity of code is shared by each method.&lt;br /&gt;
&lt;br /&gt;
The method first checks the number of participants that are in an assignment and the number of teams that are present. It then sets the values for the maximum number of reviews that are possible and also the minimum number that are required. Then it assigns the reviews to each of the teams randomly when a request for review is made by a participant. At last it checks if the participant has the minimum number of reviews required after allotting the reviews. If not, it assigns more reviews to valid teams so that the minimum requirement is met.&lt;br /&gt;
&lt;br /&gt;
The part of the method which assigns reviews to teams automatically is divided into a separate method called peer_review_strategy.&lt;br /&gt;
&lt;br /&gt;
'''4. Moving code specific to models to models instead of controller'''&lt;br /&gt;
&lt;br /&gt;
The method response_report contains code which generates&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101292</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101292"/>
		<updated>2016-03-24T02:39:55Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] was used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101291</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101291"/>
		<updated>2016-03-24T02:39:44Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. The following are the couple of places where shuffle[0] is used and is replaced by sample.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101289</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101289"/>
		<updated>2016-03-24T02:39:04Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements in an array are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle. Also by using shuffle[0] we are shuffling all the elements in the array and then picking the first element instead of picking a single element randomly which is more efficient. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101288</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101288"/>
		<updated>2016-03-24T02:37:07Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
'''1. Unused variables and arguments'''&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
'''2. Use sample instead of shuffle'''&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101286</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101286"/>
		<updated>2016-03-24T02:36:10Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments&lt;br /&gt;
&lt;br /&gt;
There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101283</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101283"/>
		<updated>2016-03-24T02:34:12Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods which use the stack unnecessarily. So, it is better to remove the unused variables or at the least indicate that a variable is unused.&lt;br /&gt;
&lt;br /&gt;
For suppose when both keys and values are not used in a hash but are given as arguments, then the unused variables can be indicated by adding a &amp;quot;_&amp;quot; infront of the name or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allow them arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above case teams_hash should consist of a hash with both keys and values but the sorting is done based on values. So the key is replaced with a &amp;quot;_&amp;quot; so that the user may deem it unused in the implementation of the process.&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101279</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101279"/>
		<updated>2016-03-24T02:30:08Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* Code Improvements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101278</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101278"/>
		<updated>2016-03-24T02:29:28Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* About Review mapping controller */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the team for a submission , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
At line 533 and line 539&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101273</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101273"/>
		<updated>2016-03-24T02:27:46Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the submission team , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
At line 533 and line 539&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/harsha1007/expertiza Forked repository for the project]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ Expertiza Main Page]&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101269</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101269"/>
		<updated>2016-03-24T02:26:31Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: /* About Review mapping controller */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the submission team , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
At line 533 and line 539&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://bit.ly/myexpertiza  Demo link] &lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://relishapp.com/rspec Rspec Documentation]&lt;br /&gt;
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101267</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101267"/>
		<updated>2016-03-24T02:25:42Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==='''About Review mapping controller'''===&lt;br /&gt;
&lt;br /&gt;
/* When a reviewerreport reviews, assigning reviews and quizzes to teams. It also contains methods to assign reviews to participants automatically*/ Ignore this&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the submission team , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
At line 533 and line 539&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://bit.ly/myexpertiza  Demo link] &lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://relishapp.com/rspec Rspec Documentation]&lt;br /&gt;
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101266</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101266"/>
		<updated>2016-03-24T02:25:10Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1615. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Wiki write up'''==&lt;br /&gt;
&lt;br /&gt;
About Review mapping controller&lt;br /&gt;
&lt;br /&gt;
/* When a reviewerreport reviews, assigning reviews and quizzes to teams. It also contains methods to assign reviews to participants automatically*/ Ignore this&lt;br /&gt;
&lt;br /&gt;
Review mapping controller contains methods related to peer reviewing strategies. It contains methods to add a reviewer, delete a reviewer, selecting a reviewer. Depending on the number of students and number of submissions, the topics to be reviewed are assigned to the students automatically. If a user wants to look for the submission team , it returns the team by comparing the submission id's with the team id's. Also, it assigns quizzes dynamically. Generation of review report, feedback report and teammate review is done.&lt;br /&gt;
&lt;br /&gt;
==='''Code Improvements'''===&lt;br /&gt;
&lt;br /&gt;
1. Unused variables and arguments: There are unused variables in the methods. If there are unused variables in the methods, it uses stack unnecessarily. So, it is better to remove the unused variables.&lt;br /&gt;
&lt;br /&gt;
When both keys and values are not used, but given as arguments, then the used variables can be added &amp;quot;_&amp;quot; or replaced with &amp;quot;_&amp;quot; to represent it as unused variable but allowed in the arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
At line 533 and line 539&lt;br /&gt;
Previous Code: teams_hash = unsorted_teams_hash.sort_by{|k, v| v}.to_h&lt;br /&gt;
After Changing the code: teams_hash = unsorted_teams_hash.sort_by{|_, v| v}.to_h&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Use sample instead of shuffle&lt;br /&gt;
When sample is used, the elements are chosen by using random and unique indices in the array so that the elements doesn't repeat in the array. This cannot be guaranteed in shuffle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Previous Code:&lt;br /&gt;
assignment_team = assignment_teams.to_a.shuffle[0] rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.shuffle[0] rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
After Changing the code:&lt;br /&gt;
&lt;br /&gt;
assignment_team = assignment_teams.to_a.sample rescue nil&lt;br /&gt;
&lt;br /&gt;
topic = assignment.candidate_topics_to_review(reviewer).to_a.sample rescue nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://bit.ly/myexpertiza  Demo link] &lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://relishapp.com/rspec Rspec Documentation]&lt;br /&gt;
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101151</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101151"/>
		<updated>2016-03-23T21:07:27Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1553. Refactoring the Review Mapping Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides details about the OSS project which was based on refactoring one of controllers related to peer reviewing strategies used in Expertiza.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Introduction to Expertiza===&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is an open source project based on [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
===Problem Statement===&lt;br /&gt;
The following tasks were accomplished in this project:&lt;br /&gt;
&lt;br /&gt;
* Improved the clarity of code by improving the variable and parameter names.&lt;br /&gt;
* Long character strings were taken and given appropriate names.&lt;br /&gt;
* Handled pagination by a separate helper module, which can be used by multiple controllers.&lt;br /&gt;
* Implemented action_allowed for access_control  to prevent unauthorized access of methods.&lt;br /&gt;
* Prevented displaying of all versions for all users and tables when a user views the index page.&lt;br /&gt;
* Added missing CRUD methods to Versions Controller&lt;br /&gt;
* Added RSPEC testcases for testing changes done in Versions Controller&lt;br /&gt;
&lt;br /&gt;
===About Versions Controller===&lt;br /&gt;
This class manages different versions of reviews.  If a reviewer reviews a submission, and after that, the author revises the submission, the next time the reviewer does a review (s)he will create a new version.  Sometimes it’s necessary to find the current version of a review; sometimes it’s necessary to find all versions.  Similarly, a user may want to delete the current version of a review, or all versions of a review.&lt;br /&gt;
Pagination of versions helps the user to view a subset of versions at a time. Considering the huge number of versions in the system, it is very useful to have a pagination mechanism and a filtering mechanism which can be applied on the whole set of versions. The idea is to display the versions in an ordered, comprehensible and logical manner. In Expertiza the gem ‘will_paginate’ is used to achieve pagination.&lt;br /&gt;
&lt;br /&gt;
===Current Implementation===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Functionality=====&lt;br /&gt;
* Any user irrespective of his/ her privileges can view all the versions.&lt;br /&gt;
::The versions which a particular user can view should be restricted based on the privileges of the user. For instance, only a user with Administrator privileges should be able to view all the versions in the system. However, that is not the case now. Every user can view all the versions irrespective of whether the user is a student or an administrator.&lt;br /&gt;
* Any user can delete any version&lt;br /&gt;
::The versions which a particular user can delete should be restricted based on the privileges of the user. For instance, a student should not be allowed to delete any version. According to the current implementation any user can delete any version in the system.&lt;br /&gt;
* Filtering of versions were restricted to the current user&lt;br /&gt;
::The filtering options on versions were restricted to the current user. Sometimes a user might want to view versions associated with other users. For instance, an instructor might want to view the list of versions created by a particular student. This is not possible with the current implementation.&lt;br /&gt;
&lt;br /&gt;
=====Drawbacks and Solutions=====&lt;br /&gt;
* '''Problem 1''': The method paginate_list is doing more than one thing.&lt;br /&gt;
::The method paginate_list was building a complex search criteria based on the input params, getting the list of versions from the Database matching this search criteria and then calling the Page API. All these tasks in a single method made it difficult to understand.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# For filtering the versions list with proper search and pagination.&lt;br /&gt;
  def paginate_list(id, user_id, item_type, event, datetime)&lt;br /&gt;
    # Set up the search criteria&lt;br /&gt;
    criteria = ''&lt;br /&gt;
    criteria = criteria + &amp;quot;id = #{id} AND &amp;quot; if id &amp;amp;&amp;amp; id.to_i &amp;gt; 0&lt;br /&gt;
    if current_user_role? == 'Super-Administrator'&lt;br /&gt;
      criteria = criteria + &amp;quot;whodunnit = #{user_id} AND &amp;quot; if user_id &amp;amp;&amp;amp; user_id.to_i &amp;gt; 0&lt;br /&gt;
    end&lt;br /&gt;
    criteria = criteria + &amp;quot;whodunnit = #{current_user.try(:id)} AND &amp;quot; if current_user.try(:id) &amp;amp;&amp;amp; current_user.try(:id).to_i &amp;gt; 0&lt;br /&gt;
    criteria = criteria + &amp;quot;item_type = '#{item_type}' AND &amp;quot; if item_type &amp;amp;&amp;amp; !(item_type.eql? 'Any')&lt;br /&gt;
    criteria = criteria + &amp;quot;event = '#{event}' AND &amp;quot; if event &amp;amp;&amp;amp; !(event.eql? 'Any')&lt;br /&gt;
    criteria = criteria + &amp;quot;created_at &amp;gt;= '#{time_to_string(params[:start_time])}' AND &amp;quot;&lt;br /&gt;
    criteria = criteria + &amp;quot;created_at &amp;lt;= '#{time_to_string(params[:end_time])}' AND &amp;quot;&lt;br /&gt;
&lt;br /&gt;
    if current_role == 'Instructor' || current_role == 'Administrator'&lt;br /&gt;
&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Remove the last ' AND '&lt;br /&gt;
    criteria = criteria[0..-5]&lt;br /&gt;
&lt;br /&gt;
    versions = Version.page(params[:page]).order('id').per_page(25).where(criteria)&lt;br /&gt;
    versions&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* '''Solution''': The implementation has been changed in such a way that the versions which a user is allowed to see depends on the privileges of the user. The approach we have taken is as follows:&lt;br /&gt;
**An administrator can see all the versions&lt;br /&gt;
**An instructor can see all the versions created by him and other users who are in his course or are participants in the assignments he creates.&lt;br /&gt;
**A TA can see all the versions created by him and other users who are in the course for which he/ she assists.&lt;br /&gt;
**A Student can see all the versions created by him/ her.&lt;br /&gt;
* '''Problem 2''': The search criteria created in the method paginate_list was difficult to comprehend.&lt;br /&gt;
::The code which builds the search criteria in the method paginate_list uses many string literals and conditions and is hardly intuitive. The programmer will have to spend some time to understand what the code is really doing.&lt;br /&gt;
* '''Solution''': The implementation has been changed. A student is not allowed to delete any versions now. Other types of users, for instance administrators, instructors and TAs are allowed to delete only the versions they are authorized to view.&lt;br /&gt;
* '''Problem 3''': The paginate method can be moved to a helper class.&lt;br /&gt;
::VersionsController is not the only component which require to paginate items. There are other components too. For instance, the UsersController has to paginate the list of users. Hence the Paginate method can be moved to a helper class which can be accessed by other components as well.&lt;br /&gt;
* '''Solution''': The filtering options has also been enhanced. The current user can now choose as part of the version search filter any user from a list of users if the current user is authorized to see the versions created by that user.&lt;br /&gt;
&lt;br /&gt;
===New Implementation===&lt;br /&gt;
*The method paginate_list has been split into 2 methods now. &lt;br /&gt;
** BuildSearchCriteria – as the name suggests the sole purpose of this method is to build a search criteria based on the input search filters when the current user initiates a search in versions.&lt;br /&gt;
** paginate_list – this method will call the paginate API.&lt;br /&gt;
:First the search criteria is built, then the criteria is applied to versions in the database to get all versions which matches the criteria and then the retrieved versions are paginated.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # pagination.&lt;br /&gt;
  def paginate_list(versions)&lt;br /&gt;
    paginate(versions, VERSIONS_PER_PAGE);&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def BuildSearchCriteria(id, user_id, item_type, event)&lt;br /&gt;
    # Set up the search criteria&lt;br /&gt;
    search_criteria = ''&lt;br /&gt;
    search_criteria = search_criteria + add_id_filter_if_valid(id).to_s&lt;br /&gt;
    if current_user_role? == 'Super-Administrator'&lt;br /&gt;
      search_criteria = search_criteria + add_user_filter_for_super_admin(user_id).to_s&lt;br /&gt;
    end&lt;br /&gt;
    search_criteria = search_criteria + add_user_filter&lt;br /&gt;
    search_criteria = search_criteria + add_version_type_filter(item_type).to_s&lt;br /&gt;
    search_criteria = search_criteria + add_event_filter(event).to_s&lt;br /&gt;
    search_criteria = search_criteria + add_date_time_filter&lt;br /&gt;
    search_criteria&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* The string literals and conditions in the method paginate_list were replaced with methods with intuitive names so that the programmer can understand the code more easily. We also removed an empty if clause and a redundant statement.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def add_id_filter_if_valid (id)&lt;br /&gt;
    &amp;quot;id = #{id} AND &amp;quot; if id &amp;amp;&amp;amp; id.to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_user_filter_for_super_admin (user_id)&lt;br /&gt;
    &amp;quot;whodunnit = #{user_id} AND &amp;quot; if user_id &amp;amp;&amp;amp; user_id.to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_user_filter&lt;br /&gt;
    &amp;quot;whodunnit = #{current_user.try(:id)} AND &amp;quot; if current_user.try(:id) &amp;amp;&amp;amp; current_user.try(:id).to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_event_filter (event)&lt;br /&gt;
    &amp;quot;event = '#{event}' AND &amp;quot; if event &amp;amp;&amp;amp; !(event.eql? 'Any')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_date_time_filter&lt;br /&gt;
    &amp;quot;created_at &amp;gt;= '#{time_to_string(params[:start_time])}' AND &amp;quot; +&lt;br /&gt;
        &amp;quot;created_at &amp;lt;= '#{time_to_string(params[:end_time])}'&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_version_type_filter (version_type)&lt;br /&gt;
    &amp;quot;item_type = '#{version_type}' AND &amp;quot; if version_type &amp;amp;&amp;amp; !(version_type.eql? 'Any')&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* The paginate method has been moved to the helper class Pagination_Helper. This new method can be now reused by the different components like UsersController etc. The method receives two parameters, first the list to paginate and second the number of items to be displayed in a page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module PaginationHelper&lt;br /&gt;
&lt;br /&gt;
  def paginate (items, number_of_items_per_page)&lt;br /&gt;
    items.page(params[:page]).per_page(number_of_items_per_page)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code improvements===&lt;br /&gt;
* Introduced a constant VERSIONS_PER_PAGE and assigned the value 25 to it. The pagination algorithm for VersionsController displays at most 25 versions in a page. The existing implementation uses the value 25 straight in the code and there are few problems associated with such an approach.&lt;br /&gt;
** It is not easy to understand what 25 is unless the programmer takes a close look at the code.&lt;br /&gt;
** In case if the value 25 is used at more than one places and in future a new requirement comes to show at most 30 versions in a page, all the values will have to be modified. It is not very DRY.&lt;br /&gt;
* The VersionsController was overriding AccessHelper - action_allowed? method to return true in all the cases. This was violating the whole purpose of the method action_allowed?. The purpose of this method is to determine whether the user who is triggering a CRUD operation is allowed to do so. So when the current user invokes a CRUD operation, the action_allowed? method is invoked first and if the method returns true the CRUD operation is triggered or else the user is intimated with a message and gracefully exited. Hence, when the action_allowed? method is overridden to return true always, it results in providing unauthorized access to certain users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    true&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:With the new implementation the AccessHelper - action_allowed? method has been modified in such a way that unauthorized access is prevented. As per the new algorithm, 'new', 'create', 'edit', 'update' cannot be invoked by any user. These operations can be accessed only by ‘papertrail’ gem. Only an ‘Administrator’ or ‘Super-Administrator’ can call 'destroy_all' method. All the other methods are accessible to ‘Administrator’,  ‘Super-Administrator’, ‘Instructor’, ‘Teaching Assistant’ and ‘Student’.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'new', 'create', 'edit', 'update'&lt;br /&gt;
    #Modifications can only be done by papertrail&lt;br /&gt;
      return false&lt;br /&gt;
    when 'destroy_all'&lt;br /&gt;
      ['Super-Administrator',&lt;br /&gt;
       'Administrator'].include? current_role_name&lt;br /&gt;
    else&lt;br /&gt;
      #Allow all others&lt;br /&gt;
      ['Super-Administrator',&lt;br /&gt;
       'Administrator',&lt;br /&gt;
       'Instructor',&lt;br /&gt;
       'Teaching Assistant',&lt;br /&gt;
       'Student'].include? current_role_name&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Automated Testing using RSPEC===&lt;br /&gt;
The current version of expertiza did not have any test for VersionsController. Using the test driven development(TDD) approach, we have added an exhaustive set of RSPEC tests for VersionsController, to test all the modifications we have done to the code of the controller class. The tests use double and stub features of rspec-rails gem, to fake the log in by different users - Administrator, Instructor, Student etc. The tests can be executed &amp;quot;rpec spec&amp;quot; command as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user-expertiza $rspec spec&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
Finished in 5.39 seconds (files took 25.33 seconds to load)&lt;br /&gt;
66 examples, 0 failures&lt;br /&gt;
&lt;br /&gt;
Randomized with seed 19254&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Testing from UI===&lt;br /&gt;
Following are a few testcases with respectto our code changes that can be tried from UI:&lt;br /&gt;
1. To go to versions index page, type in the following url after logging in:&lt;br /&gt;
   http://152.46.16.81:3000/versions&lt;br /&gt;
&lt;br /&gt;
2. After logging in as student/instructor or admin : Try accessing the  new, create, edit, update actions. These actions are not allowed to any of the users.&lt;br /&gt;
   http://152.46.16.81:3000/versions/new&lt;br /&gt;
   This calls the new action. In the current production version of expertiza, it is unhandled and application gives a default 404 page.&lt;br /&gt;
&lt;br /&gt;
3. Another feature that can be tested from UI is Pagination. Try searching for a user's versions and see if the results are paginated or not. Search here:&lt;br /&gt;
   http://152.46.16.81:3000/versions/search&lt;br /&gt;
&lt;br /&gt;
4. Visit the same URL as step 3, you should see only the students under that instructor in the users dropdown.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://bit.ly/myexpertiza  Demo link] &lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://relishapp.com/rspec Rspec Documentation]&lt;br /&gt;
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101150</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=101150"/>
		<updated>2016-03-23T21:05:47Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==E1553. Refactoring the Versions Controller==&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Introduction to Expertiza===&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is an open source project based on [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
===Problem Statement===&lt;br /&gt;
The following tasks were accomplished in this project:&lt;br /&gt;
&lt;br /&gt;
* Improved the clarity of code by improving the variable and parameter names.&lt;br /&gt;
* Long character strings were taken and given appropriate names.&lt;br /&gt;
* Handled pagination by a separate helper module, which can be used by multiple controllers.&lt;br /&gt;
* Implemented action_allowed for access_control  to prevent unauthorized access of methods.&lt;br /&gt;
* Prevented displaying of all versions for all users and tables when a user views the index page.&lt;br /&gt;
* Added missing CRUD methods to Versions Controller&lt;br /&gt;
* Added RSPEC testcases for testing changes done in Versions Controller&lt;br /&gt;
&lt;br /&gt;
===About Versions Controller===&lt;br /&gt;
This class manages different versions of reviews.  If a reviewer reviews a submission, and after that, the author revises the submission, the next time the reviewer does a review (s)he will create a new version.  Sometimes it’s necessary to find the current version of a review; sometimes it’s necessary to find all versions.  Similarly, a user may want to delete the current version of a review, or all versions of a review.&lt;br /&gt;
Pagination of versions helps the user to view a subset of versions at a time. Considering the huge number of versions in the system, it is very useful to have a pagination mechanism and a filtering mechanism which can be applied on the whole set of versions. The idea is to display the versions in an ordered, comprehensible and logical manner. In Expertiza the gem ‘will_paginate’ is used to achieve pagination.&lt;br /&gt;
&lt;br /&gt;
===Current Implementation===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Functionality=====&lt;br /&gt;
* Any user irrespective of his/ her privileges can view all the versions.&lt;br /&gt;
::The versions which a particular user can view should be restricted based on the privileges of the user. For instance, only a user with Administrator privileges should be able to view all the versions in the system. However, that is not the case now. Every user can view all the versions irrespective of whether the user is a student or an administrator.&lt;br /&gt;
* Any user can delete any version&lt;br /&gt;
::The versions which a particular user can delete should be restricted based on the privileges of the user. For instance, a student should not be allowed to delete any version. According to the current implementation any user can delete any version in the system.&lt;br /&gt;
* Filtering of versions were restricted to the current user&lt;br /&gt;
::The filtering options on versions were restricted to the current user. Sometimes a user might want to view versions associated with other users. For instance, an instructor might want to view the list of versions created by a particular student. This is not possible with the current implementation.&lt;br /&gt;
&lt;br /&gt;
=====Drawbacks and Solutions=====&lt;br /&gt;
* '''Problem 1''': The method paginate_list is doing more than one thing.&lt;br /&gt;
::The method paginate_list was building a complex search criteria based on the input params, getting the list of versions from the Database matching this search criteria and then calling the Page API. All these tasks in a single method made it difficult to understand.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# For filtering the versions list with proper search and pagination.&lt;br /&gt;
  def paginate_list(id, user_id, item_type, event, datetime)&lt;br /&gt;
    # Set up the search criteria&lt;br /&gt;
    criteria = ''&lt;br /&gt;
    criteria = criteria + &amp;quot;id = #{id} AND &amp;quot; if id &amp;amp;&amp;amp; id.to_i &amp;gt; 0&lt;br /&gt;
    if current_user_role? == 'Super-Administrator'&lt;br /&gt;
      criteria = criteria + &amp;quot;whodunnit = #{user_id} AND &amp;quot; if user_id &amp;amp;&amp;amp; user_id.to_i &amp;gt; 0&lt;br /&gt;
    end&lt;br /&gt;
    criteria = criteria + &amp;quot;whodunnit = #{current_user.try(:id)} AND &amp;quot; if current_user.try(:id) &amp;amp;&amp;amp; current_user.try(:id).to_i &amp;gt; 0&lt;br /&gt;
    criteria = criteria + &amp;quot;item_type = '#{item_type}' AND &amp;quot; if item_type &amp;amp;&amp;amp; !(item_type.eql? 'Any')&lt;br /&gt;
    criteria = criteria + &amp;quot;event = '#{event}' AND &amp;quot; if event &amp;amp;&amp;amp; !(event.eql? 'Any')&lt;br /&gt;
    criteria = criteria + &amp;quot;created_at &amp;gt;= '#{time_to_string(params[:start_time])}' AND &amp;quot;&lt;br /&gt;
    criteria = criteria + &amp;quot;created_at &amp;lt;= '#{time_to_string(params[:end_time])}' AND &amp;quot;&lt;br /&gt;
&lt;br /&gt;
    if current_role == 'Instructor' || current_role == 'Administrator'&lt;br /&gt;
&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    # Remove the last ' AND '&lt;br /&gt;
    criteria = criteria[0..-5]&lt;br /&gt;
&lt;br /&gt;
    versions = Version.page(params[:page]).order('id').per_page(25).where(criteria)&lt;br /&gt;
    versions&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* '''Solution''': The implementation has been changed in such a way that the versions which a user is allowed to see depends on the privileges of the user. The approach we have taken is as follows:&lt;br /&gt;
**An administrator can see all the versions&lt;br /&gt;
**An instructor can see all the versions created by him and other users who are in his course or are participants in the assignments he creates.&lt;br /&gt;
**A TA can see all the versions created by him and other users who are in the course for which he/ she assists.&lt;br /&gt;
**A Student can see all the versions created by him/ her.&lt;br /&gt;
* '''Problem 2''': The search criteria created in the method paginate_list was difficult to comprehend.&lt;br /&gt;
::The code which builds the search criteria in the method paginate_list uses many string literals and conditions and is hardly intuitive. The programmer will have to spend some time to understand what the code is really doing.&lt;br /&gt;
* '''Solution''': The implementation has been changed. A student is not allowed to delete any versions now. Other types of users, for instance administrators, instructors and TAs are allowed to delete only the versions they are authorized to view.&lt;br /&gt;
* '''Problem 3''': The paginate method can be moved to a helper class.&lt;br /&gt;
::VersionsController is not the only component which require to paginate items. There are other components too. For instance, the UsersController has to paginate the list of users. Hence the Paginate method can be moved to a helper class which can be accessed by other components as well.&lt;br /&gt;
* '''Solution''': The filtering options has also been enhanced. The current user can now choose as part of the version search filter any user from a list of users if the current user is authorized to see the versions created by that user.&lt;br /&gt;
&lt;br /&gt;
===New Implementation===&lt;br /&gt;
*The method paginate_list has been split into 2 methods now. &lt;br /&gt;
** BuildSearchCriteria – as the name suggests the sole purpose of this method is to build a search criteria based on the input search filters when the current user initiates a search in versions.&lt;br /&gt;
** paginate_list – this method will call the paginate API.&lt;br /&gt;
:First the search criteria is built, then the criteria is applied to versions in the database to get all versions which matches the criteria and then the retrieved versions are paginated.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # pagination.&lt;br /&gt;
  def paginate_list(versions)&lt;br /&gt;
    paginate(versions, VERSIONS_PER_PAGE);&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def BuildSearchCriteria(id, user_id, item_type, event)&lt;br /&gt;
    # Set up the search criteria&lt;br /&gt;
    search_criteria = ''&lt;br /&gt;
    search_criteria = search_criteria + add_id_filter_if_valid(id).to_s&lt;br /&gt;
    if current_user_role? == 'Super-Administrator'&lt;br /&gt;
      search_criteria = search_criteria + add_user_filter_for_super_admin(user_id).to_s&lt;br /&gt;
    end&lt;br /&gt;
    search_criteria = search_criteria + add_user_filter&lt;br /&gt;
    search_criteria = search_criteria + add_version_type_filter(item_type).to_s&lt;br /&gt;
    search_criteria = search_criteria + add_event_filter(event).to_s&lt;br /&gt;
    search_criteria = search_criteria + add_date_time_filter&lt;br /&gt;
    search_criteria&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* The string literals and conditions in the method paginate_list were replaced with methods with intuitive names so that the programmer can understand the code more easily. We also removed an empty if clause and a redundant statement.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def add_id_filter_if_valid (id)&lt;br /&gt;
    &amp;quot;id = #{id} AND &amp;quot; if id &amp;amp;&amp;amp; id.to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_user_filter_for_super_admin (user_id)&lt;br /&gt;
    &amp;quot;whodunnit = #{user_id} AND &amp;quot; if user_id &amp;amp;&amp;amp; user_id.to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_user_filter&lt;br /&gt;
    &amp;quot;whodunnit = #{current_user.try(:id)} AND &amp;quot; if current_user.try(:id) &amp;amp;&amp;amp; current_user.try(:id).to_i &amp;gt; 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_event_filter (event)&lt;br /&gt;
    &amp;quot;event = '#{event}' AND &amp;quot; if event &amp;amp;&amp;amp; !(event.eql? 'Any')&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_date_time_filter&lt;br /&gt;
    &amp;quot;created_at &amp;gt;= '#{time_to_string(params[:start_time])}' AND &amp;quot; +&lt;br /&gt;
        &amp;quot;created_at &amp;lt;= '#{time_to_string(params[:end_time])}'&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def add_version_type_filter (version_type)&lt;br /&gt;
    &amp;quot;item_type = '#{version_type}' AND &amp;quot; if version_type &amp;amp;&amp;amp; !(version_type.eql? 'Any')&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* The paginate method has been moved to the helper class Pagination_Helper. This new method can be now reused by the different components like UsersController etc. The method receives two parameters, first the list to paginate and second the number of items to be displayed in a page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module PaginationHelper&lt;br /&gt;
&lt;br /&gt;
  def paginate (items, number_of_items_per_page)&lt;br /&gt;
    items.page(params[:page]).per_page(number_of_items_per_page)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code improvements===&lt;br /&gt;
* Introduced a constant VERSIONS_PER_PAGE and assigned the value 25 to it. The pagination algorithm for VersionsController displays at most 25 versions in a page. The existing implementation uses the value 25 straight in the code and there are few problems associated with such an approach.&lt;br /&gt;
** It is not easy to understand what 25 is unless the programmer takes a close look at the code.&lt;br /&gt;
** In case if the value 25 is used at more than one places and in future a new requirement comes to show at most 30 versions in a page, all the values will have to be modified. It is not very DRY.&lt;br /&gt;
* The VersionsController was overriding AccessHelper - action_allowed? method to return true in all the cases. This was violating the whole purpose of the method action_allowed?. The purpose of this method is to determine whether the user who is triggering a CRUD operation is allowed to do so. So when the current user invokes a CRUD operation, the action_allowed? method is invoked first and if the method returns true the CRUD operation is triggered or else the user is intimated with a message and gracefully exited. Hence, when the action_allowed? method is overridden to return true always, it results in providing unauthorized access to certain users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def action_allowed?&lt;br /&gt;
    true&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:With the new implementation the AccessHelper - action_allowed? method has been modified in such a way that unauthorized access is prevented. As per the new algorithm, 'new', 'create', 'edit', 'update' cannot be invoked by any user. These operations can be accessed only by ‘papertrail’ gem. Only an ‘Administrator’ or ‘Super-Administrator’ can call 'destroy_all' method. All the other methods are accessible to ‘Administrator’,  ‘Super-Administrator’, ‘Instructor’, ‘Teaching Assistant’ and ‘Student’.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def action_allowed?&lt;br /&gt;
    case params[:action]&lt;br /&gt;
    when 'new', 'create', 'edit', 'update'&lt;br /&gt;
    #Modifications can only be done by papertrail&lt;br /&gt;
      return false&lt;br /&gt;
    when 'destroy_all'&lt;br /&gt;
      ['Super-Administrator',&lt;br /&gt;
       'Administrator'].include? current_role_name&lt;br /&gt;
    else&lt;br /&gt;
      #Allow all others&lt;br /&gt;
      ['Super-Administrator',&lt;br /&gt;
       'Administrator',&lt;br /&gt;
       'Instructor',&lt;br /&gt;
       'Teaching Assistant',&lt;br /&gt;
       'Student'].include? current_role_name&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Automated Testing using RSPEC===&lt;br /&gt;
The current version of expertiza did not have any test for VersionsController. Using the test driven development(TDD) approach, we have added an exhaustive set of RSPEC tests for VersionsController, to test all the modifications we have done to the code of the controller class. The tests use double and stub features of rspec-rails gem, to fake the log in by different users - Administrator, Instructor, Student etc. The tests can be executed &amp;quot;rpec spec&amp;quot; command as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user-expertiza $rspec spec&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
Finished in 5.39 seconds (files took 25.33 seconds to load)&lt;br /&gt;
66 examples, 0 failures&lt;br /&gt;
&lt;br /&gt;
Randomized with seed 19254&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Testing from UI===&lt;br /&gt;
Following are a few testcases with respectto our code changes that can be tried from UI:&lt;br /&gt;
1. To go to versions index page, type in the following url after logging in:&lt;br /&gt;
   http://152.46.16.81:3000/versions&lt;br /&gt;
&lt;br /&gt;
2. After logging in as student/instructor or admin : Try accessing the  new, create, edit, update actions. These actions are not allowed to any of the users.&lt;br /&gt;
   http://152.46.16.81:3000/versions/new&lt;br /&gt;
   This calls the new action. In the current production version of expertiza, it is unhandled and application gives a default 404 page.&lt;br /&gt;
&lt;br /&gt;
3. Another feature that can be tested from UI is Pagination. Try searching for a user's versions and see if the results are paginated or not. Search here:&lt;br /&gt;
   http://152.46.16.81:3000/versions/search&lt;br /&gt;
&lt;br /&gt;
4. Visit the same URL as step 3, you should see only the students under that instructor in the users dropdown.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
&lt;br /&gt;
#[https://github.com/expertiza/expertiza Expertiza on GitHub]&lt;br /&gt;
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://bit.ly/myexpertiza  Demo link] &lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://relishapp.com/rspec Rspec Documentation]&lt;br /&gt;
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100842</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100842"/>
		<updated>2016-02-07T04:56:23Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| license                = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
}}&lt;br /&gt;
Devise is a [https://en.wikipedia.org/wiki/Rack_(web_server_interface) rack] based full-fledged authentication system for [https://en.wikipedia.org/wiki/Ruby_on_Rails Rails]. It is a complete [https://en.wikipedia.org/wiki/Model-view-controller MVC] solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by [https://github.com/plataformatec Plataformatec], a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called [https://github.com/hassox/warden/wiki Warden] which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for [https://en.wikipedia.org/wiki/Encryption encrypting] passwords, [https://en.wikipedia.org/wiki/Basic_access_authentication HTTP Authentication] etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a [https://en.wikipedia.org/wiki/Data_migration#Database_migration database migration] in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the [https://en.wikipedia.org/wiki/Database database]. Also most of these modules have specific [https://en.wikipedia.org/wiki/Form_(HTML) forms] and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100841</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100841"/>
		<updated>2016-02-07T04:47:51Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| license                = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
}}&lt;br /&gt;
Devise is a [https://en.wikipedia.org/wiki/Rack_(web_server_interface) rack] based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100840</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100840"/>
		<updated>2016-02-07T04:45:46Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| license                = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
}}&lt;br /&gt;
Devise is a [[Rack (web server interface)|rack]] based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100839</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100839"/>
		<updated>2016-02-07T04:44:33Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| license                = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
}}&lt;br /&gt;
Devise is a [[Rack_(web_server_interface)|rack]] based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100838</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100838"/>
		<updated>2016-02-07T04:39:43Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| license                = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
}}&lt;br /&gt;
Devise is a rack based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100837</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100837"/>
		<updated>2016-02-07T04:37:27Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox software&lt;br /&gt;
| name                   = &lt;br /&gt;
| logo                   = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| logo alt               = &lt;br /&gt;
| screenshot             = &amp;lt;!-- Image name is enough --&amp;gt;&lt;br /&gt;
| caption                = &lt;br /&gt;
| screenshot alt         = &lt;br /&gt;
| collapsible            = &lt;br /&gt;
| author                 = &lt;br /&gt;
| developer              = &lt;br /&gt;
| released               = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| discontinued           = &lt;br /&gt;
| latest release version = &lt;br /&gt;
| latest release date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| latest preview version = &lt;br /&gt;
| latest preview date    = &amp;lt;!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --&amp;gt;&lt;br /&gt;
| status                 = &lt;br /&gt;
| programming language   = &lt;br /&gt;
| operating system       = &lt;br /&gt;
| platform               = &lt;br /&gt;
| size                   = &lt;br /&gt;
| language               = &lt;br /&gt;
| language count         = &amp;lt;!-- Number only --&amp;gt;&lt;br /&gt;
| language footnote      = &lt;br /&gt;
| genre                  = &lt;br /&gt;
| license                = &lt;br /&gt;
| alexa                  = &lt;br /&gt;
| website                = {{URL|example.org}}&lt;br /&gt;
| standard               = &lt;br /&gt;
| AsOf                   = &lt;br /&gt;
}}&lt;br /&gt;
Devise is a rack based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100836</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100836"/>
		<updated>2016-02-07T04:35:36Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Devise is a rack based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100835</id>
		<title>CSC/ECE 517 Spring 2016/Refactor review mapping controller.rb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016/Refactor_review_mapping_controller.rb&amp;diff=100835"/>
		<updated>2016-02-07T04:35:21Z</updated>

		<summary type="html">&lt;p&gt;Skunapa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Devise}}&lt;br /&gt;
Devise is a rack based full-fledged authentication system for Rails. It is a complete MVC solution meaning it can support various models, views and controllers as part of its code and can be used be directly by developers. Devise is simple to use and starts up with a couple of commands but it is also highly customizable. Devise saves a lot of time and effort as many applications require user registration and authentication mechanisms which are difficult to develop from scratch.&lt;br /&gt;
&lt;br /&gt;
=='''History'''==&lt;br /&gt;
Devise is first introduced in January 2010 by Plataformatec, a company which builds web and mobile applications. Devise is one of the few authentication systems which support rack based applications and hence can support Rails 3 and up as they are completely rack based. The latest version of Devise available is v3.5.3 and it is up to date with Rails 5 beta 2.&lt;br /&gt;
===Warden===&lt;br /&gt;
Devise gem is built on top of a rack application called Warden which is used to verify the identity of logged in user using a session string. In Warden, the id which is a primary key of a user is somehow stored to match it later with the logged in user. Warden also provides restricted access to guest users depending on the functional requirements of an application.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Since Warden does not know of the existence of the Rails application, it cannot provide any helper methods, controller classes, views etc. This is where Devise comes into play and can integrate with Rails seemlessly. Devise interacts often with Warden using Strategy design pattern for encrypting passwords, HTTP Authentication etc.&lt;br /&gt;
&lt;br /&gt;
=='''Installation'''==&lt;br /&gt;
Although Devise is very useful and reduces the amount of effort in developing authentication mechanisms significantly, it requires a good understanding of the Rails framework. Hence it is advised for beginners to not use Devise.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are several commands that are required for the successful installation of Devise. They are listed out below:&lt;br /&gt;
*Add the devise gem to your gemfile.&lt;br /&gt;
 gem 'devise'&lt;br /&gt;
*Run bundle command to install the gem.&lt;br /&gt;
 bundle install&lt;br /&gt;
*You need to run the generator next which will install an initializer and creates all the configuration files.&lt;br /&gt;
 rails generate devise:install&lt;br /&gt;
*Now you can add Devise to any of your models using the generator. This will create a class with the model name given and routes etc. The model will be configured with default Devise modules. The config/routes.rb file will be configured to point to the Devise controller.&lt;br /&gt;
 rails generate devise user //Assuming that the model name is user&lt;br /&gt;
*Next, add any configuration changes that are required and then run:&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
*The following step will create Devise views but is optional. Devise has views for every generic operation like Login or SignUp and can be used directly instead of creating custom views.&lt;br /&gt;
 rails generate devise:views users&lt;br /&gt;
* There are also many routes that are defined in config/routes.rb with a line like:&lt;br /&gt;
 devise_for :users&lt;br /&gt;
&lt;br /&gt;
=='''Modules'''==&lt;br /&gt;
When a devise generator is invoked a model class is created in app/models for you modify for your specific application requirements. This is the place where many important configuration changes are specified. Perhaps the most important are the Devise modules which provides essential functionalities like enhanced securtiy.&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
There are 10 modules listed on the official page of Devise by Plataformatec. These modules are features that are contained in Devise and can be used by the developers depending on the use-cases or requirements of their application. Below is the list of modules:&lt;br /&gt;
*Database Authenticable&lt;br /&gt;
*Omniauthable&lt;br /&gt;
*Confirmable&lt;br /&gt;
*Recoverable&lt;br /&gt;
*Registerable&lt;br /&gt;
*Rememberable&lt;br /&gt;
*Trackable&lt;br /&gt;
*Timeoutable&lt;br /&gt;
*Validatable&lt;br /&gt;
*Lockable&lt;br /&gt;
Information regarding each module is listed in README as well as the [http://devise.plataformatec.com.br/ Plataformatec] website. The modules are included in an application in this way:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
  devise :database_authenticable, :omniauthable, :confirmable, :rememberable,&lt;br /&gt;
          :trackable, :timeoutable, :lockable&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In addition to the classes generated Devise also generates a database migration in which fields related to the functionalities of these modules are added. Each field is related to a specific module and hence when a module is not require some of the fields may be removed from the migration to the database. Also most of these modules have specific forms and view associated with them. The forms are used by an end user to type in his/her information which will then be sent to the Devise controllers.&lt;br /&gt;
&lt;br /&gt;
=='''Methods'''==&lt;br /&gt;
There are many classes in Devise which include models, controllers, helpers, views, routes etc. But much of the functionality offered by Devise is exposed via simple helper methods. Some of the most important methods which can be used in building our own application are:&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
*'''authenticate_user! :''' This method is used to check whether a user is logged in before he/she attempts to perform a specific set of controller actions. authenticate_user! may be called with before_action as shown below to ensure the user is logged in before performing any of the operations.&lt;br /&gt;
 before_action :authenticate_user!&lt;br /&gt;
If only some of the actions need authentication and some do not, we can use except clause so that only some actions are blocked as guest and others are accessible. The code with except clause is as below:&lt;br /&gt;
 before_action :authenticate_user! except [:index, :show]&lt;br /&gt;
In the above example index and show are two controller actions associated with operations which do not require user authentication and can be browsed as a guest.Authenticate user may also be used with in a controller action so that it is application only for that specification. We need to use before_filter instead of before_action to achieve this purpose.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EndUserBaseController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this example the application will authenticate a user only if he is trying to perform an action associated with the EndUserBaseController. If in any of the above cases a user is not logged in the application backs off and redirects to its sign-in page.&lt;br /&gt;
*'''current_user :''' current_user method is used to return the model class corresponding to the user who is currently signed in. For example, if you are building a messaging application, you may retrieve all the sent messages of a user as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SentMessagesController &amp;lt; ApplicationController&lt;br /&gt;
   before_filter :authentication_user!&lt;br /&gt;
   def index&lt;br /&gt;
     @sent_messages = current_user.sent_messages.all&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice how authenticate_user! is used before checking the messages of the current user so as to ensure that the user is signed in before checking his/her messages.&lt;br /&gt;
*'''user_signed_in? :''' As the name suggests, user_signed_in? method is used to check whether a user is signed in. This is useful when you want to show two different pages depending on whether a user has logged in or not. For example, when a user is logged in you want to show him/her an option to Logout otherwise you want to show Register or Login options.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% if user_signed_in? %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Logout&amp;quot;, destroy_user_session_path, method :delete %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% else %&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Sign Up&amp;quot;, new_user_registration_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;%- link_to &amp;quot;Login&amp;quot;, new_user_session_path %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We need to use method :delete to logout so that Devise will only logout when a HTTP delete request is made by user and does not accidentally logout because of a malicious link that automatically logs out the user.&lt;br /&gt;
*'''sign_in(@user) and sign_out(@user) :''' These methods are used to login(sign_in(@user)) or logout(sign_out(@user)) a newly created or existing user.&lt;br /&gt;
*'''user_session :''' This method returns metadata about the user that is currently logged in.&lt;br /&gt;
The methods that are most frequently used by developers are current_user and user_signed_in? which are present as helper methods. Also if the methods are to be referred to an Admin then replace user in each method with admin i.e. current_user becomes current_admin etc.&lt;br /&gt;
&lt;br /&gt;
=='''See Also'''==&lt;br /&gt;
=='''References'''==&lt;/div&gt;</summary>
		<author><name>Skunapa</name></author>
	</entry>
</feed>