diff --git a/app/models/template.rb b/app/models/template.rb index ca8b8d0f..0caaf6e7 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -67,7 +67,7 @@ class Template < ApplicationRecord has_many :schema_documents, ->(e) { where(uuid: e.schema.pluck('attachment_uuid')) }, class_name: 'ActiveStorage::Attachment', dependent: :destroy, as: :record, inverse_of: :record - has_many :submissions, dependent: :destroy + has_many :submissions, dependent: :destroy, counter_cache: :submissions_count has_many :template_sharings, dependent: :destroy has_many :template_accesses, dependent: :destroy has_many :template_versions, dependent: :destroy diff --git a/app/models/template_folder.rb b/app/models/template_folder.rb index 58539a2e..226d7d52 100644 --- a/app/models/template_folder.rb +++ b/app/models/template_folder.rb @@ -51,4 +51,11 @@ class TemplateFolder < ApplicationRecord def default? name == DEFAULT_NAME end + + def submissions_count + @submissions_count ||= + Rails.cache.fetch("#{cache_key_with_version}/submissions_count", expires_in: 5.minutes) do + active_templates.sum(:submissions_count) + subfolders.sum(&:submissions_count) + end + end end diff --git a/app/views/template_folders/_folder.html.erb b/app/views/template_folders/_folder.html.erb index 8413e045..c2bb5c9d 100644 --- a/app/views/template_folders/_folder.html.erb +++ b/app/views/template_folders/_folder.html.erb @@ -3,10 +3,18 @@ <% if !is_long %> <%= svg_icon('folder', class: 'w-6 h-6') %> <% end %> -
- <% if is_long %> - <%= svg_icon('folder', class: 'w-6 h-6 inline') %> +
+
+ <% if is_long %> + <%= svg_icon('folder', class: 'w-6 h-6 inline') %> + <% end %> + <%= folder.name %> +
+ <% if folder.submissions_count > 0 %> + + <%= svg_icon('file_text', class: 'w-3 h-3') %> + <%= folder.submissions_count %> + <% end %> - <%= folder.name %>
diff --git a/app/views/templates/_template.html.erb b/app/views/templates/_template.html.erb index 2ba35ebd..7595fe0c 100644 --- a/app/views/templates/_template.html.erb +++ b/app/views/templates/_template.html.erb @@ -24,6 +24,11 @@ <%= svg_icon('folder', class: 'w-4 h-4 flex-shrink-0') %> <%= template.folder.full_name %> + <% else %> + + <%= svg_icon('file_text', class: 'w-4 h-4') %> + <%= template.submissions_count %> + <% end %>

diff --git a/db/migrate/20260510100000_add_submissions_count_to_templates.rb b/db/migrate/20260510100000_add_submissions_count_to_templates.rb new file mode 100644 index 00000000..c16cbb8e --- /dev/null +++ b/db/migrate/20260510100000_add_submissions_count_to_templates.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class AddSubmissionsCountToTemplates < ActiveRecord::Migration[7.2] + def up + add_column :templates, :submissions_count, :integer, default: 0, null: false + + # Backfill existing counts + execute <<~SQL.squish + UPDATE templates + SET submissions_count = ( + SELECT COUNT(*) + FROM submissions + WHERE submissions.template_id = templates.id + ) + SQL + end + + def down + remove_column :templates, :submissions_count + end +end \ No newline at end of file diff --git a/spec/system/dashboard_spec.rb b/spec/system/dashboard_spec.rb index beca9606..1c51d051 100644 --- a/spec/system/dashboard_spec.rb +++ b/spec/system/dashboard_spec.rb @@ -36,6 +36,15 @@ RSpec.describe 'Dashboard Page' do expect(page).to have_link('Create', href: new_template_path) end + it 'shows submission count for templates' do + create(:submission, template: templates[0]) + + visit root_path + + expect(page).to have_content('0') + expect(page).to have_content('1') + end + it 'initializes the template creation process' do click_link 'Create'