Skip to content

Commit

Permalink
Show Entries that match a Keyword or it's Aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Jul 15, 2023
1 parent 0fff6e2 commit 8bcca39
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions app/furniture/journal/keyword.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Journal
class Keyword < ApplicationRecord
location(parent: :journal)
extend StripsNamespaceFromModelName

self.table_name = "journal_keywords"
validates :canonical_keyword, presence: true, uniqueness: {case_sensitive: false, scope: :journal_id}
Expand All @@ -11,6 +12,10 @@ class Keyword < ApplicationRecord
.or(where("lower(canonical_keyword) IN (?)", keywords.map(&:downcase)))
end)

def entries
journal.entries.where("keywords::text[] && ARRAY[?]::text[]", [canonical_keyword] + (aliases.presence || []))
end

def self.extract_and_create_from!(body)
body.scan(/#(\w+)/)&.flatten&.map do |keyword|
existing_keyword = search(keyword).first
Expand Down
17 changes: 17 additions & 0 deletions app/furniture/journal/keyword_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class Journal
class KeywordPolicy < ApplicationPolicy
alias_method :keyword, :object

def show?
true
end

class Scope < ApplicationScope
def resolve
scope
end
end
end
end
14 changes: 12 additions & 2 deletions app/furniture/journal/keywords/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
<%= keyword.canonical_keyword %>
<%= keyword.aliases %>
<h1><%= keyword.canonical_keyword %></h1>
<ul>
<%= keyword&.aliases&.each do |aliass| %>
<li><%= aliass %></li>
<% end %>
</ul>

<ul>
<%- policy_scope(keyword.entries).each do |entry|%>
<li><%= link_to(entry.headline, entry.location) %></li>
<%- end %>
</ul>
7 changes: 5 additions & 2 deletions app/furniture/journal/keywords_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Journal
class KeywordsController < Controller
expose(:keyword, scope: -> { policy_scope(journal.keywords) })
class KeywordsController < FurnitureController
expose(:keyword, scope: -> { policy_scope(journal.keywords) }, model: Keyword,
find: ->(id, scope) { scope.find_by(canonical_keyword: id) })

expose(:journal, -> { Journal.find(params[:journal_id]) })
def show
authorize(keyword)
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/furniture/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
end

factory :journal_keyword, class: "Journal::Keyword" do
canonical_keyword { Faker::Fantasy::Tolkien.location }
journal
end
end
16 changes: 2 additions & 14 deletions spec/furniture/journal/keywords_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,8 @@
response
end

context "when the :id is the keyword's id" do
it { is_expected.to be_ok }
let(:keyword) { create(:journal_keyword) }

it "has a canonical entry in the head to the canonical keyword based url"
end

context "when the :id is a canonical keyword" do
it { is_expected.to be_ok }
end

context "when the :id is an alias" do
it { is_expected.to be_ok }

it "has a canonical entry in the head to the canonical keyword based url"
end
it { is_expected.to be_ok }
end
end

0 comments on commit 8bcca39

Please sign in to comment.