<?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=Mrsingh</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=Mrsingh"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mrsingh"/>
	<updated>2026-09-11T19:00:38Z</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_2015_E1526_MPRI&amp;diff=96386</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96386"/>
		<updated>2015-04-01T03:52:57Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* What changes need to be made */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Similar to the previous task, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is difficult to re-design all webpages and make them responsive. After discussing with the contact person, we will be focusing only on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
1. Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application. The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
2. The webpage looks primitive.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
1. Actually it is unnecessary to fetch all data at the very beginning; if there is too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course. So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
2. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
As illustrated above, now the displaying list is too long and it takes too long to scroll to the end, which makes locating a specific course quite difficult.&lt;br /&gt;
&lt;br /&gt;
Also, the loading time can be reduced.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
&lt;br /&gt;
1. Create several buttons for different time intervals, for example, after clicking on the button '2013-2014', only courses for 2013 to 2014 are presenting, and there is no redirection during this process.&lt;br /&gt;
&lt;br /&gt;
2. The database should not be executed before the user clicks on the button. Only parts of databased is fetched when each button is clicked.&lt;br /&gt;
&lt;br /&gt;
3. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
The list is too long&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96385</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96385"/>
		<updated>2015-04-01T03:50:19Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* What's wrong with it */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Similar to the previous task, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is difficult to re-design all webpages and make them responsive. After discussing with the contact person, we will be focusing only on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
1. Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application. The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
2. The webpage looks primitive.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
1. Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course. So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
2. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
As illustrated above, now the displaying list is too long and it takes too long to scroll to the end, which makes locating a specific course quite difficult.&lt;br /&gt;
&lt;br /&gt;
Also, the loading time can be reduced.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
&lt;br /&gt;
1. Create several buttons for different time intervals, for example, after clicking on the button '2013-2014', only courses for 2013 to 2014 are presenting, and there is no redirection during this process.&lt;br /&gt;
&lt;br /&gt;
2. The database should not be executed before the user clicks on the button. Only parts of databased is fetched when each button is clicked.&lt;br /&gt;
&lt;br /&gt;
3. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
The list is too long&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96384</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96384"/>
		<updated>2015-04-01T03:49:51Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Pages To Be Modified */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Similar to the previous task, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is difficult to re-design all webpages and make them responsive. After discussing with the contact person, we will be focusing only on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
1. Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application. The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
2. The webpage look primitive.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
1. Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course. So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
2. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
As illustrated above, now the displaying list is too long and it takes too long to scroll to the end, which makes locating a specific course quite difficult.&lt;br /&gt;
&lt;br /&gt;
Also, the loading time can be reduced.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
&lt;br /&gt;
1. Create several buttons for different time intervals, for example, after clicking on the button '2013-2014', only courses for 2013 to 2014 are presenting, and there is no redirection during this process.&lt;br /&gt;
&lt;br /&gt;
2. The database should not be executed before the user clicks on the button. Only parts of databased is fetched when each button is clicked.&lt;br /&gt;
&lt;br /&gt;
3. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
The list is too long&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96383</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96383"/>
		<updated>2015-04-01T03:49:11Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Pages To Be Modified */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Similar to the previous task, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is difficult to re-design all webpages and make them responsive. After discussing with the contact person, our job is mainly focusing on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
1. Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application. The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
2. The webpage look primitive.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
1. Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course. So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
2. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
As illustrated above, now the displaying list is too long and it takes too long to scroll to the end, which makes locating a specific course quite difficult.&lt;br /&gt;
&lt;br /&gt;
Also, the loading time can be reduced.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
&lt;br /&gt;
1. Create several buttons for different time intervals, for example, after clicking on the button '2013-2014', only courses for 2013 to 2014 are presenting, and there is no redirection during this process.&lt;br /&gt;
&lt;br /&gt;
2. The database should not be executed before the user clicks on the button. Only parts of databased is fetched when each button is clicked.&lt;br /&gt;
&lt;br /&gt;
3. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
The list is too long&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96382</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96382"/>
		<updated>2015-04-01T03:48:00Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Making grades_view responsive and fast */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Similar to the previous task, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is unrealistic to re-design all webpages and make them responsive. After discussing with the contact person, our job is mainly focusing on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
1. Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application. The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
2. The webpage look primitive.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
1. Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course. So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
2. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
As illustrated above, now the displaying list is too long and it takes too long to scroll to the end, which makes locating a specific course quite difficult.&lt;br /&gt;
&lt;br /&gt;
Also, the loading time can be reduced.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
&lt;br /&gt;
1. Create several buttons for different time intervals, for example, after clicking on the button '2013-2014', only courses for 2013 to 2014 are presenting, and there is no redirection during this process.&lt;br /&gt;
&lt;br /&gt;
2. The database should not be executed before the user clicks on the button. Only parts of databased is fetched when each button is clicked.&lt;br /&gt;
&lt;br /&gt;
3. Bootstrap will be applied for better user interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
The list is too long&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96379</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96379"/>
		<updated>2015-04-01T03:43:45Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Problem Statement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use of a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant.So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
We will need to design better UI so that it is possible to add users to expertiza, to search a user and change roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
= Pages To Be Modified =&lt;br /&gt;
For this project, it is unrealistic to re-design all webpages and make them responsive. After discussing with the contact person, our job is mainly focusing on these 3 pages:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application.&lt;br /&gt;
&lt;br /&gt;
The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course.&lt;br /&gt;
&lt;br /&gt;
So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96366</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96366"/>
		<updated>2015-04-01T03:16:49Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Making grades_view responsive and fast */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Task 5===&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=== What's wrong with it ===&lt;br /&gt;
Now it takes more than 15 seconds to even login to the admin's home page with the sample development database. This is definitely not good enough for a daily used web application.&lt;br /&gt;
&lt;br /&gt;
The cause for the long rendering time is that it will be redirected to 'tree_display/list', which needs to fetch all questionnaires, courses and assignments before page rendering.&lt;br /&gt;
&lt;br /&gt;
=== What changes need to be made ===&lt;br /&gt;
Actually it is unnecessary to fetch all data at the very beginning; if there are too much data to present, the list will be extremely long and it is quite hard for users to locate a specific course.&lt;br /&gt;
&lt;br /&gt;
So, we will use AngularJS and jQuery to make a asynchronous webpage that delays the database query until all the basic html elements are correctly rendered, or until the user explicitly asks for that part of data.&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96362</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96362"/>
		<updated>2015-04-01T03:06:43Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* ACTION PANEL TOO COMPLEX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===Action panel too complex===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96360</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96360"/>
		<updated>2015-04-01T03:06:20Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* HELLO2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===ACTION PANEL TOO COMPLEX===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===Making grades_view responsive and fast===&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96358</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96358"/>
		<updated>2015-04-01T03:05:18Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* HELLO2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===ACTION PANEL TOO COMPLEX===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===HELLO2===&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96357</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96357"/>
		<updated>2015-04-01T03:05:04Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* ACTION PANEL TOO COMPLEX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===ACTION PANEL TOO COMPLEX===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===HELLO2===&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96356</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96356"/>
		<updated>2015-04-01T03:04:44Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* ACTION PANEL TOO COMPLEX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===ACTION PANEL TOO COMPLEX===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
===HELLO2===&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96354</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96354"/>
		<updated>2015-04-01T03:03:51Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* HELLO1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===ACTION PANEL TOO COMPLEX===&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===HELLO2===&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96352</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96352"/>
		<updated>2015-04-01T03:03:09Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Explanation of Tasks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
&lt;br /&gt;
===HELLO1===&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
&lt;br /&gt;
===HELLO2===&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96348</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96348"/>
		<updated>2015-04-01T03:02:22Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Explanation of Tasks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture4-2.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Picture5.png]]&lt;br /&gt;
