Skip to content

Commit 72049a1

Browse files
committed
save document word count updates, too
1 parent b087c42 commit 72049a1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

app/jobs/save_document_revision_job.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def perform(*args)
1111
new_word_count = document.computed_word_count
1212
document.update(cached_word_count: new_word_count)
1313

14+
# Save a WordCountUpdate for this document for today
15+
update = document.word_count_updates.find_or_initialize_by(
16+
for_date: DateTime.current,
17+
)
18+
update.word_count = new_word_count
19+
update.user_id ||= document.user_id
20+
update.save!
21+
1422
# Make sure we're only storing revisions at least every 5 min
1523
latest_revision = document.document_revisions.order('created_at DESC').limit(1).first
1624
if latest_revision.present? && latest_revision.created_at > 5.minutes.ago

app/models/documents/document.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class Document < ApplicationRecord
2626

2727
attr_accessor :tagged_text
2828

29+
# Duplicated from is_content_page since we don't include that here yet
30+
has_many :word_count_updates, as: :entity, dependent: :destroy
31+
def latest_word_count_cache
32+
word_count_updates.order('for_date DESC').limit(1).first.try(:word_count) || 0
33+
end
34+
2935
KEYS_TO_TRIGGER_REVISION_ON_CHANGE = %w(title body synopsis notes_text)
3036

3137
def self.color

0 commit comments

Comments
 (0)