Skip to content

Display documents count on Topic index (no counter cache) #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ group :development do
gem "bullet"
gem "hotwire-spark"
gem "letter_opener"
gem "rack-mini-profiler"
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
end
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ GEM
raabro (1.4.0)
racc (1.8.1)
rack (3.1.16)
rack-mini-profiler (4.0.0)
rack (>= 1.2.0)
rack-session (2.1.1)
base64 (>= 0.1.0)
rack (>= 3.0.0)
Expand Down Expand Up @@ -519,6 +521,7 @@ DEPENDENCIES
pg (~> 1.1)
propshaft
puma (>= 5.0)
rack-mini-profiler
rails (~> 8.0.1)
rails-controller-testing
requestjs-rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TopicsController < ApplicationController
before_action :set_topic, only: [ :show, :edit, :tags, :update, :destroy, :archive ]

def index
@pagy, @topics = pagy(scope.search_with_params(search_params))
@pagy, @topics = pagy(scope.includes(:documents_attachments).search_with_params(search_params))
@available_providers = other_available_providers
@languages = scope.map(&:language).uniq.sort_by(&:name)
end
Expand Down
1 change: 1 addition & 0 deletions app/views/topics/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<td class="text-bold-500"><%= topic.uid.truncate(10, omission: "...") if topic.uid %></td>
<td class="text-bold-500"><%= topic.language.name %></td>
<td class="text-bold-500"><%= topic.provider.name %></td>
<td class="text-bold-500"><%= topic.documents.size %></td>
<td class="text-bold-500"><%= topic.state %></td>
<td class="text-end">
<%= link_to topic, class: "btn btn-primary btn-sm", data: {turbo: false } do %>
Expand Down
1 change: 1 addition & 0 deletions app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<th>UID</th>
<th>Language</th>
<th>Provider</th>
<th>Training<br>Materials</th>
<th>State</th>
<th class="text-end">Actions</th>
</tr>
Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@

# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
# config.generators.apply_rubocop_autocorrect_after_generate!
Bullet.add_safelist type: :unused_eager_loading, class_name: "Topic", association: :documents_attachments
end
8 changes: 8 additions & 0 deletions spec/requests/topics/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
expect(assigns(:topics)).to eq([ topic ])
end

it "displays the number of documents" do
topic = create(:topic, :with_documents, provider:)

get topics_url

expect(page).to have_css("td", text: "1")
end

describe "searching" do
context "with a query" do
it "filters topics by query" do
Expand Down