From 7635b71862cb4f3c492fc6e93ebabfe108a43d37 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 18 Jul 2023 09:35:58 +0200 Subject: [PATCH] Fix page search We overwrote ransackable attributes to only include page_layout and urlname, but Alchemy needs name as well. Fixed by adding page_layout to already defined ransackable attributes. --- app/controllers/alchemy/json_api/pages_controller.rb | 5 +++-- app/models/alchemy/json_api/page.rb | 2 +- spec/requests/alchemy/json_api/pages_spec.rb | 12 ++++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/alchemy/json_api/pages_controller.rb b/app/controllers/alchemy/json_api/pages_controller.rb index c3b05d1..bc1aee1 100644 --- a/app/controllers/alchemy/json_api/pages_controller.rb +++ b/app/controllers/alchemy/json_api/pages_controller.rb @@ -6,7 +6,7 @@ class PagesController < JsonApi::BaseController before_action :load_page_for_cache_key, only: :show def index - allowed = [:page_layout, :urlname] + allowed = Alchemy::Page.ransackable_attributes jsonapi_filter(page_scope, allowed) do |filtered_pages| @pages = filtered_pages.result @@ -79,6 +79,7 @@ def load_page def load_page_by_id return unless params[:path] =~ /\A\d+\z/ + page_scope_with_includes.find_by(id: params[:path]) end @@ -104,7 +105,7 @@ def page_scope_with_includes ], }, }, - ] + ], ) end diff --git a/app/models/alchemy/json_api/page.rb b/app/models/alchemy/json_api/page.rb index e181aeb..4e68668 100644 --- a/app/models/alchemy/json_api/page.rb +++ b/app/models/alchemy/json_api/page.rb @@ -2,7 +2,7 @@ module Alchemy # With Ransack 4 we need to define the attributes # that are allowed to be searched. def Page.ransackable_attributes(_auth_object = nil) - %w[urlname page_layout] + super | %w[page_layout] end module JsonApi diff --git a/spec/requests/alchemy/json_api/pages_spec.rb b/spec/requests/alchemy/json_api/pages_spec.rb index e1fdd36..d81a63b 100644 --- a/spec/requests/alchemy/json_api/pages_spec.rb +++ b/spec/requests/alchemy/json_api/pages_spec.rb @@ -264,9 +264,9 @@ context "with filters" do let!(:standard_page) { FactoryBot.create(:alchemy_page, :public, published_at: 2.weeks.ago) } let!(:news_page) { FactoryBot.create(:alchemy_page, :public, page_layout: "news", published_at: 1.week.ago) } - let!(:news_page2) { FactoryBot.create(:alchemy_page, :public, page_layout: "news", published_at: Date.yesterday) } + let!(:news_page2) { FactoryBot.create(:alchemy_page, :public, name: "News", page_layout: "news", published_at: Date.yesterday) } - it "returns only matching pages" do + it "returns only matching pages by page_layout" do get alchemy_json_api.pages_path(filter: { page_layout_eq: "news" }) document = JSON.parse(response.body) expect(document["data"]).not_to include(have_id(standard_page.id.to_s)) @@ -274,6 +274,14 @@ expect(document["data"]).to include(have_id(news_page2.id.to_s)) end + it "returns only matching pages by name" do + get alchemy_json_api.pages_path(filter: { name_eq: "News" }) + document = JSON.parse(response.body) + expect(document["data"]).not_to include(have_id(standard_page.id.to_s)) + expect(document["data"]).not_to include(have_id(news_page.id.to_s)) + expect(document["data"]).to include(have_id(news_page2.id.to_s)) + end + context "if no pages returned for filter params" do it "does not throw error" do get alchemy_json_api.pages_path(filter: { page_layout_eq: "not-found" })