[[File:Picture6.png]]&lt;br /&gt;
[[File:Picture7.png]]&lt;br /&gt;
[[File:Picture8.png]]&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
[[File:Picture1.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
[[File:Picture2.png]]&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;br /&gt;
[[File:Picture3.png]]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96324</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96324"/>
		<updated>2015-04-01T02:42:56Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Explanation of Tasks'''==&lt;br /&gt;
&lt;br /&gt;
Explanation to the task listed in the decription decumentation:&lt;br /&gt;
Updrading all buttons to Bootstrap (http://angular-ui.github.io/bootstrap/)&lt;br /&gt;
	No explanation needed&lt;br /&gt;
Creation of new theme which locks menu on the top for easy navigation.&lt;br /&gt;
	Example: https://www.virginamerica.com/&lt;br /&gt;
	When the page scrolls down, the menu is fixed on the top&lt;br /&gt;
&lt;br /&gt;
Auto hides contents on the dashboard for instructors. Updating action buttons with a responsive layout.&lt;br /&gt;
Now, when trying to open manage-courses, all records are loaded before rendering them onto the page, which takes too long time to load and is not responsive at all.&lt;br /&gt;
Better practice is on Virgin American’s website: https://www.virginamerica.com/book/rt/a1/sfo_bos/20150401_20150402&lt;br /&gt;
When the website is representing the calendar, the price for each day is not actually loaded. But the skeleton of the page, which is the calendar in this case, can be displayed first.&lt;br /&gt;
&lt;br /&gt;
So in Expertiza, we can create several buttons for course semesters in the page. After clicking on Fall 2014, the courses for Fall 2014 will come out. And if it is still too long and takes a long time load, we can use a ‘show more’ button ,or automatically load more data when the scroll bar reaching the end of the page, to minimize the content we need to load after one single mouse click. &lt;br /&gt;
&lt;br /&gt;
We will need to reduce the number of buttons. For example, the first row are actions for assignments. The second row is for participant...So we can change it to 5 buttons with responsive design, that is, no redirection after clicking on the buttons.&lt;br /&gt;
Better UI to add users to expertiza, search a user, change it roles.&lt;br /&gt;
Same as task 3, we will need to make it responsive. Also, when clicking on the letter A-Z, no redirections should happen and only the form content should be replaced rather than the whole page.&lt;br /&gt;
Upgrading gradesheet (grades_view) and review mapping something which webassign uses. &lt;br /&gt;
	After clicking on ‘Your scores’, all reviews are loaded before rendering the page now.&lt;br /&gt;
	That takes a long time to load and the length of review list is too long.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
View student score to be updated to show review on clicks rather than one below another.&lt;br /&gt;
	Same as task 5&lt;br /&gt;
Better score table using bootstrap.&lt;br /&gt;
No need to explain&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96320</id>
		<title>CSC/ECE 517 Spring 2015 E1526 MPRI</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015_E1526_MPRI&amp;diff=96320"/>
		<updated>2015-04-01T02:38:19Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1526. Responsive web design for Expertiza '''&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
&lt;br /&gt;
Despite an amazing set of functionality Expertiza offers, there are numerous parts of it that could use a more stylish look and an improved user experience. The goal of this project is to use both Bootstrap and AngularJS to improve the look of the entire Expertiza including the representations of buttons, tables, and other elements. Certain changes in design will also improve the efficiency of the web app when it comes to the amount of time it takes for a page to be loaded.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=='''Login Page'''==&lt;br /&gt;
&lt;br /&gt;
=='''Manage-Course page'''==&lt;br /&gt;
&lt;br /&gt;
=='''Manage-User page'''==&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95998</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95998"/>
		<updated>2015-03-27T17:02:33Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
= Design Patterns Followed =&lt;br /&gt;
'''Single Responsibility Principle:'''&lt;br /&gt;
&lt;br /&gt;
As per Single Responsibility Principle, every class should have responsibility over a single part of functionality provided by software and that responsibility should be entirely encapsulated by the class. All the services and operations within the class should be aligned with that responsibility. &lt;br /&gt;
Following this principle, we found that methods - add_bmapping and add_bmapping_signuptopic were present in the bookmarks class. They actually belong to bmapping class. So we moved those methods to the bmapping class. We have taken care of all the dependencies and successfully tested the changes.&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism:'''&lt;br /&gt;
&lt;br /&gt;
We realized that the methods search_fortags_allusers,search_fortags_forusers could be combined into one method . Similarly , search_alltags_allusers, , search_alltags_forusers could be grouped together into one method. This is because these methods are essentially doing the the same functionality but using different parameters. So we write a common method which takes parameters and based on the parameters passed, it achieves the required functionality.&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  #Adds bookmark with and without topic_id&lt;br /&gt;
 2:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 3:    # first check existence of the given bookmark and bmapping name&lt;br /&gt;
 5:    bookmark_exists = check_bookmark_exists(b_url) &lt;br /&gt;
 6:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 7:    if (!bookmark_exists || !bmapping_exists) # they don't exists, so create new ones&lt;br /&gt;
 8:      if(topic_id) # topic_id has been provided&lt;br /&gt;
 9:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
10:      else # topic_id has not been provided&lt;br /&gt;
11:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
12:      end&lt;br /&gt;
13:    elsif (bmapping_exists) # if the bmapping already exists, edit it instead of creating a new one&lt;br /&gt;
14:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
15:    end&lt;br /&gt;
16:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Before changes ===&lt;br /&gt;
Parts such as &amp;lt;b&amp;gt;bookmark_resource.nil?&amp;lt;/b&amp;gt; need to be refactored to a shorter version &amp;lt;b&amp;gt;!bookmark_resource&amp;lt;/b&amp;gt;. Similar opportunity to refactor comes up 2 more times within this code block (&amp;lt;b&amp;gt; !bmapping.nil? , !topic.nil?&amp;lt;/b&amp;gt;). We also need to change the first &amp;lt;i&amp;gt;if&amp;lt;/i&amp;gt; block to enable signup topic to be added only if topic_id does not already exists. Doing this will fix the bug of signup_topic overwriting an existing topic_id in case if topic was already created in the past.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== After Changes ===&lt;br /&gt;
Once the changes described in the &amp;lt;i&amp;gt;Before Change&amp;lt;/i&amp;gt; section were carried out, the code looks cleaner, the bug with topic_id is removed and the code functions marvelously!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if !bookmark_resource # if bookmark resource is nil (does not exist)&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(topic_id) # if topic_id was provided, add the signup topic&lt;br /&gt;
          # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if (bmapping) # if bmapping exists (it is not nil), look it up and edit the bookmarks&lt;br /&gt;
          topic = SignUpTopic.find(topic_id)&lt;br /&gt;
          if(topic) # if topic exists (it is not nil) use it to edit bookmark&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ##If the user id is passed, it will not be nil. So we use it in the query as shown below.&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
 &lt;br /&gt;
          ## If no user id is passed, the search is for all the user ids&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) ## Most popular tags for the supplied user&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil) ## Most popular tags for all users&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can visit our deployed Expertiza at &amp;lt;b&amp;gt;[http://52.11.88.182:3000 Demo]&amp;lt;/b&amp;gt; and use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&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/manish211/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://52.11.88.182:3000/ Demo link] - This might not be available after May of 2015&lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://docs.google.com/document/d/1J0WUBtYV_MDhpEQ-50z8gXE-OrvVkpEaZxvn_9RCAsM/edit#heading=h.mo1zn1d9w38l The OSS project requirements doc (Spring 2015 - Expertiza)]&lt;br /&gt;
#[https://drive.google.com/a/ncsu.edu/file/d/0B49VkAq-KMtebXBILUlTQktBVnM/view?usp=sharing Demo of Test suite]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95996</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95996"/>
		<updated>2015-03-27T17:00:49Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Problem Statement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
= Design Pattern Followed =&lt;br /&gt;
'''Single Responsibility Principle:'''&lt;br /&gt;
&lt;br /&gt;
As per Single Responsibility Principle, every class should have responsibility over a single part of functionality provided by software and that responsibility should be entirely encapsulated by the class. All the services and operations within the class should be aligned with that responsibility. &lt;br /&gt;
Following this principle, we found that methods - add_bmapping and add_bmapping_signuptopic were present in the bookmarks class. They actually belong to bmapping class. So we moved those methods to the bmapping class. We have taken care of all the dependencies and successfully tested the changes.&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism:'''&lt;br /&gt;
&lt;br /&gt;
We realized that the methods search_fortags_allusers,search_fortags_forusers could be combined into one method . Similarly , search_alltags_allusers, , search_alltags_forusers could be grouped together into one method. This is because these methods are essentially doing the the same functionality but using different parameters. So we write a common method which takes parameters and based on the parameters passed, it achieves the required functionality.&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  #Adds bookmark with and without topic_id&lt;br /&gt;
 2:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 3:    # first check existence of the given bookmark and bmapping name&lt;br /&gt;
 5:    bookmark_exists = check_bookmark_exists(b_url) &lt;br /&gt;
 6:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 7:    if (!bookmark_exists || !bmapping_exists) # they don't exists, so create new ones&lt;br /&gt;
 8:      if(topic_id) # topic_id has been provided&lt;br /&gt;
 9:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
10:      else # topic_id has not been provided&lt;br /&gt;
11:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
12:      end&lt;br /&gt;
13:    elsif (bmapping_exists) # if the bmapping already exists, edit it instead of creating a new one&lt;br /&gt;
14:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
15:    end&lt;br /&gt;
16:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Before changes ===&lt;br /&gt;
Parts such as &amp;lt;b&amp;gt;bookmark_resource.nil?&amp;lt;/b&amp;gt; need to be refactored to a shorter version &amp;lt;b&amp;gt;!bookmark_resource&amp;lt;/b&amp;gt;. Similar opportunity to refactor comes up 2 more times within this code block (&amp;lt;b&amp;gt; !bmapping.nil? , !topic.nil?&amp;lt;/b&amp;gt;). We also need to change the first &amp;lt;i&amp;gt;if&amp;lt;/i&amp;gt; block to enable signup topic to be added only if topic_id does not already exists. Doing this will fix the bug of signup_topic overwriting an existing topic_id in case if topic was already created in the past.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== After Changes ===&lt;br /&gt;
Once the changes described in the &amp;lt;i&amp;gt;Before Change&amp;lt;/i&amp;gt; section were carried out, the code looks cleaner, the bug with topic_id is removed and the code functions marvelously!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if !bookmark_resource # if bookmark resource is nil (does not exist)&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(topic_id) # if topic_id was provided, add the signup topic&lt;br /&gt;
          # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if (bmapping) # if bmapping exists (it is not nil), look it up and edit the bookmarks&lt;br /&gt;
          topic = SignUpTopic.find(topic_id)&lt;br /&gt;
          if(topic) # if topic exists (it is not nil) use it to edit bookmark&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ##If the user id is passed, it will not be nil. So we use it in the query as shown below.&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
 &lt;br /&gt;
          ## If no user id is passed, the search is for all the user ids&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) ## Most popular tags for the supplied user&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil) ## Most popular tags for all users&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can visit our deployed Expertiza at &amp;lt;b&amp;gt;[http://52.11.88.182:3000 Demo]&amp;lt;/b&amp;gt; and use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&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/manish211/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://52.11.88.182:3000/ Demo link] - This might not be available after May of 2015&lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://docs.google.com/document/d/1J0WUBtYV_MDhpEQ-50z8gXE-OrvVkpEaZxvn_9RCAsM/edit#heading=h.mo1zn1d9w38l The OSS project requirements doc (Spring 2015 - Expertiza)]&lt;br /&gt;
#[https://drive.google.com/a/ncsu.edu/file/d/0B49VkAq-KMtebXBILUlTQktBVnM/view?usp=sharing Demo of Test suite]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95991</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95991"/>
		<updated>2015-03-27T16:07:50Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  #Adds bookmark with and without topic_id&lt;br /&gt;
 2:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 3:    # first check existence of the given bookmark and bmapping name&lt;br /&gt;
 5:    bookmark_exists = check_bookmark_exists(b_url) &lt;br /&gt;
 6:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 7:    if (!bookmark_exists || !bmapping_exists) # they don't exists, so create new ones&lt;br /&gt;
 8:      if(topic_id) # topic_id has been provided&lt;br /&gt;
 9:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
10:      else # topic_id has not been provided&lt;br /&gt;
11:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
12:      end&lt;br /&gt;
13:    elsif (bmapping_exists) # if the bmapping already exists, edit it instead of creating a new one&lt;br /&gt;
14:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
15:    end&lt;br /&gt;
16:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ##If the user id is passed, it will not be nil. So we use it in the query as shown below.&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
 &lt;br /&gt;
          ## If no user id is passed, the search is for all the user ids&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) ## Most popular tags for the supplied user&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil) ## Most popular tags for all users&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can visit our deployed Expertiza at &amp;lt;b&amp;gt;[http://52.11.88.182:3000 Demo]&amp;lt;/b&amp;gt; and use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&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/manish211/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://52.11.88.182:3000/ Demo link] - This might not be available after May of 2015&lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://docs.google.com/document/d/1J0WUBtYV_MDhpEQ-50z8gXE-OrvVkpEaZxvn_9RCAsM/edit#heading=h.mo1zn1d9w38l The OSS project requirements doc (Spring 2015 - Expertiza)]&lt;br /&gt;
#[https://drive.google.com/a/ncsu.edu/file/d/0B49VkAq-KMtebXBILUlTQktBVnM/view?usp=sharing Demo of Test suite]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95989</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95989"/>
		<updated>2015-03-27T16:02:08Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ##If the user id is passed, it will not be nil. So we use it in the query as shown below.&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
 &lt;br /&gt;
          ## If no user id is passed, the search is for all the user ids&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can visit our deployed Expertiza at &amp;lt;b&amp;gt;[http://52.11.88.182:3000 Demo]&amp;lt;/b&amp;gt; and use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&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/manish211/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://52.11.88.182:3000/ Demo link] - This might not be available after May of 2015&lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://docs.google.com/document/d/1J0WUBtYV_MDhpEQ-50z8gXE-OrvVkpEaZxvn_9RCAsM/edit#heading=h.mo1zn1d9w38l The OSS project requirements doc (Spring 2015 - Expertiza)]&lt;br /&gt;
#[https://drive.google.com/a/ncsu.edu/file/d/0B49VkAq-KMtebXBILUlTQktBVnM/view?usp=sharing Demo of Test suite]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95988</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95988"/>
		<updated>2015-03-27T16:01:06Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ##If the user id is passed, it will not be nil. So we use it in the query as shown below.&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can visit our deployed Expertiza at &amp;lt;b&amp;gt;[http://52.11.88.182:3000 Demo]&amp;lt;/b&amp;gt; and use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&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/manish211/expertiza GitHub Project Repository Fork]&lt;br /&gt;
#[http://expertiza.ncsu.edu/ The live Expertiza website]&lt;br /&gt;
#[http://52.11.88.182:3000/ Demo link] - This might not be available after May of 2015&lt;br /&gt;
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]&lt;br /&gt;
#[https://docs.google.com/document/d/1J0WUBtYV_MDhpEQ-50z8gXE-OrvVkpEaZxvn_9RCAsM/edit#heading=h.mo1zn1d9w38l The OSS project requirements doc (Spring 2015 - Expertiza)]&lt;br /&gt;
#[https://drive.google.com/a/ncsu.edu/file/d/0B49VkAq-KMtebXBILUlTQktBVnM/view?usp=sharing Demo of Test suite]&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95894</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95894"/>
		<updated>2015-03-24T00:39:48Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes : search_alltags_allusers function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95893</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95893"/>
		<updated>2015-03-24T00:39:38Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes : search_alltags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95892</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95892"/>
		<updated>2015-03-24T00:39:19Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes:search_alltags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95891</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95891"/>
		<updated>2015-03-24T00:38:49Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_foruser function===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95890</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95890"/>
		<updated>2015-03-24T00:38:30Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* search_fortags_allusers function before the code changes is shown below */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes: search_fortags_allusers function ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95889</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95889"/>
		<updated>2015-03-24T00:38:03Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===search_fortags_allusers function before the code changes is shown below===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95888</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95888"/>
		<updated>2015-03-24T00:37:47Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
===search_fortags_allusers function before the code changes is shown below===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95887</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95887"/>
		<updated>2015-03-24T00:37:23Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95886</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95886"/>
		<updated>2015-03-24T00:37:07Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser function before the code changes is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95885</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95885"/>
		<updated>2015-03-24T00:36:39Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Before Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95884</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95884"/>
		<updated>2015-03-24T00:36:27Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. If we want to search for specified tags for all users, the userid is passed as nil. If we want to search for the specified tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95880</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95880"/>
		<updated>2015-03-24T00:33:30Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| view_topic_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view_review_bookmarks&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was not being accessed.&lt;br /&gt;
|-&lt;br /&gt;
| view&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark_form&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| create_tag_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;Bmappingstags&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;Bmappingstag&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect class name, the function was inaccessible.&lt;br /&gt;
|-&lt;br /&gt;
| edit_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;&lt;br /&gt;
| The method name has been refactored to &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt; as per the requirement&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark&lt;br /&gt;
| Renamed &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt;&lt;br /&gt;
| Due to incorrect method name, the method was inaccessible&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below. If we want to search for all tags for all users, the userid is passed as nil. If we want to search for all tags belonging to a particular userid, we pass that userid. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95831</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95831"/>
		<updated>2015-03-23T23:22:41Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Change on line 9 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95830</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95830"/>
		<updated>2015-03-23T23:22:31Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95829</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95829"/>
		<updated>2015-03-23T23:21:47Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     {{color|blue|@tags = BookmarksHelper.find_tags(tags_array)}}&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;I will change the color in {{color|blue|the middle part of}} this sentence.&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95828</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95828"/>
		<updated>2015-03-23T23:20:50Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     {{color|blue|@tags = BookmarksHelper.find_tags(tags_array)}}&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95827</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95827"/>
		<updated>2015-03-23T23:19:21Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     &amp;lt;SPAN STYLE=&amp;quot;background-color: #ffffcc&amp;quot;&amp;gt;@tags = BookmarksHelper.find_tags(tags_array)&amp;lt;/span&amp;gt;&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95826</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95826"/>
		<updated>2015-03-23T23:18:21Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* After Changes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:orange&amp;quot;&amp;gt; @tags = BookmarksHelper.find_tags(tags_array)&amp;lt;/span&amp;gt;&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95825</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95825"/>
		<updated>2015-03-23T23:10:38Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| &amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;add_bookmark&amp;quot;, :method =&amp;gt; &amp;quot;post&amp;quot;) do %&amp;gt;&lt;br /&gt;
