CSC/ECE 517 Fall 2021 - E2121. Refactor suggestion controller.rb
This page provides a description of the Expertiza based OSS project.
About Expertiza
Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.
Problem Statement
The following tasks were accomplished in this project:
- Used more Ruby-like String concepts.
- Refactored create_new_team to assignment_team.rb.
- Refactored notification to simplify control logic.
- Refactored approve_suggestion to indicate that notification is being sent.
- Fixed the error in approve function.
- Added RSPEC testcases for testing changes done in Suggestion Controller.
Problems and Solutions
- Problem 1: Use more Ruby-like String concepts.
- The original code uses '==' and '!=' to compare a string with null, which is not Ruby-like.
if @suggestion.save flash[:success] = 'Thank you for your suggestion!' if @suggestion.unityID != '' flash[:success] = 'You have submitted an anonymous suggestion. It will not show in the suggested topic table below.' if @suggestion.unityID == '' end
- Solution: The approach we have taken is as follows: using empty? instead.
if @suggestion.save flash[:success] = 'Thank you for your suggestion!' unless @suggestion.unityID.empty? flash[:success] = 'You have submitted an anonymous suggestion. It will not show in the suggested topic table below.' if @suggestion.unityID.empty? end
- Problem 2: Refactor create_new_team to assignment_team.rb.
- The create_new_team function is not similiar to any other functions in suggestion_controller.rb.
- Solution: We should move create_new_team to assignment_team.rb.
- Problem 3: Refactor notification to simplify control logic.
- The original code uses too many 'if' and 'else', it looks complex.
def notification if @suggestion.signup_preference == 'Y' if @team_id.nil? create_new_team else if @topic_id.nil? SignedUpTeam.where(team_id: @team_id, is_waitlisted: 1).destroy_all SignedUpTeam.create(topic_id: @signuptopic.id, team_id: @team_id, is_waitlisted: 0) else @signuptopic.private_to = @user_id @signuptopic.save send_email end end else send_email end end
- Solution: Simplify the control logic as follow:
## old code needs review, new code is coming soon...
- Problem 4: Refactor approve_suggestion to indicate that notification is being sent.
- In the original version, there is no sign that if a notification is being sent or not.
- Solution: A comfirmation page will pop up when a notification is being sent.
- Problem 5: Fixed the error in approve function.
- There is an error when the instructor approves a topic:
- Solution: To fix it, we added new_topic_from_suggestion to app/models/sign_up_topic.rb and used it in approve function of app/controllers/suggestion_controller.rb.
def self.new_topic_from_suggestion(suggestion) signuptopic = SignUpTopic.new signuptopic.topic_identifier = 'S' + Suggestion.where("assignment_id = ? and id <= ?", suggestion.assignment_id, suggestion.id).size.to_s signuptopic.topic_name = suggestion.title signuptopic.assignment_id = suggestion.assignment_id signuptopic.max_choosers = 1 #return this model based on these checks if signuptopic.save && suggestion.update_attribute('status', 'Approved') return signuptopic else return 'failed' end end
Here is the screenshot after bug is fixed:
Automated Testing using RSPEC
Go to the expertiza source code root path and use following command to run test:
$ rspec spec/contollers/suggestion_controller_spec.rb
Here is the operation video link:
Test Plan
Travis CI
Also , our project passed the Travis CI build test.
Coverage
Our coverage increased 0.5%.
Testing from UI
Here is the VCL link:
http://152.7.176.62:8081/
For users intending to view the deployed Expertiza associated with this assignment, the credentials are below:
- Instructor login: username -> instructor6, password -> password (Use this account for Testing)
- Student login: username -> student4340, password -> password
- Student login: username -> student4405, password -> password
1. After logging in as Instructor, click "Manage instructor content" -> "Assignment":
2. Select the "Assignment" tab, in this example we search "textbook" as figure shows.
3. Choose a item and click "view suggestion" icon. In this example we use "Wiki textbook 2":
4. Click the "view" link in which tuple the Status is "Initiated":
5. Now we can see the Suggestion page with "Approve suggestion" and "Reject suggestion" butttons:
References
- Expertiza on GitHub
- GitHub Project Repository Fork
- The live Expertiza website
- VCL link (will expire in 3 weeks)
- Expertiza project documentation wiki
- Rspec Documentation
- Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin