From d214a4c1ffb567db0b2ac9a741aa843b0770c965 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:38:47 -0700 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=A5=94=E2=9C=A8=20`Journal`:=20Sprout?= =?UTF-8?q?=20`Keywords#show`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/zinc-collective/convene/issues/1566 --- app/furniture/journal/keywords/show.html.erb | 2 ++ app/furniture/journal/keywords_controller.rb | 8 ++++++ app/furniture/journal/routes.rb | 5 ++-- .../keywords_controller_request_spec.rb | 26 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 app/furniture/journal/keywords/show.html.erb create mode 100644 app/furniture/journal/keywords_controller.rb create mode 100644 spec/furniture/journal/keywords_controller_request_spec.rb diff --git a/app/furniture/journal/keywords/show.html.erb b/app/furniture/journal/keywords/show.html.erb new file mode 100644 index 000000000..a9de1d323 --- /dev/null +++ b/app/furniture/journal/keywords/show.html.erb @@ -0,0 +1,2 @@ +<%= keyword.canonical_keyword %> +<%= keyword.aliases %> diff --git a/app/furniture/journal/keywords_controller.rb b/app/furniture/journal/keywords_controller.rb new file mode 100644 index 000000000..2792f7c7d --- /dev/null +++ b/app/furniture/journal/keywords_controller.rb @@ -0,0 +1,8 @@ +class Journal + class KeywordsController < Controller + expose(:keyword, scope: -> { policy_scope(journal.keywords) }) + def show + authorize(keyword) + end + end +end diff --git a/app/furniture/journal/routes.rb b/app/furniture/journal/routes.rb index ab0e5a911..008cc0fd0 100644 --- a/app/furniture/journal/routes.rb +++ b/app/furniture/journal/routes.rb @@ -1,8 +1,9 @@ class Journal class Routes def self.append_routes(router) - router.resources :journals do - router.resources :entries, module: "journal" + router.resources :journals, module: "journal" do + router.resources :entries + router.resources :keywords, only: [:show] end end end diff --git a/spec/furniture/journal/keywords_controller_request_spec.rb b/spec/furniture/journal/keywords_controller_request_spec.rb new file mode 100644 index 000000000..afa6db9ce --- /dev/null +++ b/spec/furniture/journal/keywords_controller_request_spec.rb @@ -0,0 +1,26 @@ +require "rails_helper" + +RSpec.describe Journal::KeywordsController, type: :request do + describe "#show" do + subject(:perform_request) do + get polymorphic_path(keyword.location) + response + end + + context "when the :id is the keyword's id" do + it { is_expected.to be_ok } + + 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 + end +end From 07e1562b2840544be7ef2a8eb9e638ec12bcb9cc 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 2/5] 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 %> +
Also known as <%= to_sentence(keyword.aliases) %>
+<%- end %> +