Skip to content

Commit

Permalink
Journal: Entries may be queried by Keywords
Browse files Browse the repository at this point in the history
- https://github.com/zinc-collective/convene/issues/1662
- https://github.com/zinc-collective/convene/issues/1566

This gets us a little bit closer to being able to browse `Entry` by
`Keyword`s, since each `Entry` will know it's keywords
  • Loading branch information
zspencer committed Jul 13, 2023
1 parent 7d965ad commit 6f9a625
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/furniture/journal/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions app/furniture/journal/keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230713230334_journal_add_keywords_to_entries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class JournalAddKeywordsToEntries < ActiveRecord::Migration[7.0]
def change
add_column :journal_entries, :keywords, :string, array: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/furniture/journal/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6f9a625

Please sign in to comment.