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.
- Added RSPEC testcases for testing changes done in Suggestion Controller.
About Suggestion Controller
TBD
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:
# Waiting code to be commit...
- 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.
# code area
- Solution: A comfirmation page will pop up when a notification is being sent.
# code area
- 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
Automated Testing using RSPEC
The current version of expertiza did not have any test for VersionsController. Using the test driven development(TDD) approach, we have added an exhaustive set of RSPEC tests for VersionsController, to test all the modifications we have done to the code of the controller class. The tests use double and stub features of rspec-rails gem, to fake the log in by different users - Administrator, Instructor, Student etc. The tests can be executed "rpec spec" command as shown below.
user-expertiza $rspec spec . . . Finished in 5.39 seconds (files took 25.33 seconds to load) 66 examples, 0 failures Randomized with seed 19254 . .
Testing from UI
Following are a few testcases with respectto our code changes that can be tried from UI: 1. To go to versions index page, type in the following url after logging in:
http://152.46.16.81:3000/versions
2. After logging in as student/instructor or admin : Try accessing the new, create, edit, update actions. These actions are not allowed to any of the users.
http://152.46.16.81:3000/versions/new This calls the new action. In the current production version of expertiza, it is unhandled and application gives a default 404 page.
3. Another feature that can be tested from UI is Pagination. Try searching for a user's versions and see if the results are paginated or not. Search here:
http://152.46.16.81:3000/versions/search
4. Visit the same URL as step 3, you should see only the students under that instructor in the users dropdown.
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