Skip to content

Commit

Permalink
Add test to controller and update scopes and filtering to handle edge…
Browse files Browse the repository at this point in the history
… cases
  • Loading branch information
kcne committed Oct 9, 2024
1 parent 410ff94 commit 2e18a7c
Show file tree
Hide file tree
Showing 5 changed files with 1,675 additions and 171 deletions.
4 changes: 2 additions & 2 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def index
@title = t(".title", :user => @user.display_name)
@page = (params[:page] || 1).to_i
@page_size = 10

@notes = @notes.visible unless current_user&.moderator?
@notes = @user.notes
.filter_hidden_notes(current_user)
# .filter_hidden_notes(current_user)
.filter_by_status(params[:status])
.filter_by_note_type(params[:note_type], @user.id)
.filter_by_date_range(params[:from], params[:to])
Expand Down
8 changes: 4 additions & 4 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class Note < ApplicationRecord
case note_type
when "commented"
joins(:comments)
.where("notes.id IN (SELECT note_id FROM note_comments WHERE author_id != ?)", user_id)
.where("notes.id IN (SELECT note_id FROM note_comments WHERE author_id != ? OR author_id IS NULL)", user_id)
.distinct
when "submitted"
joins(:comments)
.where(:note_comments => { :author_id => user_id })
.where("note_comments.id = (SELECT MIN(id) FROM note_comments WHERE note_comments.note_id = notes.id)")
.where("note_comments.id = (SELECT MIN(nc.id) FROM note_comments nc WHERE nc.note_id = notes.id)")
.distinct
else
all
Expand All @@ -72,8 +72,8 @@ class Note < ApplicationRecord

scope :filter_by_date_range, lambda { |from, to|
notes = all
notes = notes.where(:notes => { :created_at => DateTime.parse(from).. }) if from.present?
notes = notes.where(:notes => { :created_at => ..DateTime.parse(to) }) if to.present?
notes = notes.where(:created_at => DateTime.parse(from).beginning_of_day..) if from.present?
notes = notes.where(:created_at => ..DateTime.parse(to).end_of_day) if to.present?
notes
}

Expand Down
Loading

0 comments on commit 2e18a7c

Please sign in to comment.