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 Oct 31, 2024
1 parent 9b9e857 commit 0d0045e
Show file tree
Hide file tree
Showing 5 changed files with 52 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.with_status(params[:status])
@notes = @notes.order("updated_at DESC, id").distinct.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)

render :layout => "site"
Expand Down
10 changes: 10 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class Note < ApplicationRecord

scope :visible, -> { where.not(:status => "hidden") }
scope :invisible, -> { where(:status => "hidden") }
scope :with_status, lambda { |status|
case status
when "open"
where(:status => "open")
when "closed"
where(:status => "closed")
else
all
end
}

after_initialize :set_defaults

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
21 changes: 21 additions & 0 deletions test/models/note_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ def test_lat_lon_format
assert_equal "0.0000400", note.lat.to_s
assert_equal "0.0000800", note.lon.to_s
end

def test_with_status_scope
open_note = create(:note, :status => "open")
closed_note = create(:note, :status => "closed")
reopened_note = create(:note, :status => "open", :closed_at => 2.days.ago)

filtered_notes = Note.with_status("open")
assert_includes filtered_notes, open_note
assert_includes filtered_notes, reopened_note
assert_not_includes filtered_notes, closed_note

filtered_notes = Note.with_status("closed")
assert_includes filtered_notes, closed_note
assert_not_includes filtered_notes, open_note
assert_not_includes filtered_notes, reopened_note

filtered_notes = Note.with_status(nil)
assert_includes filtered_notes, open_note
assert_includes filtered_notes, closed_note
assert_includes filtered_notes, reopened_note
end
end

0 comments on commit 0d0045e

Please sign in to comment.