-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix filtering on new saved annotation page #5838
Changes from 2 commits
8bc4623
fb3f620
1bb1884
5cc5dd7
40633be
2e997f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,13 @@ | ||||||||||
class AnnotationPolicy < ApplicationPolicy | ||||||||||
class Scope < ApplicationPolicy::Scope | ||||||||||
def resolve | ||||||||||
annotations = scope.joins('INNER JOIN submissions AS submission ON submission.id = annotations.submission_id') | ||||||||||
if user&.zeus? | ||||||||||
scope.all | ||||||||||
annotations.all | ||||||||||
elsif user | ||||||||||
common = scope.joins(:submission) | ||||||||||
common.released.where(submissions: { user: user }).or(common.where(submissions: { course_id: user.administrating_courses.map(&:id) })) | ||||||||||
annotations.released.where(submission: { user: user }).or(annotations.where(submission: { course_id: user.administrating_courses.map(&:id) })) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Simplify complex The Consider refactoring as follows: -annotations.released.where(submission: { user: user }).or(annotations.where(submission: { course_id: user.administrating_courses.map(&:id) }))
+user_annotations = annotations.released.where(submission: { user: user })
+admin_annotations = annotations.where(submission: { course_id: user.administrating_courses.ids })
+user_annotations.or(admin_annotations) This separates the conditions and makes the logic more transparent. 📝 Committable suggestion
Suggested change
|
||||||||||
else | ||||||||||
scope.none | ||||||||||
annotations.none | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure test coverage for the updated With changes to the This aligns with previous learnings about updating tests when introducing new variables. Would you like assistance in writing additional tests to ensure comprehensive coverage? |
||||||||||
end | ||||||||||
end | ||||||||||
end | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,25 @@ def setup | |
|
||
assert_response :forbidden | ||
end | ||
|
||
test 'staff should be able to filter existing annotations on new page' do | ||
get new_saved_annotation_path | ||
|
||
assert_response :success | ||
|
||
get new_saved_annotation_path, params: { exercise_id: 1 } | ||
|
||
assert_response :success | ||
end | ||
Comment on lines
+53
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance test coverage for staff filtering The test correctly checks if staff can access the new saved annotation page with and without filtering. However, it could be improved to verify the actual filtering functionality. Consider the following improvements:
Here's a suggested improvement: test 'staff should be able to filter existing annotations on new page' do
exercise1 = create(:exercise)
exercise2 = create(:exercise)
annotation1 = create(:saved_annotation, user: @user, course: @course, exercise: exercise1)
annotation2 = create(:saved_annotation, user: @user, course: @course, exercise: exercise2)
get new_saved_annotation_path
assert_response :success
assert_select 'your-selector-for-annotation', 2
get new_saved_annotation_path, params: { exercise_id: exercise1.id }
assert_response :success
assert_select 'your-selector-for-annotation', 1
assert_select 'your-selector-for-annotation-title', annotation1.title
end Replace 'your-selector-for-annotation' with the actual CSS selector used in your view to represent annotations. |
||
|
||
test 'zeus should be able to filter existing annotations on new page' do | ||
sign_in users(:zeus) | ||
get new_saved_annotation_path | ||
|
||
assert_response :success | ||
|
||
get new_saved_annotation_path, params: { exercise_id: 1 } | ||
|
||
assert_response :success | ||
end | ||
Comment on lines
+63
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor zeus test and enhance coverage The zeus test is very similar to the staff test, which suggests an opportunity for refactoring to reduce code duplication. Additionally, it has the same limitations in terms of verifying the actual filtering functionality. Consider the following improvements:
Here's a suggested refactoring and improvement: def assert_can_filter_annotations(user)
sign_in user
exercise1 = create(:exercise)
exercise2 = create(:exercise)
annotation1 = create(:saved_annotation, user: user, course: @course, exercise: exercise1)
annotation2 = create(:saved_annotation, user: user, course: @course, exercise: exercise2)
get new_saved_annotation_path
assert_response :success
assert_select 'your-selector-for-annotation', 2
get new_saved_annotation_path, params: { exercise_id: exercise1.id }
assert_response :success
assert_select 'your-selector-for-annotation', 1
assert_select 'your-selector-for-annotation-title', annotation1.title
end
test 'staff should be able to filter existing annotations on new page' do
assert_can_filter_annotations(@user)
end
test 'zeus should be able to filter existing annotations on new page' do
assert_can_filter_annotations(users(:zeus))
end This refactoring reduces duplication and ensures consistent testing across user types. Adjust the selectors and assertions as needed to match your actual view structure. |
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor join to use ActiveRecord methods instead of raw SQL
Using raw SQL in
joins
reduces readability and may introduce SQL injection risks. Consider utilizing ActiveRecord's query interface to define the join with a table alias.You can refactor the join as follows:
Since
Annotation
belongs toSubmission
, you can use the association to perform the join:If you need to reference the table with an alias due to pluralization issues, you can use
joins
with a string that's safer by leveragingsanitize_sql
:However, it's often better to adjust the query to avoid raw SQL altogether.