From 337d37e71d1070ffe729ab6b7c6d877dc7cce203 Mon Sep 17 00:00:00 2001
From: Emin Kocan
Date: Wed, 30 Oct 2024 22:00:59 +0100
Subject: [PATCH] Add status filter to user's note page
---
app/controllers/notes_controller.rb | 3 ++-
app/views/notes/index.html.erb | 14 ++++++++++
config/locales/en.yml | 5 ++++
test/models/note_test.rb | 41 +++++++++++++++++++++++++++++
4 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb
index 26d27692e5..b41a18e83b 100644
--- a/app/controllers/notes_controller.rb
+++ b/app/controllers/notes_controller.rb
@@ -18,12 +18,13 @@ class NotesController < ApplicationController
def index
param! :page, Integer, :min => 1
- @params = params.permit(:display_name)
+ @params = params.permit(:display_name, :status)
@title = t ".title", :user => @user.display_name
@page = (params[:page] || 1).to_i
@page_size = 10
@notes = @user.notes
@notes = @notes.visible unless current_user&.moderator?
+ @notes = @notes.where(status: params[:status]) if params[:status].present? && params[:status] != "all"
@notes = @notes.order("updated_at DESC, id").distinct.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
render :layout => "site"
diff --git a/app/views/notes/index.html.erb b/app/views/notes/index.html.erb
index d5efe0d79c..11837155a9 100644
--- a/app/views/notes/index.html.erb
+++ b/app/views/notes/index.html.erb
@@ -6,6 +6,20 @@
:commented => tag.span(t(".subheading_commented"), :class => "px-2 py-1 bg-body") %>
<% end %>
+<%= form_with :url => url_for(:controller => "notes", :action => "index"), :method => :get, :data => { :turbo => true } do %>
+
+
+ <%= label_tag :status, t(".status") %>
+ <%= select_tag :status,
+ options_for_select([[t(".all"), "all"], [t(".open"), "open"], [t(".closed"), "closed"]], params[:status] || "all"),
+ :class => "form-select" %>
+
+
+ <%= submit_tag t(".apply"), :class => "btn btn-primary" %>
+
+
+<% end %>
+
<% if @notes.empty? %>
<%= t ".no_notes" %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index f68488c09c..3f2a4cb140 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2966,6 +2966,11 @@ en:
description: "Description"
created_at: "Created at"
last_changed: "Last changed"
+ apply: "Apply"
+ all: "All"
+ open: "Open"
+ closed: "Closed"
+ status: "Status"
show:
title: "Note: %{id}"
description: "Description"
diff --git a/test/models/note_test.rb b/test/models/note_test.rb
index 34b16c19d5..955be83019 100644
--- a/test/models/note_test.rb
+++ b/test/models/note_test.rb
@@ -71,4 +71,45 @@ def test_lat_lon_format
assert_equal "0.0000400", note.lat.to_s
assert_equal "0.0000800", note.lon.to_s
end
+
+ def test_index_filter_by_status
+ user = create(:user)
+ other_user = create(:user)
+
+ open_note = create(:note, status: "open", user: user)
+ closed_note = create(:note, status: "closed", user: user)
+ hidden_note = create(:note, status: "hidden", user: user)
+
+ commented_note = create(:note, status: "open", user: other_user) do |note|
+ create(:note_comment, note: note, author: user)
+ end
+
+ get user_notes_path(user, status: "all")
+ assert_response :success
+ assert_select "table.note_list tbody tr", count: 3
+ assert_select "tr[data-note-id='#{open_note.id}']"
+ assert_select "tr[data-note-id='#{closed_note.id}']"
+ assert_select "tr[data-note-id='#{commented_note.id}']"
+ assert_select "tr[data-note-id='#{hidden_note.id}']", count: 0
+
+ get user_notes_path(user, status: "open")
+ assert_response :success
+ assert_select "table.note_list tbody tr", count: 2
+ assert_select "tr[data-note-id='#{open_note.id}']"
+ assert_select "tr[data-note-id='#{commented_note.id}']"
+ assert_select "tr[data-note-id='#{closed_note.id}']", count: 0
+
+ get user_notes_path(user, status: "closed")
+ assert_response :success
+ assert_select "table.note_list tbody tr", count: 1
+ assert_select "tr[data-note-id='#{closed_note.id}']"
+ assert_select "tr[data-note-id='#{open_note.id}']", count: 0
+
+ get user_notes_path(user)
+ assert_response :success
+ assert_select "table.note_list tbody tr", count: 3
+ assert_select "tr[data-note-id='#{open_note.id}']"
+ assert_select "tr[data-note-id='#{closed_note.id}']"
+ assert_select "tr[data-note-id='#{commented_note.id}']"
+ end
end