Skip to content
Open
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
2 changes: 1 addition & 1 deletion app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 :dynamic_documents, dependent: :destroy
Expand Down
7 changes: 7 additions & 0 deletions app/models/template_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ def full_name
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
16 changes: 12 additions & 4 deletions app/views/template_folders/_folder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
<% if !is_long %>
<%= svg_icon('folder', class: 'w-6 h-6') %>
<% end %>
<div class="text-lg font-semibold mt-1" style="overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: <%= is_long ? 2 : 1 %>;">
<% if is_long %>
<%= svg_icon('folder', class: 'w-6 h-6 inline') %>
<div class="text-lg font-semibold mt-1 flex items-center justify-between" style="overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: <%= is_long ? 2 : 1 %>;">
<div>
<% if is_long %>
<%= svg_icon('folder', class: 'w-6 h-6 inline') %>
<% end %>
<%= folder.name %>
</div>
<% if folder.submissions_count > 0 %>
<span class="text-xs text-base-content/60 flex items-center space-x-1">
<%= svg_icon('file_text', class: 'w-3 h-3') %>
<span><%= folder.submissions_count %></span>
</span>
<% end %>
<%= folder.name %>
</div>
</a>
5 changes: 5 additions & 0 deletions app/views/templates/_template.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<%= svg_icon('folder', class: 'w-4 h-4 flex-shrink-0') %>
<span class="truncate"><%= template.folder.full_name %></span>
</span>
<% else %>
<span class="flex items-center space-x-1 w-1/2 justify-end">
<%= svg_icon('file_text', class: 'w-4 h-4') %>
<span><%= template.submissions_count %></span>
</span>
<% end %>
</p>
</div>
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20260510100000_add_submissions_count_to_templates.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/system/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
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'

Expand Down