diff --git a/app/furniture/journal/entry.rb b/app/furniture/journal/entry.rb index 2670b2731..958f6ab1e 100644 --- a/app/furniture/journal/entry.rb +++ b/app/furniture/journal/entry.rb @@ -30,7 +30,7 @@ class Entry < ApplicationRecord belongs_to :journal, inverse_of: :entries has_one :room, through: :journal has_one :space, through: :journal - after_save :extract_keywords, if: :saved_change_to_body? + before_save :extract_keywords, if: :will_save_change_to_body? def published? published_at.present? @@ -52,7 +52,7 @@ def self.renderer end def extract_keywords - journal.keywords.extract_and_create_from!(body) + self.keywords = journal.keywords.extract_and_create_from!(body).pluck(:canonical_keyword) end def to_param diff --git a/app/furniture/journal/keyword.rb b/app/furniture/journal/keyword.rb index bff92119e..41bcc69d4 100644 --- a/app/furniture/journal/keyword.rb +++ b/app/furniture/journal/keyword.rb @@ -7,9 +7,11 @@ class Keyword < ApplicationRecord belongs_to :journal, inverse_of: :keywords def self.extract_and_create_from!(body) - body.scan(/#(\w+)/)&.flatten&.each do |keyword| - next if where(":aliases = ANY (aliases)", aliases: keyword) - .or(where(canonical_keyword: keyword)).exists? + body.scan(/#(\w+)/)&.flatten&.map do |keyword| + existing_keyword = where(":aliases = ANY (aliases)", aliases: keyword) + .or(where(canonical_keyword: keyword)).first + + next existing_keyword if existing_keyword.present? find_or_create_by!(canonical_keyword: keyword) end diff --git a/db/migrate/20230713230334_journal_add_keywords_to_entries.rb b/db/migrate/20230713230334_journal_add_keywords_to_entries.rb new file mode 100644 index 000000000..aad25704e --- /dev/null +++ b/db/migrate/20230713230334_journal_add_keywords_to_entries.rb @@ -0,0 +1,5 @@ +class JournalAddKeywordsToEntries < ActiveRecord::Migration[7.0] + def change + add_column :journal_entries, :keywords, :string, array: true + end +end diff --git a/db/schema.rb b/db/schema.rb index a52718609..5c55faff5 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[7.0].define(version: 2023_07_06_003709) do +ActiveRecord::Schema[7.0].define(version: 2023_07_13_230334) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -114,6 +114,7 @@ t.datetime "published_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "keywords", array: true t.index ["journal_id"], name: "index_journal_entries_on_journal_id" end diff --git a/spec/furniture/journal/entry_spec.rb b/spec/furniture/journal/entry_spec.rb index f8ee56790..14c2ca16d 100644 --- a/spec/furniture/journal/entry_spec.rb +++ b/spec/furniture/journal/entry_spec.rb @@ -25,7 +25,7 @@ let(:journal) { entry.journal } context "when the body is changing" do - it "idempotently creates `Keywords` in the `Journal`" do + it "idempotently creates `Keywords` in the `Journal` and `Entry`" do bad_apple = entry.journal.keywords.create!(canonical_keyword: "BadApple", aliases: ["BadApples"]) good_times = entry.journal.keywords.find_by!(canonical_keyword: "GoodTimes") expect do @@ -35,6 +35,7 @@ expect(journal.keywords.where(canonical_keyword: "GoodTimes")).to exist expect(journal.keywords.where(canonical_keyword: "HardCider")).to exist expect(journal.keywords.where(canonical_keyword: "BadApples")).not_to exist + expect(entry.reload.keywords).to eq(["GoodTimes", "HardCider", "BadApple"]) end end end