From 43525456563c2764012c6423f1d72da8bc350906 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 1 Jul 2021 22:51:20 -0700 Subject: [PATCH] cache word counts on revisions + polish --- app/jobs/save_document_revision_job.rb | 14 ++++++++------ app/views/data/documents.html.erb | 6 ++++-- ..._add_cached_word_count_to_document_revisions.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20210702054655_add_cached_word_count_to_document_revisions.rb diff --git a/app/jobs/save_document_revision_job.rb b/app/jobs/save_document_revision_job.rb index 3fb83f7f..d52d737f 100644 --- a/app/jobs/save_document_revision_job.rb +++ b/app/jobs/save_document_revision_job.rb @@ -8,7 +8,8 @@ class SaveDocumentRevisionJob < ApplicationJob return unless document.present? # Update cached word count for the document regardless of how often this is called - document.update(cached_word_count: document.computed_word_count) + new_word_count = document.computed_word_count + document.update(cached_word_count: new_word_count) # Make sure we're only storing revisions at least every 5 min latest_revision = document.document_revisions.order('created_at DESC').limit(1).first @@ -18,11 +19,12 @@ class SaveDocumentRevisionJob < ApplicationJob # Store the document information as-is document.document_revisions.create!( - title: document.title, - body: document.body, - synopsis: document.synopsis, - universe_id: document.universe_id, - notes_text: document.notes_text + title: document.title, + body: document.body, + synopsis: document.synopsis, + universe_id: document.universe_id, + notes_text: document.notes_text, + cached_word_count: new_word_count ) end end \ No newline at end of file diff --git a/app/views/data/documents.html.erb b/app/views/data/documents.html.erb index 7fbb2b0a..7a022165 100644 --- a/app/views/data/documents.html.erb +++ b/app/views/data/documents.html.erb @@ -108,7 +108,8 @@ }, title: "Last 45 days", ytitle: "minutes", - colors: [Document.color] + colors: [Document.color], + suffix: " minutes" %>
<%= @@ -117,7 +118,8 @@ }, title: "All time", ytitle: "minutes", - colors: [Document.color] + colors: [Document.color], + suffix: " minutes" %> diff --git a/db/migrate/20210702054655_add_cached_word_count_to_document_revisions.rb b/db/migrate/20210702054655_add_cached_word_count_to_document_revisions.rb new file mode 100644 index 00000000..9056762b --- /dev/null +++ b/db/migrate/20210702054655_add_cached_word_count_to_document_revisions.rb @@ -0,0 +1,5 @@ +class AddCachedWordCountToDocumentRevisions < ActiveRecord::Migration[6.0] + def change + add_column :document_revisions, :cached_word_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 08c9a272..06b4055d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_07_01_002938) do +ActiveRecord::Schema.define(version: 2021_07_02_054655) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false @@ -1174,6 +1174,7 @@ ActiveRecord::Schema.define(version: 2021_07_01_002938) do t.datetime "deleted_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "cached_word_count" t.index ["document_id"], name: "index_document_revisions_on_document_id" end