Skip to content

Commit

Permalink
πŸžπŸ”¨ Journal: Fix Keywords#show erroring instead of 404ing (#1755)
Browse files Browse the repository at this point in the history
- #1754

* `Journal`: `Keywords` are case insensitive and aliasable

* `Journal`: `Keyword`s redirect to their canonical location
  • Loading branch information
zspencer authored Aug 14, 2023
1 parent ad16220 commit b955757
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/furniture/journal/keywords_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Journal
class KeywordsController < FurnitureController
expose(:keyword, scope: -> { policy_scope(journal.keywords) }, model: Keyword,
find: ->(id, scope) { scope.find_by(canonical_keyword: id) })
find: lambda do |id, scope|
scope.search(id)&.first ||
(raise ActiveRecord::RecordNotFound.new(nil, self, id, id))
end)

expose(:journal, -> { Journal.find(params[:journal_id]) })
def show
authorize(keyword)
if polymorphic_path(keyword.location) != request.path
redirect_to(polymorphic_path(keyword.location))
end
end
end
end
30 changes: 29 additions & 1 deletion spec/furniture/journal/keywords_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@
response
end

let(:keyword) { create(:journal_keyword) }
let(:keyword) { create(:journal_keyword, aliases: ["pony"]) }
let(:journal) { keyword.journal }

it { is_expected.to be_ok }

context "when the keyword is of a different case" do
subject(:perform_request) do
get polymorphic_path(journal.location(child: :keyword), id: keyword.canonical_keyword.upcase)
response
end

it { is_expected.to redirect_to polymorphic_path(keyword.location) }
end

context "when the keyword is an alias" do
subject(:perform_request) do
get polymorphic_path(journal.location(child: :keyword), id: "Pony")
response
end

it { is_expected.to redirect_to polymorphic_path(keyword.location) }
end

context "when the keyword doesn't exist" do
subject(:perform_request) do
get polymorphic_path(journal.location(child: :keyword), id: "Gibberish")
response
end

it { is_expected.to be_not_found }
end
end
end

0 comments on commit b955757

Please sign in to comment.