Skip to content

Commit

Permalink
Add status filter to user's note page
Browse files Browse the repository at this point in the history
  • Loading branch information
kcne committed Nov 6, 2024
1 parent 9b9e857 commit 337d37e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions app/views/notes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
:commented => tag.span(t(".subheading_commented"), :class => "px-2 py-1 bg-body") %></p>
<% end %>

<%= form_with :url => url_for(:controller => "notes", :action => "index"), :method => :get, :data => { :turbo => true } do %>
<div class="row gx-2 align-items-end">
<div class="col-sm-auto mb-3">
<%= 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" %>
</div>
<div class="col-sm-auto mb-3">
<%= submit_tag t(".apply"), :class => "btn btn-primary" %>
</div>
</div>
<% end %>

<% if @notes.empty? %>
<h4><%= t ".no_notes" %></h4>

Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions test/models/note_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 337d37e

Please sign in to comment.