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 %> +

<%= keyword.canonical_keyword %>

+ + + diff --git a/app/furniture/journal/keywords_controller.rb b/app/furniture/journal/keywords_controller.rb index 2792f7c7d..5b2f3ce84 100644 --- a/app/furniture/journal/keywords_controller.rb +++ b/app/furniture/journal/keywords_controller.rb @@ -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 diff --git a/spec/factories/furniture/journal.rb b/spec/factories/furniture/journal.rb index ac9b11217..aad752b51 100644 --- a/spec/factories/furniture/journal.rb +++ b/spec/factories/furniture/journal.rb @@ -11,6 +11,7 @@ end factory :journal_keyword, class: "Journal::Keyword" do + canonical_keyword { Faker::Fantasy::Tolkien.location } journal end end diff --git a/spec/furniture/journal/keywords_controller_request_spec.rb b/spec/furniture/journal/keywords_controller_request_spec.rb index afa6db9ce..734860962 100644 --- a/spec/furniture/journal/keywords_controller_request_spec.rb +++ b/spec/furniture/journal/keywords_controller_request_spec.rb @@ -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 From 39f59ab328e63b060702ed3bd9c924c3ed3970df Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Fri, 14 Jul 2023 19:02:23 -0700 Subject: [PATCH 3/5] Use the canonical-keyword when building urls --- app/furniture/journal/keyword.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/furniture/journal/keyword.rb b/app/furniture/journal/keyword.rb index 6eca9ccbf..bcfebbca8 100644 --- a/app/furniture/journal/keyword.rb +++ b/app/furniture/journal/keyword.rb @@ -16,6 +16,10 @@ def entries journal.entries.where("keywords::text[] && ARRAY[?]::text[]", [canonical_keyword] + (aliases.presence || [])) end + def to_param + canonical_keyword + end + def self.extract_and_create_from!(body) body.scan(/#(\w+)/)&.flatten&.map do |keyword| existing_keyword = search(keyword).first From 4db5e020b6af1924025758737bb9484ae9ed98a1 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Thu, 20 Jul 2023 10:50:10 -0700 Subject: [PATCH 4/5] `Journal`: Extract `Entry.matching_keywords([...])` scope --- app/furniture/journal/entry.rb | 2 ++ app/furniture/journal/keyword.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/furniture/journal/entry.rb b/app/furniture/journal/entry.rb index ff0603bae..7e51b67ae 100644 --- a/app/furniture/journal/entry.rb +++ b/app/furniture/journal/entry.rb @@ -32,6 +32,8 @@ class Entry < ApplicationRecord has_one :space, through: :journal before_save :extract_keywords, if: :will_save_change_to_body? + scope :matching_keywords, ->(keywords) { where("keywords::text[] && ARRAY[?]::text[]", keywords) } + def published? published_at.present? end diff --git a/app/furniture/journal/keyword.rb b/app/furniture/journal/keyword.rb index bcfebbca8..e0f8048cc 100644 --- a/app/furniture/journal/keyword.rb +++ b/app/furniture/journal/keyword.rb @@ -13,7 +13,7 @@ class Keyword < ApplicationRecord end) def entries - journal.entries.where("keywords::text[] && ARRAY[?]::text[]", [canonical_keyword] + (aliases.presence || [])) + journal.entries.matching_keywords([canonical_keyword] + (aliases.presence || [])) end def to_param From 0d5ed0dcc877d076e48a1ff5a644e63cc3489b94 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Thu, 20 Jul 2023 10:50:49 -0700 Subject: [PATCH 5/5] Spiff up the Presentation a touch --- app/furniture/journal/keywords/show.html.erb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/furniture/journal/keywords/show.html.erb b/app/furniture/journal/keywords/show.html.erb index faddd2b0d..47d448e8e 100644 --- a/app/furniture/journal/keywords/show.html.erb +++ b/app/furniture/journal/keywords/show.html.erb @@ -1,10 +1,9 @@

<%= keyword.canonical_keyword %>

- +<%- if keyword.aliases.present? %> +

Also known as <%= to_sentence(keyword.aliases) %>

+<%- end %> +

Entries about <%= keyword.canonical_keyword %>