| % used instead of %= and it was not working&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| added partials &lt;br /&gt;
| Nothing was there before to display&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95823</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95823"/>
		<updated>2015-03-23T23:08:14Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| removed unused code&lt;br /&gt;
| not used &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| Changed line to &amp;lt;%= link_to('Back', session[:comingFrom]) %&amp;gt;&lt;br /&gt;
| To make the back link work&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| changed line to&amp;lt;%= form_tag(:action =&amp;gt; &amp;quot;search_bookmarks&amp;quot;) do%&amp;gt;&lt;br /&gt;
| % was used instead of %= and it was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95822</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95822"/>
		<updated>2015-03-23T23:06:01Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb &lt;br /&gt;
| removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95821</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95821"/>
		<updated>2015-03-23T23:05:16Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
  removed asctime from the below line&lt;br /&gt;
| Was throwing error&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 4 : Refactoring &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods ==&lt;br /&gt;
&lt;br /&gt;
The methods ,&amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt;, add new bmapping to newly added bookmarks. These methods are initially served in bookmarks model. Ideally, they should be serving the bmapping model. &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; is a continuation to &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt;. It adds signup topic to the newly created bmapping. Along with moving these methods from bookmarks model, the team had to check for dependencies for these methods in all corresponding models and take care of them.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Changes done to bmapping model. The new added code in this model has been moved from bookmarks model.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Bmapping &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :user&lt;br /&gt;
  belongs_to :bookmark&lt;br /&gt;
  has_many :bmappings_tags&lt;br /&gt;
  has_and_belongs_to_many :sign_up_topics&lt;br /&gt;
  has_many :ratings, :class_name =&amp;gt; &amp;quot;BmappingRatings&amp;quot;, :foreign_key =&amp;gt; &amp;quot;bmapping_id&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  def cumulative_rating&lt;br /&gt;
    rating = 0.0&lt;br /&gt;
    count = 0&lt;br /&gt;
    self.ratings.each do |br|&lt;br /&gt;
      rating = rating + br.rating&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    end&lt;br /&gt;
    if count &amp;gt; 0&lt;br /&gt;
      return rating/count&lt;br /&gt;
    else&lt;br /&gt;
      return nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # Add bookmark - user association with its meta fields&lt;br /&gt;
    def self.add_bmapping(bid, b_title, user_id, b_description,b_tags_text)&lt;br /&gt;
      bookmark_user_mapping = Bmapping.new&lt;br /&gt;
      bookmark_user_mapping.bookmark_id = bid&lt;br /&gt;
      bookmark_user_mapping.title = b_title&lt;br /&gt;
      bookmark_user_mapping.description = b_description&lt;br /&gt;
      bookmark_user_mapping.user_id =user_id&lt;br /&gt;
      current_timestamp = Time.now&lt;br /&gt;
      bookmark_user_mapping.date_created = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.date_modified = current_timestamp&lt;br /&gt;
      bookmark_user_mapping.save&lt;br /&gt;
      # Add tags&lt;br /&gt;
      # tags come in as a text, separating them into a array&lt;br /&gt;
