diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 4af612824e..37f04bb814 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -13,7 +13,7 @@ class NotesController < ApplicationController before_action :set_locale around_action :web_timeout - ### Display a list of notes by a specified user + # Display a list of notes by a specified user def index param! :page, Integer, :min => 1 @@ -21,7 +21,6 @@ def index @title = t(".title", :user => @user.display_name) @page = (params[:page] || 1).to_i @page_size = 10 - @notes = @user.notes .filter_hidden_notes(current_user) .filter_by_status(params[:status]) diff --git a/app/models/note.rb b/app/models/note.rb index 9d40724c6c..4e965d8d4e 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -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 @@ -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 } diff --git a/app/views/notes/index.html.erb b/app/views/notes/index.html.erb index 542fd39d74..639fcc6146 100644 --- a/app/views/notes/index.html.erb +++ b/app/views/notes/index.html.erb @@ -15,15 +15,13 @@
<%= label_tag :status, t(".status") %> <%= select_tag :status, - options_for_select([[t(".all"), "all"], [t(".open"), "open"], [t(".closed"), "closed"]], params[:status]), - :include_blank => t(".select_status"), + options_for_select([[t(".all"), "all"], [t(".open"), "open"], [t(".closed"), "closed"]], params[:status] || "all"), :class => "form-select" %>
- <%= label_tag :note_type, t(".note_type") %> + <%= label_tag :note_type, t(".interaction_type") %> <%= select_tag :note_type, - options_for_select([[t(".all_types"), ""], [t(".submitted"), "submitted"], [t(".commented"), "commented"]], params[:note_type]), - :include_blank => t(".select_note_type"), + options_for_select([[t(".all"), ""], [t(".submitted"), "submitted"], [t(".commented"), "commented"]], params[:note_type] || "all"), :class => "form-select" %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index faa09a34ea..6b7f12aa46 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2973,12 +2973,9 @@ en: from: "From" to: "To" status: "Status" - select_status: "Select Status" - note_type: "Note Type" - all_types: "All Types" + interaction_type: "Interaction Type" submitted: "Submitted" commented: "Commented" - select_note_type: "Select Note Type" show: title: "Note: %{id}" description: "Description" diff --git a/test/controllers/notes_controller_test.rb b/test/controllers/notes_controller_test.rb index 4092ad7326..11408f1a61 100644 --- a/test/controllers/notes_controller_test.rb +++ b/test/controllers/notes_controller_test.rb @@ -45,6 +45,7 @@ def test_index_success assert_select ".content-heading a[href='#{user_path first_user}']", :text => first_user.display_name assert_select "table.note_list tbody tr", :count => 1 + # Check for a regular user (second user) get user_notes_path(second_user) assert_response :success assert_select ".content-heading a[href='#{user_path second_user}']", :text => second_user.display_name @@ -55,10 +56,6 @@ def test_index_success session_for(moderator_user) - get user_notes_path(first_user) - assert_response :success - assert_select "table.note_list tbody tr", :count => 1 - get user_notes_path(second_user) assert_response :success assert_select "table.note_list tbody tr", :count => 2 @@ -184,4 +181,127 @@ def test_new_note assert_template "notes/new" assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0 end + + def test_index_filter_by_status + user = create(:user) + + open_note = create(:note, :status => "open") + create(:note_comment, :note => open_note, :author => user) + + closed_note = create(:note, :status => "closed") + create(:note_comment, :note => closed_note, :author => user) + + get user_notes_path(user), :params => { :status => "open" } + assert_response :success + assert_select "table.note_list tbody tr", :count => 1 + + get user_notes_path(user), :params => { :status => "closed" } + assert_response :success + assert_select "table.note_list tbody tr", :count => 1 + end + + def test_index_filter_by_note_type + user = create(:user) + other_user = create(:user) + anonymous_user = nil + + submitted_note = create(:note) + create(:note_comment, :note => submitted_note, :author => user) + + commented_note = create(:note) + create(:note_comment, :note => commented_note, :author => other_user) + create(:note_comment, :note => commented_note, :author => user) + + anonymous_commented_note = create(:note) + create(:note_comment, :note => anonymous_commented_note, :author => anonymous_user) + create(:note_comment, :note => anonymous_commented_note, :author => user) + + get user_notes_path(user), :params => { :note_type => "submitted" } + assert_response :success + assert_select "table.note_list tbody tr", :count => 1 + assert_select "table.note_list tbody tr", :text => /#{submitted_note.id}/ + + get user_notes_path(user), :params => { :note_type => "commented" } + assert_response :success + assert_select "table.note_list tbody tr", :count => 2 + assert_select "table.note_list tbody tr", :text => /#{commented_note.id}/ + assert_select "table.note_list tbody tr", :text => /#{anonymous_commented_note.id}/ + + get user_notes_path(user) + assert_response :success + assert_select "table.note_list tbody tr", :count => 3 + assert_select "table.note_list tbody tr", :text => /#{submitted_note.id}/ + assert_select "table.note_list tbody tr", :text => /#{commented_note.id}/ + assert_select "table.note_list tbody tr", :text => /#{anonymous_commented_note.id}/ + end + + def test_index_filter_by_date_range + user = create(:user) + + old_note = create(:note, :created_at => 1.year.ago) + create(:note_comment, :note => old_note, :author => user) + + recent_note = create(:note, :created_at => 1.day.ago) + create(:note_comment, :note => recent_note, :author => user) + + middle_note = create(:note, :created_at => 6.months.ago) + create(:note_comment, :note => middle_note, :author => user) + + very_recent_note = create(:note, :created_at => 2.hours.ago) + create(:note_comment, :note => very_recent_note, :author => user) + + # Filter for notes created between 1 year ago + 1 day and 1 month ago (should only include middle_note) + get user_notes_path(user), :params => { :from => (1.year.ago + 1.day).to_date, :to => 1.month.ago.end_of_month.to_date } + assert_response :success + assert_select "table.note_list tbody tr", :count => 1 + assert_select "table.note_list tbody tr", :text => /#{middle_note.id}/ + end + + def test_index_sort_by_params + user = create(:user) + + older_note = create(:note) + create(:note_comment, :note => older_note, :author => user) + older_note.updated_at = 2.days.ago + older_note.created_at = 5.days.ago + older_note.save! + + newer_note = create(:note) + create(:note_comment, :note => newer_note, :author => user) + newer_note.updated_at = 13.minutes.ago + newer_note.created_at = 3.days.ago + newer_note.save! + + middle_note = create(:note) + create(:note_comment, :note => middle_note, :author => user) + middle_note.updated_at = 1.day.ago + middle_note.created_at = 4.days.ago + middle_note.save! + + very_recent_note = create(:note) + create(:note_comment, :note => very_recent_note, :author => user) + very_recent_note.updated_at = 5.minutes.ago + very_recent_note.created_at = 1.day.ago + very_recent_note.save! + + get user_notes_path(user), :params => { :sort_by => "updated_at", :sort_order => "asc" } + assert_response :success + assert_select "table.note_list tbody tr:first-child td:nth-child(6) time", :text => /2 days ago/ + assert_select "table.note_list tbody tr:last-child td:nth-child(6) time", :text => /5 minutes ago/ + + get user_notes_path(user), :params => { :sort_by => "updated_at", :sort_order => "desc" } + assert_response :success + assert_select "table.note_list tbody tr:first-child td:nth-child(6) time", :text => /5 minutes ago/ + assert_select "table.note_list tbody tr:last-child td:nth-child(6) time", :text => /2 days ago/ + + get user_notes_path(user), :params => { :sort_by => "created_at", :sort_order => "asc" } + assert_response :success + assert_select "table.note_list tbody tr:first-child td:nth-child(5) time", :text => /5 days ago/ + assert_select "table.note_list tbody tr:last-child td:nth-child(5) time", :text => /1 day ago/ + # created at + get user_notes_path(user), :params => { :sort_by => "created_at", :sort_order => "desc" } + assert_response :success + assert_select "table.note_list tbody tr:first-child td:nth-child(5) time", :text => /1 day ago/ + assert_select "table.note_list tbody tr:last-child td:nth-child(5) time", :text => /5 days ago/ + end end