Skip to content

Commit d86c50c

Browse files
committed
fix: Update progress calculation to count all person checklist items; improve existing item ID retrieval in specs
1 parent 9cc5272 commit d86c50c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

app/models/better_together/checklist.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def completion_percentage_for(person)
3535
total = checklist_items.count
3636
return 0 if total.zero?
3737

38-
completed = person_checklist_items.where(person:, done: true).count
38+
completed = person_checklist_items.where(person:).count
3939
((completed.to_f / total) * 100).round
4040
end
4141

db/migrate/20250901203002_add_children_count_to_checklist_items.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ def change
1717

1818
private
1919

20-
def backfill_children_count
21-
sql = "UPDATE better_together_checklist_items parent SET children_count = sub.count FROM (SELECT parent_id, COUNT(*) as count FROM better_together_checklist_items WHERE parent_id IS NOT NULL GROUP BY parent_id) AS sub WHERE parent.id = sub.parent_id" # rubocop:disable Layout/LineLength
22-
execute(sql)
20+
def backfill_children_count # rubocop:todo Metrics/MethodLength
21+
execute(<<-SQL.squish)
22+
UPDATE better_together_checklist_items parent
23+
SET children_count = sub.count
24+
FROM (
25+
SELECT parent_id, COUNT(*) as count
26+
FROM better_together_checklist_items
27+
WHERE parent_id IS NOT NULL
28+
GROUP BY parent_id
29+
) AS sub
30+
WHERE parent.id = sub.parent_id
31+
SQL
2332
end
2433
end

spec/requests/better_together/checklist_items_nested_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
let(:checklist) { create(:better_together_checklist) }
77
# create a few items
88
let!(:parent) { create(:better_together_checklist_item, checklist: checklist) }
9+
# capture existing item ids so newly created items can be found without relying on a `label` column
10+
let!(:existing_item_ids) { checklist.checklist_items.pluck(:id) }
911

1012
context 'when creating a nested child' do
1113
before do
@@ -26,7 +28,8 @@
2628
end
2729

2830
let(:created) do
29-
BetterTogether::ChecklistItem.where(checklist: checklist, parent: parent).find_by(label: 'nested child') ||
31+
BetterTogether::ChecklistItem.where(checklist: checklist,
32+
parent: parent).where.not(id: existing_item_ids).first ||
3033
BetterTogether::ChecklistItem.i18n.find_by(label: 'nested child')
3134
end
3235

@@ -39,7 +42,7 @@
3942
end
4043
end
4144

42-
context 'when reordering siblings' do
45+
context 'when reordering siblings' do # rubocop:todo RSpec/MultipleMemoizedHelpers
4346
let!(:a) { create(:better_together_checklist_item, checklist: checklist, parent: parent, position: 0) }
4447
let!(:b) { create(:better_together_checklist_item, checklist: checklist, parent: parent, position: 1) }
4548
let!(:top) { create(:better_together_checklist_item, checklist: checklist, position: 0) }
@@ -81,7 +84,7 @@
8184
end
8285

8386
let(:created_localized) do
84-
BetterTogether::ChecklistItem.where(checklist: checklist, parent: parent).find_by(label: 'localized child')
87+
BetterTogether::ChecklistItem.where(checklist: checklist, parent: parent).where.not(id: existing_item_ids).first
8588
end
8689

8790
it 'creates a localized child record' do

0 commit comments

Comments
 (0)