&lt;br /&gt;
      tag_array = BookmarksHelper.separate_tags(b_tags_text)&lt;br /&gt;
      for each_tag in tag_array&lt;br /&gt;
        # Look for each tag that is present in tags, if not make them, then make the BTU entry&lt;br /&gt;
        tag_tuple = Tag.where([&amp;quot;tagname = ?&amp;quot;,each_tag]).first&lt;br /&gt;
        if tag_tuple.nil?&lt;br /&gt;
          tag_tuple = Tag.new&lt;br /&gt;
          tag_tuple.tagname = each_tag&lt;br /&gt;
          tag_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
        # Check if there is an entry for this tag, this user and this bookmark (via bmappings table)&lt;br /&gt;
        btu_tuple = BmappingsTag.where([ &amp;quot;tag_id = ? and bmapping_id = ?&amp;quot;, tag_tuple.id, bookmark_user_mapping.id] ).first&lt;br /&gt;
        if btu_tuple.nil?&lt;br /&gt;
          btu_tuple = BmappingsTag.new&lt;br /&gt;
          btu_tuple.tag_id = tag_tuple.id&lt;br /&gt;
          btu_tuple.bmapping_id = bookmark_user_mapping.id&lt;br /&gt;
          btu_tuple.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
        return bookmark_user_mapping.id&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
      # Associate bmapping to the sign up topic&lt;br /&gt;
      def self.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        bmapping = Bmapping.find(bmappingid)&lt;br /&gt;
        unless (topic.nil? &amp;amp;&amp;amp; bmapping.nil?)&lt;br /&gt;
          topic.bmappings &amp;lt;&amp;lt; bmapping&lt;br /&gt;
          topic.save&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
