Skip to content

Commit

Permalink
Journal: Entries link to their Keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Jul 20, 2023
1 parent cde1583 commit 4714a83
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/furniture/journal/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def keywords=(keywords)
super(keywords.uniq)
end

def keywords
journal.keywords.search(*super)
end

def to_param
slug
end
Expand Down
15 changes: 13 additions & 2 deletions app/furniture/journal/entry_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ def initialize(*args, entry:, **kwargs)
end

def body_html
render_markdown(entry.body)
postprocess(render_markdown(entry.body))
end

def published_at
private def postprocess(text)
entry.keywords.map do |keyword|
search = keyword.canonical_with_aliases.sort.reverse.join("|")
text.gsub!(/\#(#{search})/i) do |match|
link_to(match, keyword.location)
end
end

text
end

private def published_at
entry.published_at.to_fs(:long_ordinal)
end

Expand Down
6 changes: 5 additions & 1 deletion app/furniture/journal/keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class Keyword < ApplicationRecord
end)

def entries
journal.entries.matching_keywords([canonical_keyword] + (aliases.presence || []))
journal.entries.matching_keywords(canonical_with_aliases)
end

def canonical_with_aliases
[canonical_keyword] + (aliases.presence || [])
end

def to_param
Expand Down
1 change: 1 addition & 0 deletions spec/furniture/journal/entry_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
let(:entry) { create(:journal_entry, body: "https://www.google.com @[email protected] #GoodTimes") }

it { is_expected.to have_link("https://www.google.com", href: "https://www.google.com") }
it { is_expected.to have_link("#GoodTimes", href: polymorphic_path(entry.journal.keywords.find_by(canonical_keyword: "GoodTimes").location)) }
end

0 comments on commit 4714a83

Please sign in to comment.