From 8bcca394b5a6a99ce942a9a20311d0d1541557de Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Fri, 14 Jul 2023 18:45:12 -0700 Subject: [PATCH] Show Entries that match a Keyword or it's Aliases --- app/furniture/journal/keyword.rb | 5 +++++ app/furniture/journal/keyword_policy.rb | 17 +++++++++++++++++ app/furniture/journal/keywords/show.html.erb | 14 ++++++++++++-- app/furniture/journal/keywords_controller.rb | 7 +++++-- spec/factories/furniture/journal.rb | 1 + .../journal/keywords_controller_request_spec.rb | 16 ++-------------- 6 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 app/furniture/journal/keyword_policy.rb diff --git a/app/furniture/journal/keyword.rb b/app/furniture/journal/keyword.rb index e89058681..6eca9ccbf 100644 --- a/app/furniture/journal/keyword.rb +++ b/app/furniture/journal/keyword.rb @@ -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} @@ -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 diff --git a/app/furniture/journal/keyword_policy.rb b/app/furniture/journal/keyword_policy.rb new file mode 100644 index 000000000..cc12a19e4 --- /dev/null +++ b/app/furniture/journal/keyword_policy.rb @@ -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 diff --git a/app/furniture/journal/keywords/show.html.erb b/app/furniture/journal/keywords/show.html.erb index a9de1d323..faddd2b0d 100644 --- a/app/furniture/journal/keywords/show.html.erb +++ b/app/furniture/journal/keywords/show.html.erb @@ -1,2 +1,12 @@ -<%= keyword.canonical_keyword %> -<%= keyword.aliases %> +