The dependencies for these methods are present in only &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method of bookmark model. As &amp;lt;i&amp;gt;add_bmapping and add_bmapping_signuptopic&amp;lt;/i&amp;gt; methods support &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; in creating new bmapping to bookmarks,the dependencies lie only in &amp;lt;i&amp;gt; add_bookmark &amp;lt;/i&amp;gt; method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
    def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
      bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
      # Bookmark with the same url does not exists.&lt;br /&gt;
      if bookmark_resource.nil?&lt;br /&gt;
        # Add the newly discovered bookmark&lt;br /&gt;
        bookmarkid = add_new_bookmark(b_url,session_user.id)&lt;br /&gt;
        # Add its associations to a user&lt;br /&gt;
        bmappingid = add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
        # Add its association to the sign up topic&lt;br /&gt;
        add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url exists.&lt;br /&gt;
      else&lt;br /&gt;
        bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
        # Bookmark with the same user - bmapping exists.&lt;br /&gt;
        if ( !bmapping.nil? )&lt;br /&gt;
          topic = SignUpTopic.find(topicid)&lt;br /&gt;
          if( !topic.nil? )&lt;br /&gt;
            topic.bmappings.each do |mapping|&lt;br /&gt;
              if (mapping.id == bmapping.id)&lt;br /&gt;
                Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
              end&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Signup Topic does not exists&lt;br /&gt;
          else&lt;br /&gt;
            add_bmapping_signuptopic(topicid, bmapping.id)&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
        else&lt;br /&gt;
          # Increment user count for the existing bookmark&lt;br /&gt;
          bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
          bookmark_resource.save&lt;br /&gt;
          # Add its association with the user&lt;br /&gt;
          bmappingid = add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
          add_bmapping_signuptopic(topicid, bmappingid)&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Adds a bookmark and its various associations&lt;br /&gt;
  def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
    bookmark_resource = Bookmark.where([&amp;quot;url = ?&lt;br /&gt;
      &amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
    # Bookmark with the same url does not exists.&lt;br /&gt;
    if bookmark_resource.nil?&lt;br /&gt;
      # Add the newly discovered bookmark&lt;br /&gt;
      bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
      # Add its associations to a user&lt;br /&gt;
      bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
      if(!topic_id.nil?)&lt;br /&gt;
        # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
      # Bookmark with the same url exists.&lt;br /&gt;
    else&lt;br /&gt;
      bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
      # Bookmark with the same user - bmapping exists.&lt;br /&gt;
      if ( !bmapping.nil? )&lt;br /&gt;
        topic = SignUpTopic.find(topic_id)&lt;br /&gt;
        if( !topic.nil? )&lt;br /&gt;
          topic.bmappings.each do |mapping|&lt;br /&gt;
            if (mapping.id == bmapping.id)&lt;br /&gt;
              Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
            end&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          # Signup Topic does not exists&lt;br /&gt;
        else&lt;br /&gt;
          Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
      else&lt;br /&gt;
        # Increment user count for the existing bookmark&lt;br /&gt;
        bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
        bookmark_resource.save&lt;br /&gt;
        # Add its association with the user&lt;br /&gt;
        bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
        Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95746</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95746"/>
		<updated>2015-03-23T19:03:23Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for tags for user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95745</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95745"/>
		<updated>2015-03-23T19:02:53Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for tags for all users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95744</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95744"/>
		<updated>2015-03-23T19:02:33Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for all tags for user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95742</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95742"/>
		<updated>2015-03-23T19:02:06Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for all tags for all users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95739</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95739"/>
		<updated>2015-03-23T19:01:31Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for all tags for user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/managing_bookmarks &amp;quot;  to view all tags for the user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for all users===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;  http://localhost:3000/bookmarks/view_bookmarks  &amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for tags for user===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot;http://localhost:3000/bookmarks/managing_bookmarks&amp;quot; , enter the tags separated by comma (example - tag1,tag2) and then click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95732</id>
		<title>CSC/ECE 517 Spring 2015/oss E1504 IMV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2015/oss_E1504_IMV&amp;diff=95732"/>
		<updated>2015-03-23T18:55:51Z</updated>

		<summary type="html">&lt;p&gt;Mrsingh: /* Search for all tags for all users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''E1504. Refactoring the Bookmark Model'''&lt;br /&gt;
&lt;br /&gt;
This page provides a description of the Expertiza based OSS project. This team project successfully refactored the Bookmark Model by removing the duplicate code, combining the methods, improving the search function, moving Bmapping model methods to their respective class, covering dependencies associated with changed segments and adding a User Interface for using bookmarks.&lt;br /&gt;
&lt;br /&gt;
'''Introduction to Expertiza'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is a large project developed as a combined effort of students and faculty using the [http://rubyonrails.org/ Ruby on Rails] framework. The main advantage of using Expertiza, in an educational environment, is for the instructor to introduce peer reviewing among the students. Expertiza allows the instructor to create and customize assignments, create a list of topics the students can sign up for, have students work on teams and then review each other's assignments at the end. Expertiza supports submission of almost any document type, including the URLs and wiki pages.&lt;br /&gt;
&lt;br /&gt;
Expertiza is supported by National Science Foundation under Grant No. 0536558. Additional funding from the [http://www.ncsu.edu/ NCSU] [http://litre.ncsu.edu/ Learning in a Technology-Rich Environment] (LITRE) program, the NCSU [http://ofd.ncsu.edu/teaching-learning/ Faculty Center for Teaching and Learning], the NCSU [http://stem.ncsu.edu/ STEM] Initiative, and the Center for Advanced Computing and Communication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Problem Statement =&lt;br /&gt;
'''Classes involved:'''&lt;br /&gt;
 bookmark.rb&lt;br /&gt;
 bmapping.rb&lt;br /&gt;
 bookmarks_controller.rb&lt;br /&gt;
 auth_controller.rb&lt;br /&gt;
 auth_helper.rb&lt;br /&gt;
&lt;br /&gt;
'''What they do:'''&lt;br /&gt;
The bookmark model and controller work together to create and maintain user specific bookmarks, which can be added to different topics. Moreover, the user that created a bookmark can use his privilege to edit it at a later point in time. As a functionality added for convenience, any user can search bookmarks by either users who created them or their bookmark tags.&lt;br /&gt;
&lt;br /&gt;
'''What needs to be done:'''&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;The search methods in bookmarks model are being used used on a very granular level. This led to redundancy in bookmarks search methods. These methods include &amp;lt;i&amp;gt;search_alltags_allusers, search_fortags_allusers, search_fortags_forusers, search_alltags_forusers&amp;lt;/i&amp;gt;. Hence duplicates in these methods are to be singled out. &amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Following the CRUD declaration in Ruby on Rails, where the method name should specify the pseudo use of itself,methods such as &amp;lt;i&amp;gt;add_new_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit_this_bookmark&amp;lt;/i&amp;gt; have violate this naming convention. Therefore, we need to change the names of these two methods to &amp;lt;i&amp;gt;create&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;edit&amp;lt;/i&amp;gt;, respectively. Upon changing the names, all the dependencies need to be updated, as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Refactor &amp;lt;i&amp;gt;add_topic_bookmark&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_this_bookmark&amp;lt;/i&amp;gt; by removing the repetitive code. These two methods differ only in 2 lines of code, which make it reasonable to merge them together into a single method that provides functionality of both individual methods.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Fix the bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method where only bookmarks without &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; can be properly created. We add the functionality to this method to allow it to also create bookmarks with provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;.&lt;br /&gt;
   &amp;lt;li&amp;gt;Methods &amp;lt;i&amp;gt;add_bmapping&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; need to be moved to their appropriate model (Bmapping model) rather than reside in Bookmarks model. Completing this task involves checking the dependencies and updating the function calls for these methods, accordingly.&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Add user interface for Bookmarks as this functionality has not been a part of Expertiza&amp;lt;/li&amp;gt;&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a functional tests suite covering the functionality of all the methods that have been changed by our team.&lt;br /&gt;
   &amp;lt;li&amp;gt;Create a Cucumber integration test to show the functionality of added user interface for Bookmarks.&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Changes Made=&lt;br /&gt;
&lt;br /&gt;
==Bookmark Model==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| add_this_bookmark&lt;br /&gt;
| Added functionality of add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_topic_bookmark &lt;br /&gt;
| Merged the functionality into add_this_bookmarks and deleted add_topic_bookmark&lt;br /&gt;
| To implement code reusability and remove repetitive code.&lt;br /&gt;
|-&lt;br /&gt;
| add_bookmark &lt;br /&gt;
| Added a line that checks whether topic_id has been provided in the request&lt;br /&gt;
| It allows creation of new Bmapping if no topic_id was given and removes the bug of searching for bookmark with nil topic_id later on&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_allusers&lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_alltags_foruser &lt;br /&gt;
| |Merged search_alltags_allusers and search_alltags_foruser functions into search_alltags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_allusers&lt;br /&gt;
| | Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| search_fortags_foruser&lt;br /&gt;
| Merged search_fortags_allusers and search_fortags_foruser functions into search_fortags function&lt;br /&gt;
| Duplicate code present in both the functions. They differed only with the usage of parameters passed.&lt;br /&gt;
|-&lt;br /&gt;
| add_new_bookmark &lt;br /&gt;
| Changed the method name to &amp;quot;create&amp;quot;&lt;br /&gt;
| It is a much more meaningful and simpler name for a method that creates a new bookmark&lt;br /&gt;
|-&lt;br /&gt;
| edit_this_bookmark &lt;br /&gt;
| Changed the name to &amp;quot;edit&amp;quot;&lt;br /&gt;
| It is a much more appropriate name for a method that edits the bookmark it was called on&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in creating new bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|-&lt;br /&gt;
| add_bmapping_signuptopic&lt;br /&gt;
| Moved to class 'Bmapping'&lt;br /&gt;
| This method helps in adding a signup topic to a newly created bmapping. Hence it makes more sense if it belongs to bmapping class itself.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Bookmark Controller==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| create&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| update&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| edit&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; valign=&amp;quot;middle&amp;quot; |&lt;br /&gt;
destroy&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| ---------------&lt;br /&gt;
| ----------&lt;br /&gt;
|-&lt;br /&gt;
| ----------&lt;br /&gt;
| ----------&lt;br /&gt;
|}&lt;br /&gt;
==Views==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! style=&amp;quot;width:13%;&amp;quot;|Method Name&lt;br /&gt;
! style=&amp;quot;width:33%;&amp;quot;|Changes Made &lt;br /&gt;
! style=&amp;quot;width:43%;&amp;quot;|Reason For Change&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
| bookmarks/_result.html.erb&lt;br /&gt;
| ----------------&lt;br /&gt;
| -----------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/search_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| --------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/view_bookmarks.html.erb&lt;br /&gt;
| --------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/_searchmine.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/add_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/edit_bookmark_form.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| bookmarks/managing_bookmarks.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| layouts/application.html.erb&lt;br /&gt;
| -----------------&lt;br /&gt;
| ---------&lt;br /&gt;
|-&lt;br /&gt;
| tree_display/list.html.erb&lt;br /&gt;
| added a link to Manage Bookmarks page&lt;br /&gt;
| This allows a user to see the link to Managing Bookmarks page as soon as the user logs in.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Re-factored Code Cases =&lt;br /&gt;
&lt;br /&gt;
== Case 1 : Refactoring add_this_bookmark and add_topic_bookmark ==&lt;br /&gt;
&lt;br /&gt;
The consistency of these two methods shows plenty of repetitive code. In order to effectively refactor these methods and reuse the code they both need, our team merged the two methods together by enabling add_this_bookmark method to handle creation of a bookmark when a topic id is provided (main functionality of add_topic_bookmark method). Once we implemented this change on add_this_bookmark method, we deleted add_topic_bookmark method as it became obsolete. Therefore, we retained the same functionality while reducing the method by 7 repetitive lines of code. &lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
Please note that these two methods only differ in a single line of code (line 6 and 16) where line 6 takes an extra parameter (&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_topic_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topicid)&lt;br /&gt;
 2:    # Check if the bookmark exists and add / edit based on that&lt;br /&gt;
 3:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 4:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 5:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 6:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topicid)&lt;br /&gt;
 7:    elsif (bmapping_exists)&lt;br /&gt;
 8:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
 9:    end&lt;br /&gt;
10:  end&lt;br /&gt;
11:&lt;br /&gt;
12:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
13:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
14:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
15:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
16:      Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
17:    elsif (bmapping_exists)&lt;br /&gt;
18:      Bookmark.edit_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
19:    end&lt;br /&gt;
20:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 1:  def self.add_this_bookmark(b_url, b_title, b_tags_text, b_description,session_user, topic_id)&lt;br /&gt;
 2:    bookmark_exists = check_bookmark_exists(b_url)&lt;br /&gt;
 3:    bmapping_exists = check_bmapping_exists(b_url,session_user)&lt;br /&gt;
 4:    if (!bookmark_exists || !bmapping_exists)&lt;br /&gt;
 5:      if(topic_id) # something has been passed for topic_id&lt;br /&gt;
 6:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
 7:      else&lt;br /&gt;
 8:        Bookmark.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user, nil)&lt;br /&gt;
 9:      end&lt;br /&gt;
10:    elsif (bmapping_exists)&lt;br /&gt;
11:      Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
12:    end&lt;br /&gt;
13:  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 2 : Fixing a bug in &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method ==&lt;br /&gt;
&lt;br /&gt;
Our team fixed the bug associate with &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; nil being provided to &amp;lt;i&amp;gt;add_bookmark&amp;lt;/i&amp;gt; method (user has not specified &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;) when creating a bookmark. We include an if statement that calls &amp;lt;i&amp;gt;add_bmapping_signuptopic&amp;lt;/i&amp;gt; method only if the provided &amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt; is not nil. This change has been implemented on line 9 in the code block shown bellow.&lt;br /&gt;
&lt;br /&gt;
=== Change on line 9 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  # Adds a bookmark and its various associations&lt;br /&gt;
      def self.add_bookmark(b_url, b_title, b_tags_text, b_description,session_user,topic_id)&lt;br /&gt;
        bookmark_resource = Bookmark.where([&amp;quot;url = ?&amp;quot;,b_url]).first&lt;br /&gt;
&lt;br /&gt;
        # Bookmark with the same url does not exists.&lt;br /&gt;
        if bookmark_resource.nil?&lt;br /&gt;
          # Add the newly discovered bookmark&lt;br /&gt;
          bookmarkid = create(b_url,session_user.id)&lt;br /&gt;
          # Add its associations to a user&lt;br /&gt;
          bmappingid = Bmapping.add_bmapping(bookmarkid, b_title, session_user.id, b_description,b_tags_text )&lt;br /&gt;
9:        if(!topic_id.nil?)&lt;br /&gt;
            # Add its association to the sign up topic if the topic was provided&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
          # Bookmark with the same url exists.&lt;br /&gt;
        else&lt;br /&gt;
          bmapping = Bmapping.where(bookmark_id: bookmark_resource.id, user_id: session_user.id).first&lt;br /&gt;
          # Bookmark with the same user - bmapping exists.&lt;br /&gt;
          if ( !bmapping.nil? )&lt;br /&gt;
            topic = SignUpTopic.find(topic_id)&lt;br /&gt;
            if( !topic.nil? )&lt;br /&gt;
              topic.bmappings.each do |mapping|&lt;br /&gt;
                if (mapping.id == bmapping.id)&lt;br /&gt;
                  Bookmark.edit(b_url, b_title, b_tags_text, b_description,session_user)&lt;br /&gt;
                end&lt;br /&gt;
              end&lt;br /&gt;
&lt;br /&gt;
              # Signup Topic does not exists&lt;br /&gt;
            else&lt;br /&gt;
              Bmapping.add_bmapping_signuptopic(topic_id, bmapping.id)&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            # Bookmark with same user - bmapping does not exists.&lt;br /&gt;
          else&lt;br /&gt;
            # Increment user count for the existing bookmark&lt;br /&gt;
            bookmark_resource.user_count = bookmark_resource.user_count + 1&lt;br /&gt;
            bookmark_resource.save&lt;br /&gt;
            # Add its association with the user&lt;br /&gt;
            bmappingid = Bmapping.add_bmapping(bookmark_resource.id, b_title, session_user.id, b_description,b_tags_text)&lt;br /&gt;
            Bmapping.add_bmapping_signuptopic(topic_id, bmappingid)&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Case 3 : Refactoring search_fortags_allusers and search_fortags_forusers ==&lt;br /&gt;
&lt;br /&gt;
Both these methods contain almost the same code except for few conditional lines. search_fortags_allusers searches for all tags for all users whereas search_fortags_forusers searches only for those tags that belong to the user. The code for both these functions differed only in the query clause where at one place it checks for records returned for a specific user and at the other place, it does not have this check.&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_allusers function &lt;br /&gt;
(&amp;lt;i&amp;gt;topic_id&amp;lt;/i&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def  self.search_fortags_allusers(tags_array, order_by)&lt;br /&gt;
&lt;br /&gt;
      result_array = Array.new # returns this array&lt;br /&gt;
      ## find the tags associated with these tagnames&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      ## retreive mapping_ids taggeed with all of the word tags&lt;br /&gt;
      ## for every word, search for every bookmark that was tagged with that word. Then take the intersection of all the bmapping_ids&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
      end&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      if (order_by ==&amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
      ## for evey bmapping_id, create a hash, and push into the result_array&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id =?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
search_fortags_foruser&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def self.search_fortags_foruser(tags_array, this_user_id, order_by)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTags.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        #for q_t in q_tuples&lt;br /&gt;
        #end&lt;br /&gt;
&lt;br /&gt;
        if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        else&lt;br /&gt;
          temp_array = Array.new&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
          @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
      temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags,this_user_id ])&lt;br /&gt;
&lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  User.find(this_user_id).name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions are merged into a single function named &amp;quot;search_fortags function&amp;quot; as shown below. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      ## searches for tspecified tags, among a specified user, orders them by most popular and the most recently added&lt;br /&gt;
    def self.search_fortags(tags_array, order_by,this_user_id)&lt;br /&gt;
      #order by is in &amp;quot;most_popular&amp;quot; and &amp;quot;most_recent&amp;quot;&lt;br /&gt;
      result_array = Array.new&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      ## search for ids of the tags&lt;br /&gt;
      @tags = BookmarksHelper.find_tags(tags_array)&lt;br /&gt;
      @q_tuples_with_all_tags = Array.new&lt;br /&gt;
      first_time = &amp;quot;true&amp;quot;&lt;br /&gt;
      for each_tag in @tags&lt;br /&gt;
        ##search for all qualifier tuples with b&lt;br /&gt;
        q_tuples = BmappingsTag.where([&amp;quot;tag_id = ?&amp;quot;, each_tag])&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        #if first_time == &amp;quot;true&amp;quot;&lt;br /&gt;
          for q_t in q_tuples&lt;br /&gt;
            @q_tuples_with_all_tags &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          #first_time = &amp;quot;false&amp;quot;&lt;br /&gt;
        # else&lt;br /&gt;
        #   temp_array = Array.new&lt;br /&gt;
        #   for q_t in q_tuples&lt;br /&gt;
        #     temp_array &amp;lt;&amp;lt; q_t.bmapping_id&lt;br /&gt;
        #   end&lt;br /&gt;
        #   @q_tuples_with_all_tags = @q_tuples_with_all_tags &amp;amp; temp_array ## returns the items  common to both arrays&lt;br /&gt;
        # end&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
&lt;br /&gt;
      if(this_user_id != nil)&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids and the requested userid- search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?) and user_id = ?&amp;quot;, @q_tuples_with_all_tags, this_user_id ])&lt;br /&gt;
      else&lt;br /&gt;
        ## now you have qualifer tuples with all the required bmapping ids - search for the req ones&lt;br /&gt;
        temp_result_records =  Bmapping.where([&amp;quot;id in (?)&amp;quot;, @q_tuples_with_all_tags])&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      ## organize these tuples in the order of most earliest, most popular&lt;br /&gt;
      result_records = Array.new&lt;br /&gt;
      if (order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.date_created &amp;lt;=&amp;gt; x.date_created}&lt;br /&gt;
      else&lt;br /&gt;
        result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count }&lt;br /&gt;
      end&lt;br /&gt;
     &lt;br /&gt;
&lt;br /&gt;
      for result in result_records&lt;br /&gt;
        result_hash = Hash.new&lt;br /&gt;
        result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
        result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
        result_hash[&amp;quot;user&amp;quot;] =  result.user.name&lt;br /&gt;
        result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
        result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
        result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
        result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
        ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
        ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
        ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
        tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id=?&amp;quot;,result.id])&lt;br /&gt;
        tag_array = Array.new&lt;br /&gt;
        for tag_fetch in tag_fetchs&lt;br /&gt;
          tag_array &amp;lt;&amp;lt;  Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
        end&lt;br /&gt;
        result_hash[&amp;quot;tags&amp;quot;]  = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
        result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_foruser&amp;quot; function from the bookmark model before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 def self.search_alltags_foruser (the_userid, order_by)&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Before Changes===&lt;br /&gt;
&lt;br /&gt;
The below is the &amp;quot;search_alltags_allusers&amp;quot; before the changes were made.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def self.search_alltags_allusers(order_by)&lt;br /&gt;
      result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      if(order_by == &amp;quot;most_recent&amp;quot;)&lt;br /&gt;
        ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
        ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
        result_records = Bmapping.all(:order =&amp;gt;&amp;quot;date_created DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = result.user.name # displaying the newest owner of the bookmark,&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
          result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count ## number of people having this user in their repository&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
      elsif (order_by == &amp;quot;most_popular&amp;quot;)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTags.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      return result_array&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===After Changes===&lt;br /&gt;
&lt;br /&gt;
The above two functions were merged into a single function &amp;quot;search_alltags&amp;quot; as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def self.search_alltags (the_userid, order_by)&lt;br /&gt;
&lt;br /&gt;
        result_array = Array.new #going to append all the results into this array&lt;br /&gt;
&lt;br /&gt;
        if(order_by == &amp;quot;most_recent&amp;quot; )&lt;br /&gt;
          ## find all the bmappings in this system, created by this user, order them by the date created.&lt;br /&gt;
          # For each tuple returned from the bmapping, generate a hash, containing the url, the specified user's name, date this mapping was made,&lt;br /&gt;
          ## title, and description provided by this user. Store these hashes sequentially in a array ad return the array&lt;br /&gt;
&lt;br /&gt;
          if(the_userid == nil)&lt;br /&gt;
            ## find all the records in the system, order them by the date created. Using Bmapping here. Returns mappings that where created most recently,&lt;br /&gt;
            ## the user that created this mapping, the title and description provided this user&lt;br /&gt;
            &lt;br /&gt;
            result_records = Bmapping.all.order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          else&lt;br /&gt;
            result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          end&lt;br /&gt;
          &lt;br /&gt;
          #result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid]).order(&amp;quot;date_created DESC&amp;quot;).limit(20)&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = result.user.name&lt;br /&gt;
            result_hash[&amp;quot;created_at&amp;quot;] = result.date_created&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
&lt;br /&gt;
          end&lt;br /&gt;
        elsif ( order_by == &amp;quot;most_popular&amp;quot; and the_userid != nil) &lt;br /&gt;
          ### retrieving the most popular records that this user.(The user need not be the discoverer). First retrieve the the user's bookmarks from the bmapping.&lt;br /&gt;
          ### order these records, based on the user_count of the url&lt;br /&gt;
          temp_result_records = Bmapping.where([&amp;quot; user_id = ?&amp;quot;, the_userid])&lt;br /&gt;
          ## order result records by result.user_count a.sort {|x,y| y &amp;lt;=&amp;gt; x }&lt;br /&gt;
          result_records = temp_result_records.sort {|x,y| y.bookmark.user_count &amp;lt;=&amp;gt; x.bookmark.user_count}&lt;br /&gt;
          for result in result_records&lt;br /&gt;
            result_hash = Hash.new&lt;br /&gt;
            result_hash[&amp;quot;url&amp;quot;] = result.bookmark.url&lt;br /&gt;
            result_hash[&amp;quot;id&amp;quot;] = result.id&lt;br /&gt;
            result_hash[&amp;quot;user&amp;quot;] = User.find(the_userid).name&lt;br /&gt;
            result_hash[&amp;quot;title&amp;quot;] = result.title&lt;br /&gt;
            result_hash[&amp;quot;description&amp;quot;] = result.description&lt;br /&gt;
            result_hash[&amp;quot;copied_by&amp;quot;] = result.bookmark.user_count&lt;br /&gt;
            ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
            ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
            ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
            tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,result.id])&lt;br /&gt;
            tag_array = Array.new&lt;br /&gt;
            for tag_fetch in tag_fetchs&lt;br /&gt;
              tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
            end&lt;br /&gt;
            result_hash[&amp;quot;tags&amp;quot;] =BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
            result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
          end&lt;br /&gt;
        elsif (order_by == &amp;quot;most_popular&amp;quot; and the_userid == nil)&lt;br /&gt;
        ## returns the url boomarked by the most number of users, the discoverer of that url, the title and description provided by the discoverer&lt;br /&gt;
        result_records = Bookmark.all(:order =&amp;gt;&amp;quot;user_count DESC&amp;quot;, :limit =&amp;gt;20)&lt;br /&gt;
        for result in result_records&lt;br /&gt;
          ## for each tuple returned by the query above, create a new hash, store the values appropriately, and append into the return_array&lt;br /&gt;
          result_hash = Hash.new&lt;br /&gt;
          result_hash[&amp;quot;url&amp;quot;] = result.url&lt;br /&gt;
          result_hash[&amp;quot;user&amp;quot;] = User.find(result.discoverer_user_id).name&lt;br /&gt;
          result_hash[&amp;quot;copied_by&amp;quot;] = result.user_count&lt;br /&gt;
          b_u_mapping = Bmapping.where([&amp;quot;bookmark_id = ? and user_id = ?&amp;quot;, result.id, result.discoverer_user_id]).first&lt;br /&gt;
          result_hash[&amp;quot;id&amp;quot;] = b_u_mapping.id&lt;br /&gt;
          result_hash[&amp;quot;description&amp;quot;] = b_u_mapping.description&lt;br /&gt;
          result_hash[&amp;quot;title&amp;quot;] = b_u_mapping.title&lt;br /&gt;
          ## now retrieving tags for this user-bookmak mapping&lt;br /&gt;
          ## first retrieve all the tag_ids mapped to the BMapping id. Then retrieve all the tag names of the tag_ids picked up.&lt;br /&gt;
          ## Append all these into a comma separated string, and push them onto the hash&lt;br /&gt;
&lt;br /&gt;
          tag_fetchs = BmappingsTag.where([&amp;quot;bmapping_id = ?&amp;quot;,b_u_mapping.id])&lt;br /&gt;
          tag_array = Array.new&lt;br /&gt;
          for tag_fetch in tag_fetchs&lt;br /&gt;
            tag_array &amp;lt;&amp;lt; Tag.find(tag_fetch.tag_id).tagname&lt;br /&gt;
          end&lt;br /&gt;
          result_hash[&amp;quot;tags&amp;quot;] = BookmarksHelper.join_tags(tag_array)&lt;br /&gt;
          result_array &amp;lt;&amp;lt; result_hash&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        return result_array&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Steps to verify changes manually=&lt;br /&gt;
&lt;br /&gt;
===User2 Role===&lt;br /&gt;
&lt;br /&gt;
In order to inspect the changes made to the bookmark mode manually, you can use the following credentials:&lt;br /&gt;
     &amp;lt;b&amp;gt;User Name: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt;user2&amp;lt;/i&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Password: &amp;lt;/b&amp;gt; &amp;lt;i&amp;gt; any password&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can notice in the credentials above, you can type in any password you wish in order to log in as user2. We have accomplished this by disabling the authentication and the reason behind it was to have an easily reachable user account for test and development purposes.&lt;br /&gt;
&lt;br /&gt;
The below screenshots demonstrate the working of search features .&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for all users===&lt;br /&gt;
&amp;lt;b&amp;gt;Visit the url &amp;quot; http://localhost:3000/bookmarks/viewing_bookmarks&amp;quot; and click on search button to view the results&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:SearchAllTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
===Search for all tags for user===&lt;br /&gt;
&lt;br /&gt;
[[File:searchAlltagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Search for tags for all users&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsAllUsers.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Search for tags for user&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:searchForTagsForUser.png]]&lt;br /&gt;
&lt;br /&gt;
=Automated Tests=&lt;br /&gt;
&lt;br /&gt;
===Functional test suite===&lt;br /&gt;
&lt;br /&gt;
We have developed a suit of extensive unit tests covering all the functionality that we have add/modified in the forked version of Expertiza. The test suite consists of 9 tests providing 14 assertions that cover the functionality of all the method changes described in the former sections of this wiki page. The source code for these tests is stored in &amp;lt;i&amp;gt;test/models/bookmark_test.rb&amp;lt;/i&amp;gt; file. You can run this test suite by simply navigating to expertiza project directory and running command: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby -I test test/models/bookmark_test.rb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Integration (Cucumber) tests===&lt;br /&gt;
&lt;br /&gt;
In addition to functional tests, we have also provided a Cucumber test as an integration test. The goal of this test is to show that a user can access the &amp;lt;i&amp;gt;Manage Bookmarks&amp;lt;/i&amp;gt; page upon successful log in, proving that bookmarks view have been successfully integrated into expertiza system. The construct of this test can be found in following files:&lt;br /&gt;
&amp;lt;pre&amp;gt;features/bookmark.feature &lt;br /&gt;
features/step_definitions/bookmark_steps&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run this cucumber test, please navigate to expertiza project directory and run the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake cucumber&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References=&lt;/div&gt;</summary>
		<author><name>Mrsingh</name></author>
	</entry>
</feed>